【发布时间】:2015-07-13 19:40:47
【问题描述】:
在gcc 4.9.0:
#include <iostream>
#include <map>
struct A
{
typedef int type;
};
template<typename T> void foo(T*) { std::cout << "a" << std::endl; }
template<typename T> void foo(typename T::type*) { std::cout << "b" << std::endl; }
template<typename T>
struct identity
{
typedef T type;
};
template<typename T> void bar(T*) { std::cout << "a" << std::endl; }
template<typename T> void bar(typename identity<T>::type*) { std::cout << "b" << std::endl; }
int main()
{
//auto f = foo<A>; // ambiguous
foo<A>(0); // prints "b", as the second overload is more specific
auto b = bar<A>; // fine
bar<A>(0); // prints "b", as the second overload is more specific (?)
b(0); // prints "b"
return 0;
}
关于为什么在第二种情况下可以获取地址的任何线索?
【问题讨论】:
-
Clang 拒绝这两种情况。
-
g++ 4.9.0 似乎接受了第二个
-
@T.C., g++ 4.9.2 编译代码并生成匹配 OP 描述的输出。
-
@T.C.我认为 Clang 在这里是错误的。至少拒绝
bar,拒绝foo是正确的
标签: c++ templates language-lawyer overload-resolution