C++11: ¿Correcta inicialización de std:: array?
Si inicializo un std:: array de la siguiente manera, el compilador me da una advertencia sobre llaves faltantes
std::array<int, 4> a = {1, 2, 3, 4};
Esto soluciona el problema:
std::array<int, 4> a = {{1, 2, 3, 4}};
Este es el mensaje de advertencia:
missing braces around initializer for 'std::array<int, 4u>::value_type [4] {aka int [4]}' [-Wmissing-braces]
¿Es esto solo un error en mi versión de gcc, o se hace intencionalmente? Si es así, ¿por qué?
3 answers
Esta es la implementación desnuda de std::array
:
template<typename T, std::size_t N>
struct array {
T __array_impl[N];
};
Es una estructura aggregate cuyo único miembro de datos es una matriz tradicional, de modo que el interior {}
se utiliza para inicializar la matriz interna.
La elisión de llaves se permite en ciertos casos con inicialización agregada (pero generalmente no se recomienda), por lo que solo se puede usar una llave en este caso. Ver aquí: Vector C++ de matrices
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:47:15
De acuerdo con cppreference. Solo se requieren llaves dobles si se omite =
.
// construction uses aggregate initialization
std::array<int, 3> a1{ {1,2,3} }; // double-braces required
std::array<int, 3> a2 = {1, 2, 3}; // except after =
std::array<std::string, 2> a3 = { {std::string("a"), "b"} };
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-05 10:36:12
Llaves dobles requeridas en C++11, pero no desde C++14:
// construction uses aggregate initialization
std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
std::array<int, 3> a2 = {1, 2, 3}; // never required after =
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-28 03:58:48