c++ - How to create std::array with initialization list without providing size directly -
c++ - How to create std::array with initialization list without providing size directly -
this question has reply here:
how emulate c array initialization “int arr[] = { e1, e2, e3, … }” behaviour std::array? 8 answershow can create a3 compile?
int main() { int a1[] = { 1, 2, 3 }; std::array<int, 3> a2 = { 1, 2, 3 }; std::array<int> a3 = { 1, 2, 3 }; } it's inconvenient, , brittle, hard-code size of array when using initialization list, long ones. there work around? hope otherwise i'm disappointed because hate c arrays , std::array supposed replacement.
there no way without rolling own make_array, there proposal n3824: make_array has next scope:
lwg 851 intended provide replacement syntax to
array<t, n> = { e1, e2, ... }; , following
auto = make_array(42u, 3.14); is well-formed (with additional static_casts applied inside) because
array<double, 2> = { 42u, 3.14 }; is well-formed.
this paper intends provide set of std::array creation interfaces comprehensive both tuple’s point of view , array’s point of view, narrowing naturally banned. see more details driven direction in design decisions.
it includes sample implementation, rather long copying here impractical konrad rudolph has simplified version here consistent sample implementation above:
template <typename... t> constexpr auto make_array(t&&... values) -> std::array< typename std::decay< typename std::common_type<t...>::type>::type, sizeof...(t)> { homecoming std::array< typename std::decay< typename std::common_type<t...>::type>::type, sizeof...(t)>{std::forward<t>(values)...}; } c++ initialization stdarray
Comments
Post a Comment