boost::geometry 和 std 命名空间与 Visual Studio 2013 冲突

boost::geometry and std namespace conflict with Visual Studio 2013

我正在使用 Visual Studio 2013 并且向导创建了 Win32 控制台应用程序项目。此代码:

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>

    using namespace std;
    using namespace boost::geometry;

    int main()
    {
        model::d2::point_xy<int> p1(1, 1), p2(2, 2);
        cout << "Distance p1-p2 is: " << distance(p1, p2) << endl;

        return 0;
    }

生成一个长模板 barf,开头为:

error C2039: 'iterator_category' : is not a member of 'boost::geometry::model::d2::point_xy<int,boost::geometry::cs::cartesian>'

相同的代码在 gcc 和 clang 上运行良好。我必须更改我的项目设置才能编译吗?

编辑

此代码适用于 Visual Studio 2015 CTP,因此问题是 VS2013 中的重载解析不足,正如 Marc Glisse 指出的那样。

using namespace std 不是问题的根源,因为 juanchopanza claims. Considering it a bad practice is somewhat arbitrary, as there are many arguments to the contrary (see discussion at Why is “using namespace std;” considered bad practice?)。在这种特定情况下,可以争辩说 using namespace boost::geometry 是一种不好的做法,而不是 using namespace std

一些编辑试图从 Whosebug 问题中删除所有冗余,这将使搜索变得更加困难,这是由于先有鸡还是先有蛋的悖论:在开始成功搜索之前,人们必须知道问题的答案。最终您可以将整个 Whosebug 减少到 42,但实用程序会受到影响。

std::distance 函数计算两个迭代器之间的距离。

尝试删除 using namespace std 或适当限定 boost distance 函数。

编辑

The question remains, why gcc and clang didn't complain?

正如 @MarcGlisse 指出的,其原因可能归结为 C++ 库实现所采用的复杂程度:

Recent versions of libstdc++ and libc++ use sfinae

Old versions of Visual Studio don't do that yet

关于为什么它适用于某些编译器而不适用于其他编译器,这是一个非常合理的解释,因为 SFINAE 是一种从编译器将考虑的可能函数集中删除函数的机制。有关 SFINAE 的更多信息 (cppreference.com and WIKIPEDIA)