在编译时零初始化算术 `value_type` 的 `std::array` 会导致缺少构造函数注释
Zero-initializing a `std::array` of arithmetic `value_type` at compile-time results in a missing constructor note
为了在编译时初始化算术类型 AT
的 std::array
,我这样做了:
#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);
}
我 godbolted it,在我看来它有效:
然而,当我用
编译它时
g++ -O3 -std=c++2a -Wall -Wpedantic -Wunused-parameter -I /usr/include main.cpp -o main
使用 g++ (GCC) 8.2.1 20181127
,我得到 "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;
为什么会有这张便条?我可以忽略它吗?如果是这样,如何摆脱它?
std::array
没有默认构造函数。您需要使用:
std::array<AT, DIM> result = {};
令我困惑的是为什么你认为你需要 zero_array
。如果你使用
std::array<double, 10> a = {};
你得到一个零初始化的对象。
为了在编译时初始化算术类型 AT
的 std::array
,我这样做了:
#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);
}
我 godbolted it,在我看来它有效:
然而,当我用
编译它时g++ -O3 -std=c++2a -Wall -Wpedantic -Wunused-parameter -I /usr/include main.cpp -o main
使用 g++ (GCC) 8.2.1 20181127
,我得到 "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;
为什么会有这张便条?我可以忽略它吗?如果是这样,如何摆脱它?
std::array
没有默认构造函数。您需要使用:
std::array<AT, DIM> result = {};
令我困惑的是为什么你认为你需要 zero_array
。如果你使用
std::array<double, 10> a = {};
你得到一个零初始化的对象。