Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Does C++ have "argument value optimization"?

36 views
Skip to first unread message
Message has been deleted

Bo Persson

unread,
May 5, 2015, 1:52:14 PM5/5/15
to
On 2015-05-05 17:17, Stefan Ram wrote:
> When a value is returned, its expression is automatically
> considered to be an rvalue and thus is /moved/ from although
> the literal meaning in early C++ once might have been that
> its value is being copied. This is known as "return value
> optimization" ("RVO").
>
> Does the same apply to arguments in the following sense?
> When a function f has a plain ::std::string parameter and
> is called
>
> f( "a"s + "b"s )
>
> , then »"a"s + "b"s« is a temporary, so it does not have to
> be copied from but can be moved from. Is this guaranteed to
> happen? Is there a name for this "optimization" (akin to "RVO")?
>
> So, if it is guaranteed that it will happen for
>
> f( ::std::string const s )

std::string has a move constructor, so yes the temporary could be moved
into s. Or maybe RVO from operator+ would make the move unnecessary?


>
> , then one does not need to write
>
> f( ::std::string && s )
>
> . Then, what are the use-cases for »f( ::std::string && s )«?
>

If f might want to pass the reference on to some other function, perhaps

g(std::forward<std::string>(s));



Bo Persson

Message has been deleted

Chris Vine

unread,
May 5, 2015, 8:20:23 PM5/5/15
to
That is not how std::forward is intended to be used: std::forward is
expected to be given a deduced type as its template parameter. The
expression 'std::forward<std::string>(s)' would just convert the lvalue
's' to a rvalue - in other words, it would be identical in effect to
'std::move(s)', which is a shorter and more comprehensible way of
writing it, if that is what is actually intended.

With respect to the OP's question about whether binding a temporary to a
function argument comprising a value std::string parameter will prompt
copy elision similar to RVO, the answer is no. Copy elision of objects
with non-trivial copy constructors and/or non-trivial destructors is not
permitted in that case. RVO only applies to function return values.

Chris

0 new messages