我最近开始喜欢自由函数std::next并std::prev显式复制和递增/递减迭代器.现在,我在一个非常具体的案例中看到了奇怪的行为,我很感激任何帮助揭开它的神秘面纱.
我有一个插值/外推功能在boost::any_range一些操作X_type.范围类型的完整定义是:
boost::any_range <
const X_type,
boost::random_access_traversal_tag,
const X_type,
std::ptrdiff_t
>
Run Code Online (Sandbox Code Playgroud)
的any_range,在该特定情况下,从所分配的iterator_range保持两个指针到const X_type,作为一个X_type大约一半的视图data()的一个区域vector<char>.
在MSVC 2010中编译我的应用程序,一切正常.在MinGW g ++ 4.7.0中编译相同的代码,它似乎挂在一个特定的位置,然后我缩小到这个(稍微缩写):
// Previously ensured conditions:
// 1) xrange is nonempty;
// 2) yrange is the same size as xrange.
auto x_equal_or_greater =
std::lower_bound(std::begin(xrange),std::end(xrange),xval);
if (x_equal_or_greater == std::end(xrange))
{
return *yit_from_xit(std::prev(x_equal_or_greater),xrange,yrange);
}
Run Code Online (Sandbox Code Playgroud)
通过在gdb单步调试代码,我发现它没有被卡住,只是走了很长的时间,从单回std::prev电话-这++的libstdc中的条款实施std::advance,最终的+=运营商.
只需将return线替换为:
auto xprev=x_equal_or_greater; …Run Code Online (Sandbox Code Playgroud)