I need a constexpr wrapper over a C array or a std::array
with some extra constructors (similar to std::vector
constructors):
template<class T, int N>
struct wrapper {
T data[N];
constexpr wrapper(int s); // a
constexpr wrapper(int j, int k); // b
constexpr wrapper(...values...) // c
: data(...values...) {}
};
I am trying to get the same behavior as the std::vector
constructors, that is:
constexpr wrapper<T,2> w(1); // calls a
constexpr wrapper<T,2> w(1,2); // calls b
constexpr wrapper<T,2> w{1}; // calls c
constexpr wrapper<T,2> w{1,2}; // calls c
Note 1: a perfect forwarding constructor:
template<class... Args>
constexpr wrapper(T&& t, Args&&... args)
: data(std::forward<T>(t), std::forward<Args>(args)...) {}
would win over the other constructors.
Note 2: T[N]/std::array<T,N>
have no std::initializer_list<T>
constructors so the following doesn't work either:
constexpr wrapper(std::initializer_list<T> v) : data(std::move(v)) {}
Note 3: if the values are not assigned in the constructor initialization list the wrapper type won't work in constant expressions.