Is the following use of p
within main
safe? I believe binding the temporary produced by mk_pair
has its lifetime extended to that of p
, but how about the temporary objects created by Wrap{1}
and Wrap{2}
?
struct Wrap { int &&x; };
struct Pair { Wrap &&w1, &&w2; };
Pair mk_pair(Wrap &&w1, Wrap &&w2) { return Pair{std::move(w1),std::move(w2)}; }
int main(int argc, char *argv[])
{
Pair &&p = mk_pair(Wrap{1},Wrap{2});
std::cout << p.w1.x << ' ' << p.w2.x << '\n';
return 0;
}