If, I might be so bold as to expand on the Benjamin's answer. Conceptually one does not need always to be explicit about the result type.
template<
typename ... T,
typename CT = std::common_type_t< T... >
>
constexpr auto make_array(T&& ... t)
-> std::array< CT , sizeof...(T)>
{
return { { static_cast<CT>( std::forward<T>(t) ) ...} };
}
Slightly simpler usage
constexpr auto std_arr = make_array(-5.0, 0.0, 5.0, 10.0);
The issue here might be, we are not exactly certain, what type of the std::array
we will get. Provided we do care about it.
const auto std_arr = make_array(-5.0, 0, 5.0, 10.0, 42.13f );
Over "here", using MSVC, the latest, I get std array of doubles. If one's bed time reading is ISO C++ standard doc, the one might be sure what type will come out of std::common_type
from the tool chain in use.
For the rest of us, literal suffixes will help
constexpr auto sizes = make_array( 1UL, 2UL, 3UL );
But why stop with numbers? One can collect quickly, into an array, instances from the same hierarchy. Not caring about the result type.
{
const auto exceptions = make_array(
std::exception{"SE"},
std::runtime_error{"RE"},
std::logic_error{"LE"} );
}
A bit useless but somewhat weird and wonderful. One thing thou remember: this is compile time situation. Thus, I might prefer:
constexpr auto sizes = std::array{ 1UL, 2UL, 3UL } ;
To answer the OP's question directly.
There is also C++20 std::to_array
. For the exact result type and for a bit more comfortable usage:
constexpr auto sizes = std::to_array<size_t>({ 0, 1, 3 });
Enjoy ...