为什么某些 Boost 函数不需要以命名空间为前缀

Why some Boost functions don't need prefixing with namespace

本文关键字:命名空间 前缀 不需要 函数 Boost 为什么      更新时间:2023-10-16

考虑以下代码(或实时示例(:

#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::命名空间前缀的情况下工作?

最令我困惑的是,根据情况,有时实际上需要前缀。下面是一个示例,当使用不同的图形结构会导致编译错误时,可以通过前缀 boost::make_iterator_range 来修复。

我浏览了一下BGL文档,但没有找到有关此问题的任何内容。是我的错还是某些 BGL 标头污染了全局命名空间?这是设计使然还是错误?

它与boost无关,但与任何namespace有关。

使用依赖于参数的查找 (ADL(,参数中的命名空间将添加到重载搜索中。

所以例如:

add_edge(0, 1, g);

g 来自命名空间 boost ,所以我们也在命名空间boost中寻找add_edge