自从我更新到 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
也许我的代码是错误的,或者它真的是优化器中的一个错误?任何提示将非常感谢!