C++11: Correct std::array initialization?
C++ArraysC++11InitializationC++14C++ Problem Overview
If I initialize a std::array as follows, the compiler gives me a warning about missing braces
std::array<int, 4> a = {1, 2, 3, 4};
This fixes the problem:
std::array<int, 4> a = {{1, 2, 3, 4}};
This is the warning message:
missing braces around initializer for 'std::array<int, 4u>::value_type [4] {aka int [4]}' [-Wmissing-braces]
Is this just a bug in my version of gcc, or is it done intentionally? If so, why?
C++ Solutions
Solution 1 - C++
This is the bare implementation of std::array
:
template<typename T, std::size_t N>
struct array {
T __array_impl[N];
};
It's an aggregate struct whose only data member is a traditional array, such that the inner {}
is used to initialize the inner array.
Brace elision is allowed in certain cases with aggregate initialization (but usually not recommended) and so only one brace can be used in this case. See here: https://stackoverflow.com/questions/6041459/c-vector-of-arrays
Solution 2 - C++
According to cppreference. Double braces are required only if =
is omitted.
// 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"} };
Solution 3 - C++
C++17 std::array
class template argument deduction (CTAD)
This new C++17 feature is used by the standard library and now allows us to omit the template types as well so that the following works:
main.cpp
#include <array>
int main() {
std::array a{1, 2, 3};
}
instead of std::array<int, 3> a{1, 2, 3};
Tested with:
g++ -ggdb3 -O0 -std=c++17 -Wall -Wextra -pedantic -o main.out main.cpp
If we set -std=c++14
instead for example, it fails to compile with:
error: missing template arguments before ‘a’
See also: https://stackoverflow.com/questions/39933567/deduce-stdarray-size
Tested on Ubuntu 18.04, GCC 7.5.0.
Solution 4 - C++
Double-braces required in C++11 prior to the CWG 1270 (not needed in C++11 after the revision and in C++14 and beyond):
// construction uses aggregate initialization
std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision
// (not needed in C++11 after the revision and in C++14 and beyond)
std::array<int, 3> a2 = {1, 2, 3}; // never required after =