Re: What C++0x features are relevant to you?
On Feb 24, 7:24 am, SG <s.gesem...@gmail.com> wrote:
On 24 Feb., 01:18, "Hak...@gmail.com" <hak...@gmail.com> wrote:
On Feb 23, 3:00 pm, SG <s.gesem...@gmail.com> wrote:
On 23 Feb., 03:51, "Hak...@gmail.com" <hak...@gmail.com> wrote:
[...]
But rvalue references would not help you in such cases (unless the
array's elements can be moved much faster than copied). Rvalue
references are great in case "logical members" are heap-allocated and
only referenced through a pointer member (see std::vector, for
example).
The way i use rvalue references is in the operator+ function:
// Pseudo code
template< T1, T2, S >
Vector<T2,S>&& operator+( const Vector<T1,S>& a, Vector<T2,S>&& b )
{
b += a;
return std::move(b);
}
Rather than creating a vector with a+b, then another with (a+b)+c, it
should create a temporary with a+b, then add c onto (a+b), then copy
that into d.
You mean a+(b+c), right? Only the 2nd parameter of this operator+
function is an rvalue reference.
Implicitly, there's a function where the rvalue ref is on the other
side. Sorry about that; i considered writing both, but thought one was
enough.
Anyhow, I'd consider it unsafe and it
is likely not worth the hassle of writing this overload. I would guess
that this doesn't safe any significant amount of time (Have you
measured it?).
No. I wrote that it is of interest to me, and i haven't measured
anything yet, but without it being interesting to me, and without
writing the function, i couldn't find out. Besides, i didn't start
this thread to talk about my usage, but my and other's interest, and
using rvalue references for this kind of operation was introduced to
me by the VC++ blog; i just happen to find the concept cool.
It's unsafe because it allows you to write things like
auto const& foo = a + (b + c);
The current draft N3035 still contains some operator+ functions for
strings which return rvalue references. But my understanding is that
this is going to be replaced. See LWG issue #1138:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3018.html#1138
Interesting you note that! What introduced me to the idea of using
rvalue references for this purpose is reading about just this use of
them on the VC++ blog. I guess it makes sense; even if i think using
auto with any kind of zeal is bad, reserving it for cases where there
is much more ambiguity than here, i can see how that creates a more
fool-proof program... Thank you for bringing my attention to that!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]