To initialize an std::array
of an arithmetic type AT
at compile-time, I did this:
#include <array>
#include <iostream>
template<typename AT, auto DIM, auto N = 0>
constexpr void assign(std::array<AT, DIM>& arr, AT value)
{
arr[N] = value;
if constexpr (N < std::size(arr) - 1)
assign<AT, DIM, N + 1>(arr, value);
}
template<typename AT, auto DIM>
constexpr std::array<AT, DIM> zero_array()
{
std::array<AT, DIM> result;
assign(result, AT{0});
return result;
}
template<typename Container>
void print(Container&& cont)
{
for (const auto& el : cont)
std::cout << el << " ";
std::cout << std::endl;
}
int main()
{
auto zero10 = zero_array<double, 10>();
print(zero10);
}
I godbolted it, and it seems to me it worked:
However, when I compile it with
g++ -O3 -std=c++2a -Wall -Wpedantic -Wunused-parameter -I /usr/include main.cpp -o main
using g++ (GCC) 8.2.1 20181127
, I get a "note"
In file included from main.cpp:1:
main.cpp: In instantiation of ‘constexpr std::array<AT, DIM> zero_array() [with AT = double; auto DIM = 10]’:
main.cpp:30:42: required from here
/usr/include/c++/8.2.1/array:94:12: note: ‘struct std::array<double, 10>’ has no user-provided default constructor
struct array
^~~~~
/usr/include/c++/8.2.1/array:110:56: note: and the implicitly-defined constructor does not initialize ‘double std::array<double, 10>::_M_elems [10]’
typename _AT_Type::_Type _M_elems;
Why is this note there? Can I ignore it? If so, how to get rid of it?
std::array
does not have a default constructor. You need to use:
std::array<AT, DIM> result = {};
What's puzzling to me is why you think you need zero_array
at all. If you use
std::array<double, 10> a = {};
you get a zero-initialized object.