开发者

What is the type of T?

In the code below:

template<typename T>
struct X {};

int main()
{
  X<int()> x; // what is the type of T ?
}

What is the type of T? I saw something 开发者_如何学编程like this in the boost sources.


Consider the function int func(). It has a function type int(void). It can be implicitly converted to pointer type as the C++ Standard says in 4.3/1, but it this case there's no need in such conversion, so T has the function type int(void), not a pointer to it.


Here is what I did. Though the output of code below is implementation specific, many times it gives a good hint into the type of T that we are dealing with.

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;
} 

The output on VC++ is

int __cdecl(void)

int __cdecl(void)


The type of T is a function that takes no parameters and returns int, as in:

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;
}

T in x and x2 are the same type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜