std::pair of references

Viewed 28799

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.

8 Answers

Post c++14, you can do:

int a, b;
auto const p(std::make_pair(std::ref(a), std::ref(b)));

Using std::cref() is also possible.

Related