The std::array is a collection. In this statement:
std::array<char, strlen("hello world!") + 1> s = {"hello world!"};
List initialization is used.
. Since the first and only element of this instance of the std::array class is an array of characters, it can be initialized with string literals.
It would be more correct to use the sizeof operator instead of the strlen function:
std::array<char, sizeof( "hello world!" )> s = {"hello world!"};
Also you could write
std::array<char, sizeof( "hello world!" )> s = { { "hello world!" } };
because the character array is in turn an aggregate.
In accordance with the C ++ standard
8.5.2 Character Arrays [dcl.init.string]
1 Array of narrow character type (3.9.1), char16_t array, char32_t array or wchar_t array can be initialized with a narrow string literal, char16_t string literal, char32_t string literal or wide literal string, respectively, or a correspondingly printed string literal enclosed in curly brackets (2.14.5). Consecutive characters of value string literal initializes the elements of the array.
[Example: char msg [] = "Syntax error in line% s \ n";
Vlad from Moscow
source share