For two arbitrary objects of type T
and U
that are composed into a class like so
template <class T, class U>
struct Comp
{
T t_m;
U u_m;
};
what would be the optimum (in terms of minimizing copy operations) way to construct them out of (available) temporaries ?
I have considered "moving" them into my class
Comp(T&& t, U&& u)
: t_m(std::move(t))
, u_m(std::move(u))
{ }
but I don't know how well their move constructors behave or if they have any whatsoever.
Since it seems that my class can be an aggregate I was wondering whether removing the constructor and allowing aggregate initialization would be a better solution, i.e. writing code like this:
Comp{ get_temporary_T(), get_temporary_U() };
or if there's an advantage in using direct initialization.
PS
In place construction (using placement new operator) is not the solution I'm looking for.
PS 2
I imagine std::tuple
uses such an optimum method since make_tuple
is shown to utilize temporaries by calling the tuple constructor :
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
could alternatively someone elaborate on how this is done ?