I\'m writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don\'t think I can return a reference from oper
On rare occasions it may be desirable to get an lvalue reference to temporary. This easily achieved with a cast opposite to std::move
:
template
T & stay(T && t) { return t; }
Usage:
std::swap(stay(foo()), stay(bar()));
As you already said, if you can't change the call site, your best option may be to write your own reference wrapper and use ADL for that:
namespace detail
{
struct IntRefPair
{
int & a, & b;
IntRefPair(int & x, int & y) : a(x), b(y) {}
};
void swap(IntRefPair && lhs, IntRefPair && rhs)
{
std::swap(lhs.a, rhs.a);
std::swap(lhs.b, rhs.b);
}
}
// ...
IntRefPair operator*() { return IntRefPair(v1[5], v2[5]); } }