Is foo(void) equivalent to foo() in a template context?

Viewed 70

For C legacy reasons, the following definitions in C++ are equivalent:

void foo() {}
void foo(void) {}

What happens when foo is in a templated class ? For example:

template <typename T> struct C { void foo(T) {} };

int main()
{ C<void> c;
  c.foo();
}

MSVC (19.30) accepts this code, but gcc (11.2) rejects it. It seems that compilers disagree as to whether foo(T) is equivalent to foo() when T = void.

Which is correct ?

1 Answers

The specification says (C++11 §8.3.5[dcl.func]/4):

A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list. Except for this special case, a parameter shall not have type cv void.

And since in your case you have a dependent type(depending on T), gcc is correct.

Related