Why is the following code legal in C++?
bool a(false);
I mean, the T a(VALUE)
should call constructor, right? I suppose it's not parsed as function declaration. But bool
is plain type, it doesn't have constructor. Or does it?
I'm using Visual Studio 2012 if it's relevant.
Although bool
is a primitive type, and as such has no constructor, language designers introduced unified initialization syntax that works for primitives as well as for classes. This greatly simplifies writing template code, because you can continue using the
T tVar(initialVal);
syntax without knowing if T
, a template type parameter, is primitive or not. This is a very significant benefit to template designers, because they no longer need to think about template type parameters in terms of primitive vs. classes.
That is just a valid syntax to initialize POD types and have a similar behavior to a constructor (or even a copy constructor for that matter).
For example, the following would be valid:
bool a(false);
bool b(a);
bool c = bool(); // initializes to false
One interesting thing to note is that in
int main(int argc, const char *argv[])
{
bool f();
return 0;
}
f
is a function declaration!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With