Can you just add a piecewise constructor for your types? If so, you can create a horrible macro that unpacks and delegates a tuple:
#define CONSTRUCT_FROM_TUPLE(CLS) \
template <class... Ts> \
CLS(std::tuple<Ts...> const& tup) \
: CLS(tup, std::index_sequence_for<Ts...>{}) \
{ } \
\
template <class Tuple, size_t... Is> \
CLS(Tuple const& tup, std::index_sequence<Is...> ) \
: CLS(std::get<Is>(tup)...) \
{ }
And just add it to your types:
struct A {
A(bool, int ) { }
A(const A& ) = delete;
CONSTRUCT_FROM_TUPLE(A)
};
struct B {
B(char, double ) { }
B(const B& ) = delete;
CONSTRUCT_FROM_TUPLE(B)
};
And pass in tuples:
std::tuple<A, B> tup(
std::forward_as_tuple(true, 42),
std::forward_as_tuple('x', 3.14));
Pre-C++11, I don't know that this is possible - you don't have delegating constructors at all. You'd have to either:
- Write your own
tuple
-like class that accepts tuples in its constructor
- Add tuple constructors to your types that explicitly initialize the same thing as the non-tuple versions did
- Have a tuple of types that are single-argument constructible, like
boost::tuple<boost::scoped_ptr<A>, boost::scoped_ptr<B>>(new A(...), new B(...))
(1) is a lot of work, (2) is code duplication and error prone, and (3) involves now having to do allocation all of a sudden.