c++ - 为什么一些 Boost 函数不需要命名空间前缀

标签 c++ boost boost-graph argument-dependent-lookup

考虑这段代码(或 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;
}

为什么函数 add_edgemake_iterator_rangeverticesdegreeadjacent_vertices来自 Boost 库的没有 boost:: 命名空间前缀的工作?

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

我环顾了一下BGL documentation ,但没有找到有关此问题的任何信息。是我的错还是某些 BGL header 污染了全局命名空间?这是设计使然还是错误?

最佳答案

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

argument-dependent lookup (ADL),来自参数的 namespace 被添加到重载搜索中。

例如:

add_edge(0, 1, g);

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

关于c++ - 为什么一些 Boost 函数不需要命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461987/

相关文章:

c++ - {} 是传递给需要迭代器(代表某个容器的 std::end() )的函数的有效参数吗?

c++ - 为什么 boost::function 慢?

c++ - Boost Graph - 大图上的 Astar 非常慢

c++ - 加载难看的输入

c++ - QT4内存管理

c++ - boost::aligned_storage 在返回时像 POD 类型一样复制非 POD 对象

c++ - boost 图形库 : access violation in reading from adjacency_list in parallel mode

c++ - 在 C 和 C++ 中发送 UDP 套接字

c++ - 从带有 opengl 代码的 C++ 转换为带有 GUI 的 QT 应用程序?

c++ - 具有自定义顶点属性的图的 boost 支配树