gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:37:38 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com