小编buc*_*els的帖子

C++中的奇怪重载规则

我正在尝试使用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* …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading namespaces g++

4
推荐指数
1
解决办法
512
查看次数

标签 统计

c++ ×1

g++ ×1

namespaces ×1

overloading ×1

templates ×1