Given some classes with parameterized constructors, such as:
class A
{
public:
A(bool b, int i) { /*...*/ }
private:
A(const A&) {}
};
class B
{
public:
B(char c, double d) { /* ... */ }
private:
B(const B&) {}
};
How to properly initialize a tuple of such classes?
boost::tuple<A,B> tup( /* ??? */ );
Not using copy constructor of A or B, and, if possible, not using move-constructor either. C++03 solution preferred, if possible.
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:
tuple
-like class that accepts tuples in its constructor 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.