Why some Boost functions don't need prefixing with namespace

Consider this code (or the 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;
}

Why do functions add_edge , make_iterator_range , vertices , degree and adjacent_vertices that come from the Boost library work without the boost:: namespace prefix?

What is most puzzling to me is that depending on the situation, the prefix is actually needed sometimes. Here is an example, when using a different graph structure results in a compilation error that can be fixed by prefixing boost::make_iterator_range .

I looked a bit around the BGL documentation, but didn't find anything regarding this issue. Is it my fault or are some BGL headers polluting the global namespace? Is this by design or is this a bug?


It is not related to boost but to any namespace .

With argument-dependent lookup (ADL), namespaces from the argument are added to the search of overloads.

So for example:

add_edge(0, 1, g);

g is from namespace boost , so we look for add_edge also in namespace boost .

链接地址: http://www.djcxy.com/p/88568.html

上一篇: 如何从Read + Seek到地道/有效地管道数据写入?

下一篇: 为什么某些Boost函数不需要使用名称空间的前缀