I just realized that trying to get the return type of a function via decltype does not involve ADL (argument-dependent-lookup) on VS2012 (tested using cl.exe V17.00.60610.1).
我刚刚意识到,通过decltype获取函数的返回类型并不需要在VS2012上使用ADL(参数依赖查找)(使用cl进行测试)。exe V17.00.60610.1)。
The following example
下面的例子
#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;
}
Gives
给了
B::Func(XX const *)
Type: int
on VS2012
在VS2012
but (what is expected):
但(预计):
B::Func(XX const *)
Type: f
on gcc 4.7.3.
在gcc 4.7.3。
So ADL works when calling the function (line 1 in output) but not when used inside decltype on VS2012.
所以ADL在调用函数(输出中的第1行)时起作用,但在VS2012中使用时不起作用。
Or am I missing some different point?
还是我漏掉了别的点?
2 个解决方案
#1
2
A minimal test case is:
一个最小的测试用例是:
namespace N
{
struct C {};
C f(C) {};
}
N::C c1;
decltype(f(c1)) c2;
If the compiler doesn't support ADL inside decltype, then the above will not compile.
如果编译器不支持在decltype里面的ADL,那么上面的代码就不会被编译。
I'm told it does compile, so maybe it is the interaction between ADL and template instantiation that is the problem.
我被告知它是编译的,所以可能是ADL和模板实例化之间的交互才是问题所在。
#2
2
If find it amusing that the IDE/Intellisense whatsoever seems to do the lookup correctly but the compiler does not.
有趣的是,IDE/智能感知似乎可以正确地执行查找,但是编译器却不能。
This example shows no intellisense errors and a
is displayed to be of type size_t
when hovering it.
这个示例没有显示任何智能感知错误,在悬停a时显示为size_t类型。
#include <iostream>
namespace A
{
struct C {};
size_t f(C*) { return 5U; };
}
namespace B
{
void f(void *) { };
void f2 (A::C x)
{ decltype(f(&x)) a; std::cout << typeid(a).name() << std::endl; }
}
int main (void)
{
A::C c;
B::f2(c);
}
The compiler stops with Error C2182
and complains about a variable of type void. It seems to be a problem independant of templates.
编译器以错误C2182停止,并对类型为void的变量进行报错。这似乎是一个与模板无关的问题。
#1
2
A minimal test case is:
一个最小的测试用例是:
namespace N
{
struct C {};
C f(C) {};
}
N::C c1;
decltype(f(c1)) c2;
If the compiler doesn't support ADL inside decltype, then the above will not compile.
如果编译器不支持在decltype里面的ADL,那么上面的代码就不会被编译。
I'm told it does compile, so maybe it is the interaction between ADL and template instantiation that is the problem.
我被告知它是编译的,所以可能是ADL和模板实例化之间的交互才是问题所在。
#2
2
If find it amusing that the IDE/Intellisense whatsoever seems to do the lookup correctly but the compiler does not.
有趣的是,IDE/智能感知似乎可以正确地执行查找,但是编译器却不能。
This example shows no intellisense errors and a
is displayed to be of type size_t
when hovering it.
这个示例没有显示任何智能感知错误,在悬停a时显示为size_t类型。
#include <iostream>
namespace A
{
struct C {};
size_t f(C*) { return 5U; };
}
namespace B
{
void f(void *) { };
void f2 (A::C x)
{ decltype(f(&x)) a; std::cout << typeid(a).name() << std::endl; }
}
int main (void)
{
A::C c;
B::f2(c);
}
The compiler stops with Error C2182
and complains about a variable of type void. It seems to be a problem independant of templates.
编译器以错误C2182停止,并对类型为void的变量进行报错。这似乎是一个与模板无关的问题。