There's structure HasArray
with template parameters typename T
and size_t N
.
template<typename T, size_t N>
struct HasArray {
// enable_if sizeof...(T) equals N
template<typename ... Types, typename std::enable_if<sizeof...(Types) == N, int>::type = 0>
explicit HasArray(Types ... s) : arr { s... } {}
protected:
std::array<T, N> arr;
};
I would like to initialize the member array arr
with the parameter-pack arguments of the constructor.
HasArray<uint32_t, 2> foo(7, 13);
But that yields a c++11-narrowing
warning in Clang.
error: non-constant-expression cannot be narrowed from type 'int' to 'std::__1::array<unsigned int, 2>::value_type' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]
I see no way to cast all s
's of type Types
to type T
. Is there one?
Edit thanks for all answers. I ended up using static_cast<>
on the packed parameters and SFINAE when not convertible:
template<typename T, size_t N>
struct HasArray {
// Use `static_cast` to construct `arr` with `s`
// Add `enable_if` all `Types` are `is_convertible`
template<typename ... Types,
typename std::enable_if<sizeof...(Types) == N, int>::type = 0,
typename std::enable_if<(std::is_convertible<Types, T>::value && ...), int>::type = 0>
explicit HasArray(Types ... s) : arr { static_cast<T>(s)... } {}
protected:
std::array<T, N> arr;
};
If you want to construct std::array
from any value convertible to T
, then the easiest solution would be just to add static_cast<T>(...)
in your constructor.
template<typename T, size_t N>
struct HasArray {
template<typename ... Types,
typename std::enable_if<sizeof...(Types) == N, int>::type = 0>
explicit HasArray(Types ... s) : arr {static_cast<T>(s)... } {}
protected:
std::array<T, N> arr;
};
It's also possible to "SFINAE out" the constructor in case such conversion is not possible, but In my opinion the default error message would be better in current simple case and you could add static_asserts with better message in constructor body.