为什么某些 Boost 函数不需要以命名空间为前缀
Why some Boost functions don't need prefixing with namespace
考虑以下代码(或实时示例(:
#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::
命名空间前缀的情况下工作?
最令我困惑的是,根据情况,有时实际上需要前缀。下面是一个示例,当使用不同的图形结构会导致编译错误时,可以通过前缀 boost::make_iterator_range
来修复。
我浏览了一下BGL文档,但没有找到有关此问题的任何内容。是我的错还是某些 BGL 标头污染了全局命名空间?这是设计使然还是错误?
它与boost
无关,但与任何namespace
有关。
使用依赖于参数的查找 (ADL(,参数中的命名空间将添加到重载搜索中。
所以例如:
add_edge(0, 1, g);
g
来自命名空间 boost
,所以我们也在命名空间boost
中寻找add_edge
。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- QXmlStreamWriter,命名空间和前缀
- 有没有办法在不使用命名空间 std 或前缀 std:: 的情况下引用 cout?
- 自定义提升前缀/命名空间 - 如何使用
- 如何避免GDB中符号的命名空间前缀?
- connect() 似乎用错误的命名空间作为信号的前缀
- 具有前缀映射和不带前缀映射的 Pugiparse 命名空间
- 在命名空间前缀解析和优化级别依赖关系中使用外部"C" C++
- 使用 std 命名空间,为什么(或为什么不)我应该在 std:: 前面加上前缀
- 为什么命名空间中的某些函数可以在没有命名空间范围前缀的情况下访问
- 在 CPP 文件中使用命名空间作为函数定义的前缀是否是一种好的做法
- 在C++命名空间中,当在头中声明的非成员子例程前面加前缀时,“static”限定符是否有任何作用
- 为什么某些 Boost 函数不需要以命名空间为前缀
- 我想从这段代码中删除使用命名空间std,但我不确定什么都需要用std::作为前缀