首页 文章

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

提问于
浏览
7

考虑这段代码(或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_edgemake_iterator_rangeverticesdegreeadjacent_vertices 在没有 boost:: 名称空间前缀的情况下工作?

对我来说最令人费解的是,根据具体情况,有时需要前缀 . Here is an example,当使用不同的图形结构时会导致编译错误,可以通过前缀 boost::make_iterator_range 来修复 .

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

1 回答

  • 4

    它与 boost 无关,但与任何 namespace 无关 .

    使用argument-dependent lookup(ADL),参数的名称空间将添加到重载搜索中 .

    例如:

    add_edge(0, 1, g);
    

    g 来自命名空间 boost ,因此我们也在命名空间 boost 中查找 add_edge .

相关问题