std::pair of references

Alexandre C. picture Alexandre C. · Sep 22, 2010 · Viewed 22.6k times · Source

Is it valid to have a std::pair of references ? In particular, are there issues with the assignment operator ? According to this link, there seems to be no special treatment with operator=, so default assignement operator will not be able to be generated.

I'd like to have a pair<T&, U&> and be able to assign to it another pair (of values or references) and have the pointed-to objects modified.

Answer

user283145 picture user283145 · Aug 27, 2013

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.