This question has this code snippet:
A::A(const char *pc) {
A(string(pc));
}
A::A(string s) {
vector<string> tmpVector;
tmpVector.push_back(s);
A(tmpVector); // <-- error
}
// Constructor
A::A(vector<string> filePathVector) {
}
The problem is that A(tmpVector);
conflicts with vector<string> tmpVector;
:
error: conflicting declaration 'A tmpVector'
error: 'tmpVector' has a previous declaration as 'std::vector<std::basic_string<char> > tmpVector'
The answer says:
This
A(tmpVector);
is the same as this
A tmpVector; // but there is already an object called tmpVector
With an added comment:
In this context, the () are superfluous.
My question is: why are the parenthesis superfluous? What exactly in the C++11 spec makes that so? I have not seen this before.
From §8 [dcl.decl] of the standard:
(Remainder of grammar omitted).
In particular, note that
ptr-declarator
is adeclarator
.( ptr-declarator )
is anoptr-declarator
which in turn is aptr-declarator
.In other words, you can have as many pairs of parentheses as you want and it's still a declarator. Now this causes an ambiguity in cases like
T(x);
, which is resolved by §6.8 [stmt.ambig] of the standard:The example accompanying that paragraph directly covers this case: