I’d like to be able to do this:
template<typename Mix>
struct A {
A(int i) { }
};
template<typename Mix>
struct B {
B() { }
B(const char*) { }
};
template<template<typename> class... Mixins>
struct Mix : Mixins<Mix<Mixins...>>... {
// This works, but forces constructors to take tuples
template<typename... Packs>
Mix(Packs... packs) : Packs::Type(packs.constructorArgs)... { }
};
template<template<typename> class MixinType, typename... Args>
struct ArgPack {
typedef MixinType Type; // pretend this is actually a template alias
tuple<Args...> constructorArgs;
ArgPack(Args... args) : constructorArgs(args...) { }
}
template<typename... Args>
ArgPack<A, Args...> A_(Args... args) {
return ArgPack<A, Args...>(args...);
}
template<typename... Args>
ArgPack<B, Args...> B_(Args... args) {
return ArgPack<B, Args...>(args...);
}
Mix<A, B> m(); // error, A has no default constructor
Mix<A, B> n(A_(1)); // A(int), B()
Mix<A, B> n(A_(1), B_("hello"); // A(int), B(const char*)
How do I fill in /* mysterious code here */ to do what I want, to provide a nice interface for calling some set of constructors of mixins? I have a solution that works by making all non-null constructs actually take a tuple of args, and then overloading figures out which one to call, but I would like to avoid constraining mixin authors by making them write a constructor A(tuple), instead of just A(int, int).
Thanks!
I think I understand what you want.
std::pairhas a similar feature:As you can see this has a lot of benefits: exactly one
TandUconstruction each takes place, neitherTandUare required to be e.g. MoveConstructible, and this only costs the constructions of two shallow tuples. This also does perfect forwarding. As a warning though, this is considerably harder to implement without inheriting constructors, and I will use that feature to demonstrate a possible implementation of a piecewise-constructor and then attempt to make a variadic version of it.But first, a neat utility that always come in handy when variadic packs and tuples are involved:
So now if we have
using tuple_type = std::tuple<int, long, double, double>;thenmake_indices<tuple_type>()yields a value of typeindices<0, 1, 2, 3>.First, a non-variadic case of piecewise-construction:
Let’s try plugging that with your mixin:
As you can see I’ve gone one level higher up with those tuple of tuples and tuple of indices. The reason for that is that I can’t express and match a type such as
std::tuple<indices<Indices...>...>(what’s the relevant pack declared as?int...... Indices?) and even if I did pack expansion isn’t designed to deal with multi-level pack expansion too much. You may have guessed it by now but packing it all in a tuple bundled with its indices is my modus operandi when it comes to solving this kind of things… This does have the drawback however that construction is not in place anymore and theMixins<...>are now required to be MoveConstructible.I’d recommend adding a default constructor, too (i.e.
Mix() = default;) because usingMix<A, B> m(std::piecewise_construct, std::forward_as_tuple(), std::forward_as_tuple());looks silly. Note that such a defaulted declaration would yield no default constructor if any of theMixin<...>is not DefaultConstructible.The code has been tested with a snapshot of GCC 4.7 and works verbatim except for that
sizeof...(Mixins)mishap.