为什么在GCC 9中,对copy_n的调用会在std::copy_n与 __gnu_cxx::copy_n之间产生歧义,而在GCC 13中则不会?
以下代码在g++-9下会产生编译错误:
#include <ext/algorithm>
#include <algorithm>
#include <vector>
int main() {
std::vector<unsigned int> a[3], b[3];
copy_n(a, 3u, b);
}
错误信息:
a.cpp: In function ‘int main()’:
a.cpp:8:20: error: call of overloaded ‘copy_n(std::vector<unsigned int> [3], unsigned int, std::vector<unsigned int> [3])’ is ambiguous
8 | copy_n(a, 3u, b);
| ^
In file included from /usr/include/c++/9/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
from a.cpp:1:
/usr/include/c++/9/bits/stl_algo.h:799:5: note: candidate: ‘_OIter std::copy_n(_IIter, _Size, _OIter) [with _IIter = std::vector<unsigned int>*; _Size = unsigned int; _OIter = std::vector<unsigned int>*]’
799 | copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
| ^~~~~~
In file included from /usr/include/c++/9/ext/ropeimpl.h:47,
from /usr/include/c++/9/ext/rope:2975,
from a.cpp:2:
/usr/include/c++/9/ext/algorithm:120:5: note: candidate: ‘std::pair<_InputIterator, _ForwardIterator> __gnu_cxx::copy_n(_InputIterator, _Size, _OutputIterator) [with _InputIterator = std::vector<unsigned int>*; _Size = unsigned int; _OutputIterator = std::vector<unsigned int>*]’
120 | copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
| ^~~~~~
std::copy_n 是通过ADL查找找到的,但 __gnu_cxx::copy_n 是如何被找到的?
在使用g++-13时,代码可以编译。为什么?
解决方案
GCC11及以下版本:
std::vector<int, std::allocator<int>> -> std::allocator<int> 继承自 __gnu_cxx::new_allocator<int>(来自 <ext/new_allocator.h>),因此 __gnu_cxx 是一个关联命名空间。
GCC12:
libstdc++已修补,将 __gnu_cxx::new_allocator 改为 std::__new_allocator(在提交中 fe9571a),因此 __gnu_cxx 不再是任何具有 std::allocator 的对象的关联命名空间。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。