Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T(U) - What does this syntax mean in c++ template

Tags:

c++

I read STL and encounter this pattern:

template <typename S>
class F;

// specialization
template <typename T, typename U>
class F<T(U)>{};

It compiles OK, so what does T(U) mean here?

like image 532
shan Avatar asked Oct 24 '25 05:10

shan


1 Answers

T(U) is a type-id denoting the type "function with a single parameter of type U and return type T".

However, C++ is context-sensitive and this is only parsed as type-id because lookup of U finds a type (because it names a type template parameter). If lookup of U found a non-type, then T(U) would be parsed as expression instead, in which case it would be a functional-style explicit cast which is equivalent to the C style cast (T)U.

And if both U and T would name non-types, then it would be parsed as a function call expression.

like image 117
user17732522 Avatar answered Oct 26 '25 17:10

user17732522