In article <fffc30lj9mmrfv9fcjh9eefhr23hs2unco@4ax.com>,
tom_usenet <tom_usenet@hotmail.com> wrote:
[color=blue]
> On Fri, 20 Feb 2004 16:59:02 GMT, David Harmon <source@netcom.com>
> wrote:
>[color=green]
> >On Fri, 20 Feb 2004 15:57:51 GMT in comp.lang.c++, "Andrew Koenig"
> ><ark@acm.org> was alleged to have written:
> >[color=darkred]
> >>> x.swap(std::vector<int>());[/color]
> >
> >[color=darkred]
> >>Do it this way:
> >>
> >> std::vector<int>().swap(x);[/color]
> >
> >The fact that one of those would work and the other not just points out
> >the sillyness of the rule against binding non-const references to
> >temporaries.[/color]
>
> The rule isn't silly except in the exact type match case (of which
> vector::swap is an example).[/color]
I would argue that there are cases when even an exact type match case
should not bind a temporary to a non-const reference:
std::deque<int> stuff...
std::advance(stuff.begin(), stuff.size()/2);
I sure am glad this call to advance doesn't compile.
That being said, there are times when you *do* want to bind a temporary
to a non-const reference (and swap is certainly one of those times).
The best person to decide when to bind a temp to a ref is the author of
the function in question. And it would even be useful to overload two
functions based on whether or not they were binding to an rvalue or
lvalue:
void foo(const A& a); // bind to lvalues
void foo(A&& a); // bind to rvalues (proposed)
This is exactly what the move proposal is about
:
http://anubis.dkuug.dk/jtc1/sc22/wg2...2002/n1377.htm
I would love to see:
template <class T, class A>
void
vector<T, A>::swap(vector&& v);
which would then allow:
x.swap(std::vector<int>());
I also wouldn't mind seeing:
template <class T> void swap(T&, T&);
template <class T> void swap(T&&, T&);
template <class T> void swap(T&, T&&);
I.e. at most one of the arguments to swap can be a temporary.
But I recommend keeping std::advance just the way it is (at least with
respect to this issue) :-)
template <class InputIterator, class Distance>
void
advance(InputIterator& i, Distance n);
-Howard