Consider the following mypair class (I'm not sure if this is the best way to do something, but it works):
#include <iostream> struct A { A() {} A(const A&) { std::cout << "Copy" << std::endl; } A(A&&) { std::cout << "Move" << std::endl; } std::string s; }; template <class T0, class T1> struct mypair { T0 x0; T1 x1; }; template <class T0, class T1, int N = -1> struct get_class {}; template<class T0, class T1> struct get_class<T0, T1, 0> { static T0& get_func(mypair<T0, T1>& x) { return x.x0; } static const T0& get_func(const mypair<T0, T1>& x) { return x.x0; } static T0&& get_func(mypair<T0, T1>&& x) { return std::move(x.x0); } }; template<class T0, class T1> struct get_class<T0, T1, 1> { static T1& get_func(mypair<T0, T1>& x) { return x.x1; } static const T1& get_func(const mypair<T0, T1>& x) { return x.x1; } static T1&& get_func(mypair<T0, T1>&& x) { return std::move(x.x1); } }; template <int N, class T0, class T1> auto get(mypair<T0, T1>& x) -> decltype(get_class<T0,T1,N>::get_func(x)) { return get_class<T0,T1,N>::get_func(x); } #define MAKE_PAIR(x1, x2) mypair<decltype(x1), decltype(x2)>{x1, x2} int main() { auto x = MAKE_PAIR(A(), A()); get<0>(x); get<1>(x); }
(perfect link)
The answer when it is aggregate initialization valid in C ++ 11 says that we can eliminate copies / movements by performing aggregate initialization.
Therefore, we can build mypair with MAKE_PAIR without having to do any movement or copy.
I would like to generalize MAKE_PAIR to MAKE_TUPLE , i.e. accept any number of arguments.
Requirements (as is the case with MAKE_PAIR ):
(1) Types are displayed.
(2) Movements / copies are removed during construction from temporary (i.e. the construction takes place on site).
Existing library solutions (e.g. Boost) would be good, although I would prefer something that accepts rvalue references. Or just the code here is great, or a combination of the two.
If at all possible, I would like it to optimize empty elements, still causing calls to move / copy calls, but I get the feeling that you are asking too much.