I mean why does std::make_tuple
exist? I know that there are situations where the function reduces the amount of characters you have to type because you can avoid template parameters. But is it the only reason? What makes std::tuple
special that the function exists while other class templates haven't such function? Is it only because you may use std::tuple
more often in such situations?
Here are two examples where std::make_tuple
reduces the amount of characters:
// Avoiding template parameters in definition of variable.
// Consider that template parameters can be very long sometimes.
std::tuple<int, double> t(0, 0.0); // without std::make_tuple
auto t = std::make_tuple(0, 0.0); // with std::make_tuple
// Avoiding template parameters at construction.
f(std::tuple<int, double>(0, 0.0)); // without std::make_tuple
f(std::make_tuple(0, 0.0)); // with std::make_tuple
But like written above, you don't have a function like this for many other class templates.
std::make_pair
. I think a rule of thumb might be that they exist if you might want to construct these objects simply as part of another expression, to call a function, etc. – Quilletmake_*
function for? We are already getting make_shared, make_unique, etc. – Periosteumstd::move
. It would get very ugly. Consider:auto z = foo(std::make_tuple(x,y));
versus{ auto j = std::tuple<X,Y>(x,y); auto z = foo(std::move(j)); }
Oops,z
is now out of scope. But I needj
out of scope. Ack. – Semifinaltie(i, ignore, s) = make_tuple(42, 3.14, "C++");
– Importunatemake_tuple([&](){ f(x); }, std::mem_fn(&X::foo))
. – Pisistratusmake_tuple
fits into the family ofmake_tuple
,tie
andforward_as_tuple
, which respectively give you prvalues, lvalues and forwarded-values. – Pisistratusstd::make_pair
yet. The functionstd::make_shared
has a optimisation and I thoughtstd::make_unique
in C++14 would be added to be consistent withmake_shared
. Mostly I have worked with types likestd::vector
yet and they haven't such functions. I think this mean there is no benefit beside less code. – Hidiemake_tuple
. I don't think the answer is obvious and so this looks like a good question to me. – Romanticist