This is an interesting problem I'm thinking about a time ago. Given a struct
with an underlying aggregate:
#include <array>
template <typename T, size_t N>
struct A
{
constexpr A() = default;
template <typename ... Ts>
constexpr A(const T& value, const Ts& ... values); // magic
std::array<T, N> arr; // aggregate
};
How would you implement variadic template constructor A(const T& value, const Ts& ... values)
to
- accept both values of type
T
and anotherA<T, N>
- properly initialize the underlying aggregate based on the values represented by the passed arguments
- respect the capacity of the aggregate
- support C++14 constexpr rules and do not introduce any runtime overhead
Satisfying the above requirements, it is possible to do the following:
int main()
{
A<int, 3> x(1, 2, 3);
A<int, 3> y(1, 2);
A<int, 6> a(x, 1, 2, 3);
A<int, 6> b(1, x, 2, 3);
A<int, 6> c(1, 2, x, 3);
A<int, 6> d(1, 2, 3, x);
A<int, 6> e(x, x);
A<int, 6> f(y, y, y);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire