mmm*_*mmm 8 c++ decltype argument-dependent-lookup c++11 visual-studio-2012
我刚刚意识到尝试通过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;
}
Run Code Online (Sandbox Code Playgroud)
给
B::Func(XX const *)
Type: int
Run Code Online (Sandbox Code Playgroud)
在VS2012上
但是(预期的是):
B::Func(XX const *)
Type: f
Run Code Online (Sandbox Code Playgroud)
在gcc 4.7.3上.
因此ADL在调用函数时会起作用(输出中的第1行),但在VS2012中的decltype中使用时则不行.
或者我错过了一些不同的观点?
一个最小的测试用例是:
namespace N
{
struct C {};
C f(C) {};
}
N::C c1;
decltype(f(c1)) c2;
Run Code Online (Sandbox Code Playgroud)
如果编译器不支持 decltype 内部的 ADL,那么上面的代码将无法编译。
有人告诉我它确实可以编译,所以也许 ADL 和模板实例化之间的交互才是问题所在。
归档时间: |
|
查看次数: |
305 次 |
最近记录: |