我刚刚意识到尝试通过 decltype 获取函数的返回类型不涉及 VS2012 上的 ADL(参数相关查找)(使用 cl.exe V17.00.60610.1 测试)。
下面的例子
#include <stdio.h>
#include <typeinfo>
namespace A {
int Func(void const *) {
printf("A::Func(void const *)\n");
return 0;
}
template <typename T> void Do(T const &t) {
Func(&t);
}
template <typename T> void PrintType(T const &t) {
printf("Type: %s\n", typeid(decltype(Func(&t))).name());
}
}
namespace B {
struct XX { };
float Func(XX const *) {
printf("B::Func(XX const *)\n");
return 0.0f;
}
}
int main(int argc, char **argv) {
B::XX xx;
A::Do(xx);
A::PrintType(xx);
return 0;
}
给
B::Func(XX const *)
Type: int
在 VS2012 上
但是(预期的):
B::Func(XX const *)
Type: f
在 gcc 4.7.3 上。
因此,ADL 在调用函数(输出中的第 1 行)时有效,但在 VS2012 上的 decltype 内使用时无效。
还是我错过了一些不同的观点?