为什么一些 Boost 函数不需要命名空间前缀
Why some Boost functions don't need prefixing with namespace
考虑此代码(或 live example):
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/range/iterator_range.hpp>
using std::cout;
int main() {
boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> g;
add_edge(0, 1, g);
add_edge(1, 2, g);
for(auto v : make_iterator_range(vertices(g))) {
cout << v << " has " << degree(v, g) << " neighbor(s): ";
for(auto w : make_iterator_range(adjacent_vertices(v, g))) cout << w << ' ';
cout << '\n';
}
return 0;
}
为什么来自 Boost 库的函数 add_edge
、make_iterator_range
、vertices
、degree
和 adjacent_vertices
在没有 boost::
命名空间前缀?
最让我费解的是,根据情况,有时确实需要前缀。 Here is an example,当使用不同的图结构导致编译错误时,可以通过前缀 boost::make_iterator_range
.
来修复
我查看了一下 BGL documentation,但没有找到任何关于此问题的信息。是我的错还是某些 BGL headers 污染了全局命名空间?这是设计使然还是错误?
它与 boost
无关,但与任何 namespace
相关。
使用 argument-dependent lookup (ADL),参数中的名称空间被添加到重载搜索中。
例如:
add_edge(0, 1, g);
g
来自命名空间 boost
,因此我们也在命名空间 boost
.
中查找 add_edge
考虑此代码(或 live example):
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/range/iterator_range.hpp>
using std::cout;
int main() {
boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> g;
add_edge(0, 1, g);
add_edge(1, 2, g);
for(auto v : make_iterator_range(vertices(g))) {
cout << v << " has " << degree(v, g) << " neighbor(s): ";
for(auto w : make_iterator_range(adjacent_vertices(v, g))) cout << w << ' ';
cout << '\n';
}
return 0;
}
为什么来自 Boost 库的函数 add_edge
、make_iterator_range
、vertices
、degree
和 adjacent_vertices
在没有 boost::
命名空间前缀?
最让我费解的是,根据情况,有时确实需要前缀。 Here is an example,当使用不同的图结构导致编译错误时,可以通过前缀 boost::make_iterator_range
.
我查看了一下 BGL documentation,但没有找到任何关于此问题的信息。是我的错还是某些 BGL headers 污染了全局命名空间?这是设计使然还是错误?
它与 boost
无关,但与任何 namespace
相关。
使用 argument-dependent lookup (ADL),参数中的名称空间被添加到重载搜索中。
例如:
add_edge(0, 1, g);
g
来自命名空间 boost
,因此我们也在命名空间 boost
.
add_edge