Extra curly braces when initializing a string

According to the question What is a string array [] = "; and why does it work? I want to ask what is the 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 are extra braces allowed? Any reference to the C ++ standard would be helpful.

+4
source share
2 answers

In C ++ 98 (and C ++ 03) this is pretty simple; in section 8.5:

14 - If Tis a scalar type, then the type declaration is T x = { a };equivalentT x = a;

In C ++ 11, this is described in the initialization list (8.5.4p3):

[...], E, T , E, [...]

, , .

+11

: . §8.5.2/1:

A char ( char, char, char), char16_t array, char32_t array wchar_t array , char16_t , char32_t , , . .

( ++ 11, , .)

, , , C . C , .

-1

Source: https://habr.com/ru/post/1524710/


All Articles