buc*_*els 4 c++ templates overloading namespaces g++
我正在尝试使用GCC 4.5.0编译此代码:
#include <algorithm>
#include <vector>
template <typename T> void sort(T, T) {}
int main()
{
std::vector<int> v;
sort(v.begin(), v.end());
}
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用:
$ g++ -c nm.cpp
nm.cpp: In function ‘int main()’:
nm.cpp:9:28: error: call of overloaded ‘sort(std::vector<int>::iterator, std::vector<int>::iterator)’ is ambiguous
nm.cpp:4:28: note: candidates are: void sort(T, T) [with T = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/bits/stl_algo.h:5199:69: note: void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
Run Code Online (Sandbox Code Playgroud)
Comeau编译这段代码没有错误.(4.3.10.1 Beta2,严格的C++ 03,没有C++ 0x)
这是有效的C++吗?
为什么GCC甚至考虑std::sort作为有效的过载?
我做了一些实验,我想我知道为什么Comeau 可以编译这个(但我不知道这个事实):
namespace foo {
typedef int* iterator_a;
class iterator_b {};
template <typename T> void bar(T) {}
}
template <typename T> void bar(T) {}
int main()
{
bar(foo::iterator_a()); // this compiles
bar(foo::iterator_b()); // this doesn't
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是第一个调用解析为bar(int*)没有ADL且没有歧义,而第二个调用解析bar(foo::iterator_b)并拉入foo::bar(但我不确定).
所以GCC可能会使用iterator_bComeau使用的东西iterator_a.
| 归档时间: |
|
| 查看次数: |
512 次 |
| 最近记录: |