在下面的代码中:
template<typename T>
struct X {};
int main()
{
X<int()> x; // what is the type of T ?
}
T的类型是什么?我在boost
来源中看到了类似的内容。
答案 0 :(得分:12)
考虑函数int func()
。它有一个函数类型int(void)
。它可以隐式转换为指针类型,如C ++标准在4.3 / 1中所述,但是在这种情况下不需要进行此类转换,因此T
具有函数类型int(void)
,而不是指向它的指针
答案 1 :(得分:2)
这就是我所做的。虽然下面的代码输出是特定于实现的,但很多时候它提供了我们正在处理的T类型的良好提示。
template<typename T>
struct X {
X(){
cout << typeid(T).name();
}
};
int main()
{
X<int()> x; // what is the type of T ?
cout << typeid(int()).name() << endl;
}
VC ++上的输出是
int __cdecl(void)
int __cdecl(void)
答案 2 :(得分:0)
T
的类型是一个不带参数并返回int
的函数,如:
template<typename T>
struct X {};
int foo()
{
return 42;
}
int main()
{
X<int()> x; // what is the type of T ?
typedef int(foo_ptr)();
X<foo_ptr> x2;
return 0;
}
<{1}}和T
中的{p> x
属于同一类型。