Re: A Brief Introduction to Rvalue References
Posted: Mar 17, 2008 9:49 AM
> Thanks! Interestingly, the current working draft
> (N2521.pdf) doesn't have generic std::swap
> overloads, to support passing an rvalue reference
> argument. I guess they could have been added, as follows:
> template<class T> void swap(T&& a, T& b);
> template<class T> void swap(T& a, T&& b);
> Are those overloads left out intentionally?
Yes, they were intentionally left out. I was nervous about allowing statements such as:
int i = 0;
The motivation for allowing containers to swap with rvalues grew out of the desire to generalize the vector "swap trick":
vector<int>(v).swap(v); // C++03
v.swap(vector<int>(v)); // C++0X
swap(v, vector<int>(v)); // C++0X
I.e. just trying to make the swap trick a little easier to read and write.
Currently the draft generally contains 3 namespace scope swap signatures for library components (in general):
// pseudo code
The motivation for the three signatures is to prohibit clients from accidently swapping two rvalue vectors which is almost definitely a mistake. However there is currently some (quite understandable) feeling on the committee that this is overkill and we might want to replace the above three signatures with just this one:
// pseudo code
which would allow either argument to be lvalue or rvalue (in all 4 combinations). Neither solution has a performance or code size advantage over the other, and the safety enhancement of the 3-signature pack is quite minimal. So it is a close call in deciding the proper way to go, and I am comfortable with either choice.