c++ - 这是 Visual Studio 2013 update 4 C++ 优化器错误还是我的代码有误?

标签 c++ c++11 boost boost-range

自从我更新到 boost 1.58 和 VS2013 以来,我一直看到我们的软件崩溃。只有当编译器优化开启时,我们才会看到崩溃。使用 boost 1.55 没有崩溃。我已经设法隔离了我在 boost::any_range 中看到的问题以及我们如何使用它。

查看下面的示例代码:

#include <boost/range/any_range.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <vector>
#include <memory>
#include <cstdio>

class DummyElement
{
public:
    float f_;
};

using ElementRange = boost::any_range < DummyElement*, boost::bidirectional_traversal_tag >;

using DummyElementUPtr = std::unique_ptr < DummyElement > ;

class BoostAnyTest
{
public:
    BoostAnyTest()
    { 
        for (int i = 0; i < 10; ++i)
        {
            auto element = DummyElementUPtr(new DummyElement());
            _tprintf(_T("BoostAnyTest::ctor() 0x%p\n"), element.get());

            c_.emplace_back(std::tuple<Int, DummyElementUPtr>(i, std::move(element)));
        }
    }

public:
    ElementRange GetAll();

private:
    using _ContainerType = std::vector < std::tuple<Int, std::unique_ptr<DummyElement>> > ;
    _ContainerType    c_;
};


ElementRange
BoostAnyTest::GetAll()
{
    auto transform = [ ] (const _ContainerType::value_type& v) -> DummyElement*
    {
        return std::get<1>(v).get();
    };

    return c_ | boost::adaptors::transformed(transform);
}


int
main()
{
    BoostAnyTest    any;

    auto range = any.GetAll();

    std::for_each(std::begin(range), std::end(range), [ ] (DummyElement* element)
    { 
        _tprintf(_T("TestBoostAnyRange() 0x%p\n"), element);
    });
}

下面是程序输出。 DEBUG 版本输出是我所期望的,但优化的 RELEASE 版本目前对我来说是一个谜...

DEBUG version output:
BoostAnyTest::ctor() 0x007D0FB0
BoostAnyTest::ctor() 0x007D0E30
BoostAnyTest::ctor() 0x007D0E60
BoostAnyTest::ctor() 0x007D1160
BoostAnyTest::ctor() 0x007D0E90
BoostAnyTest::ctor() 0x007D10A0
BoostAnyTest::ctor() 0x007D0F80
BoostAnyTest::ctor() 0x007D0FE0
BoostAnyTest::ctor() 0x007D1010
BoostAnyTest::ctor() 0x007D1040
TestBoostAnyRange() 0x007D0FB0
TestBoostAnyRange() 0x007D0E30
TestBoostAnyRange() 0x007D0E60
TestBoostAnyRange() 0x007D1160
TestBoostAnyRange() 0x007D0E90
TestBoostAnyRange() 0x007D10A0
TestBoostAnyRange() 0x007D0F80
TestBoostAnyRange() 0x007D0FE0
TestBoostAnyRange() 0x007D1010
TestBoostAnyRange() 0x007D1040

RELEASE version output:
BoostAnyTest::ctor() 0x00BFA358
BoostAnyTest::ctor() 0x00BFA238
BoostAnyTest::ctor() 0x00BFA3E8
BoostAnyTest::ctor() 0x00BFA248
BoostAnyTest::ctor() 0x00BFA258
BoostAnyTest::ctor() 0x00BFA268
BoostAnyTest::ctor() 0x00C2ECB8
BoostAnyTest::ctor() 0x00C2ED98
BoostAnyTest::ctor() 0x00C2EDA8
BoostAnyTest::ctor() 0x00C2ED48
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0
TestBoostAnyRange() 0x00A5FCE0

也许我的代码是错误的,或者它真的是优化器中的错误?任何提示将不胜感激!

最佳答案

这是 Boost bug 10493 (在 Boost 1.56 中引入,这就是您的代码可与 Boost 1.55 一起使用的原因)。

解决方法是使用 T const 作为 Reference 模板参数,在您的情况下:

using ElementRange = boost::any_range <
    DummyElement*,
    boost::bidirectional_traversal_tag,
    DummyElement* const
>;

Example .

关于c++ - 这是 Visual Studio 2013 update 4 C++ 优化器错误还是我的代码有误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31402815/

相关文章:

c++ - 重载解析和显式模板参数

android - 是什么导致信号 'SIGILL' ?

C++/Thrift:TThreadedServer::stop() 是线程安全的吗?

python - Boost Python用默认参数包装静态成员函数重载

c++ - 如何批量使用同一个线程池

c++ - 可序列化的 Boost 库 header 和仅 header 库

c++ - 是否可以使用 GDB 查看未命名的框架/函数参数?

c++ - 来自 C++ 源的 C-DLL

c++ - 使用 <ctime> 和指令重新排序进行基准测试

c++ - packaged_task 和 async 有什么区别