According to question What does string array[] = ""; mean and why does it work? I want to ask what difference between s1 and s2 in the code below:
int main() {
const char* s1 = { "Hello" }; // strange but work as followed
const char* s2 = "Hello"; // ordinary case
return 0;
}
Why extra curly braces are permitted? Any reference to C++ standard will be useful.
In C++98 (and C++03) this is pretty simple; in clause 8.5:
14 - If
T
is a scalar type, then a declaration of the formT x = { a };
is equivalent toT x = a;
In C++11 this is covered by list-initialization (8.5.4p3):
[...] if the initializer list has a single element of type E and either T is not a reference type or its referenced type is reference-related to E, the object or reference is initialized from that element [...]
I think this is the same question as Initializing scalars with braces.
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