Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double parentheses around function parameter list

Tags:

c++

I have the following code, it compiles with gcc 4.4 with no warnings, and returns 42.

template<typename T>
struct foo
{ };

template<typename T>
struct foo<void (T)>
{
  enum { value = 42 };
};

int main()
{
  return foo<void ((int))>::value;
}

Now, I see why it should work when the template parameter is void (int), but what's the deal with the double parentheses? Is this legal in C++? Is it the same as void (int)?

Cheers

like image 974
xcvii Avatar asked Jan 24 '26 08:01

xcvii


1 Answers

In this case, void ((int)) is identical to void (int).
void ((int)) part in foo<void ((int))> is called type-id.
According to §8.1/1, type-id is composed of type-specifier-seq and abstract-declarator.
In void ((int)), type-specifier-seq is void and abstract-declarator is ((int)), and abstract-declarator can be parenthesized arbitrarily.
This is legal in C and C++.

like image 134
Ise Wisteria Avatar answered Jan 27 '26 00:01

Ise Wisteria