7

[class.ctor]/1

Constructors do not have names. In a declaration of a constructor, the declarator is a function declarator (8.3.5) of the form ptr-declarator ( parameter-declaration-clause ) exception-specificationopt attribute-specifier-seqopt where the ptr-declarator consists solely of an id-expression, an optional attribute-specifier-seq, and optional surrounding parentheses, and the id-expression has one of the following forms: ...

And yes, this compiles:

struct S{
    (S)() {}
};

But why is this allowed?

João Afonso
  • 1,934
  • 13
  • 19

1 Answers1

0

This is because the constructor is a function, and most declarations can be surrounded by parentheses (even multiple):

// all are valid!
void ((a))(); // void a();
int (a); // int a;
struct S {
    (S)(); // constructors
    (~S)(); // destructors
};

See Why does C++ allow us to surround the variable name in parentheses when declaring a variable? for why this is allowed. This syntax was probably extended to constructors when they were defined.

Community
  • 1
  • 1
ralismark
  • 746
  • 9
  • 24