为什么有些Boost函数不需要使用名称空间前缀

are*_*lek 7 c++ boost boost-graph argument-dependent-lookup

考虑这段代码(或实例):

#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;
}
Run Code Online (Sandbox Code Playgroud)

为什么功能add_edge,make_iterator_range,vertices,degreeadjacent_vertices那来自无Boost库工作boost::空间前缀?

对我来说最令人费解的是,根据具体情况,有时需要前缀.下面是一个示例,当使用不同的图形结构时,会产生可以通过前缀修复的编译错误boost::make_iterator_range.

我看了一下BGL文档,但没有找到任何关于这个问题的内容.是我的错还是一些BGL标题污染全局命名空间?这是设计还是这个错误?

Jar*_*d42 6

它与boost任何namespace.

使用参数相关查找(ADL),参数中的命名空间被添加到重载搜索中。

例如:

add_edge(0, 1, g);
Run Code Online (Sandbox Code Playgroud)

g来自 namespace boost,所以我们add_edge也在 namespace 中寻找boost

  • 我认为这并不能真正回答 OP 链接的其他代码示例无法编译的原因。 (2认同)