std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.
For pair, use the not equal (!=) operator: The!= operator compares the first values of two sets, say pair1 and pair2, i.e. if pair1 and pair2 are given, the!= operator compares the first values of those two pairs.
std::ref. Constructs an object of the appropriate reference_wrapper type to hold a reference to elem . If the argument is itself a reference_wrapper (2), it creates a copy of x instead. The function calls the proper reference_wrapper constructor.
In C++11 you can use std::pair<
std::reference_wrapper
<T>, std::reference_wrapper<U>>
and the objects of that type will behave exactly as you want.
No, you cannot do this reliably in C++03, because the constructor of pair
takes references to T
, and creating a reference to a reference is not legal in C++03.
Notice that I said "reliably". Some common compilers still in use (for GCC, I tested GCC4.1, @Charles reported GCC4.4.4) do not allow forming a reference to a reference, but more recently do allow it as they implement reference collapsing (T&
is T
if T
is a reference type). If your code uses such things, you cannot rely on it to work on other compilers until you try it and see.
It sounds like you want to use boost::tuple<>
int a, b;
// on the fly
boost::tie(a, b) = std::make_pair(1, 2);
// as variable
boost::tuple<int&, int&> t = boost::tie(a, b);
t.get<0>() = 1;
t.get<1>() = 2;
I think it would be legal to have a std::pair
housing references. std::map
uses std::pair
with a const
type, after all, which can't be assigned to either.
I'd like to have a
pair<T&, U&>
and be able to assign to it another pair
Assignment won't work, since you cannot reset references. You can, however, copy-initialize such objects.
You are right. You can create a pair of references, but you can't use operator =
anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With