C function declarators
First of all, there is C. In C, A a()
is function declaration. For example, putchar
has the following declaration. Normally, such declarations are stored in header files, however nothing stops you from writing them manually, if you know how the declaration of function looks like. The argument names are optional in declarations, so I omitted it in this example.
int putchar(int);
This allows you to write the code like this.
int puts(const char *);
int main() {
puts("Hello, world!");
}
C also allows you to define functions that take functions as arguments, with nice readable syntax that looks like a function call (well, it's readable, as long you won't return a pointer to function).
#include <stdio.h>
int eighty_four() {
return 84;
}
int output_result(int callback()) {
printf("Returned: %d\n", callback());
return 0;
}
int main() {
return output_result(eighty_four);
}
As I mentioned, C allows omitting argument names in header files, therefore the output_result
would look like this in header file.
int output_result(int());
One argument in constructor
Don't you recognize that one? Well, let me remind you.
A a(B());
Yep, it's exactly the same function declaration. A
is int
, a
is output_result
, and B
is int
.
You can easily notice a conflict of C with new features of C++. To be exact, constructors being class name and parenthesis, and alternate declaration syntax with ()
instead of =
. By design, C++ tries to be compatible with C code, and therefore it has to deal with this case - even if practically nobody cares. Therefore, old C features have priority over new C++ features. The grammar of declarations tries to match the name as function, before reverting to the new syntax with ()
if it fails.
If one of those features wouldn't exist, or had a different syntax (like {}
in C++11), this issue would never have happened for syntax with one argument.
Now you may ask why A a((B()))
works. Well, let's declare output_result
with useless parentheses.
int output_result((int()));
It won't work. The grammar requires the variable to not be in parentheses.
<stdin>:1:19: error: expected declaration specifiers or ‘...’ before ‘(’ token
However, C++ expects standard expression here. In C++, you can write the following code.
int value = int();
And the following code.
int value = ((((int()))));
C++ expects expression inside inside parentheses to be... well... expression, as opposed to the type C expects. Parentheses don't mean anything here. However, by inserting useless parentheses, the C function declaration is not matched, and the new syntax can be matched properly (which simply expects an expression, such as 2 + 2
).
More arguments in constructor
Surely one argument is nice, but what about two? It's not that constructors may have just one argument. One of built-in classes which takes two arguments is std::string
std::string hundred_dots(100, '.');
This is all well and fine (technically, it would have most vexing parse if it would be written as std::string wat(int(), char())
, but let's be honest - who would write that? But let's assume this code has a vexing problem. You would assume that you have to put everything in parentheses.
std::string hundred_dots((100, '.'));
Not quite so.
<stdin>:2:36: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
In file included from /usr/include/c++/4.8/string:53:0,
from <stdin>:1:
/usr/include/c++/4.8/bits/basic_string.tcc:212:5: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
basic_string<_CharT, _Traits, _Alloc>::
^
I'm not sure why g++ tries to convert char
to const char *
. Either way, the constructor was called with just one value of type char
. There is no overload which has one argument of type char
, therefore the compiler is confused. You may ask - why the argument is of type char?
(100, '.')
Yes, ,
here is a comma operator. The comma operator takes two arguments, and gives the right-side argument. It isn't really useful, but it's something to be known for my explanation.
Instead, to solve the most vexing parse, the following code is needed.
std::string hundred_dots((100), ('.'));
The arguments are in parentheses, not the entire expression. In fact, just one of expressions needs to be in parentheses, as it's enough to break from the C grammar slightly to use the C++ feature. Things brings us to the point of zero arguments.
Zero arguments in constructor
You may have noticed the eighty_four
function in my explanation.
int eighty_four();
Yes, this is also affected by the most vexing parse. It's a valid definition, and one you most likely have seen if you created header files (and you should). Adding parentheses doesn't fix it.
int eighty_four(());
Why is that so? Well, ()
is not an expression. In C++, you have to put an expression between parentheses. You cannot write auto value = ()
in C++, because ()
doesn't mean anything (and even if did, like empty tuple (see Python), it would be one argument, not zero). Practically that means you cannot use shorthand syntax without using C++11's {}
syntax, as there are no expressions to put in parenthesis, and C grammar for function declarations will always apply.
(B())
is just a C++ expression, nothing more. It's not any kind of exception. The only difference that it makes is that there's no way it can be possibly parsed as a type, and so it's not. – Portuguese()
was doing around theB()
). Just like howA a(((((((((B())))))))));
or any number of parenthesis similarly works. – MilterA a();
is not of the same category. For the compiler, there is never any different way to parse it: An initializer at that place never consists of empty parentheses, so this is always a function declaration. – SilverfishA a();
is not an example of the most vexing parse. It is simply a function declaration, just like it is in C. – DysfunctionA a;
" is wrong. That won't give you initialization of a POD type. To get intitialization writeA a{};
. – SargassumA a();
stems from the fact that bothA *a = new A;
andA *b = new A();
are legitimate ways of usingnew
to construct anA
. Whether that second of these should ever have been allowed, given the behavior ofA a();
is an interesting academic question, but the reality is that particular boat sailed away many many years ago. – Hoahoactzin