|
Boost : |
Subject: Re: [boost] rvalue ref best practices?
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2012-06-12 17:31:58
El 12/06/2012 16:51, Mathias Gaunard escribió:
> On 06/11/2012 09:21 PM, Ion Gaztañaga wrote:
>> If it's going to be assigned, I want to reuse a's
>> resources
>
> And that's the problem. Reusing those resources is destructive, so if
> your function fails before it returns the new value, the old value is lost.
>
> It is not natural for something written as foo = bar(); to change foo if
> bar fails.
Thanks for pointing this, I was completely missing it. If the assignment
syntax is confusing then the only choice not to surprise the programmer
is to use reference passing syntax. Since we all agree that
modify_bar(a);
means that a could be left in an indeterminate state. Then the question
is how can we make this optimal also with non-constructed objects.
1) Some NRVO, some modified
B b;
b =...;
D d;
d =...;
//NRVO for a&c, mutable reference for b&d
[A a, C c] -> modify_or_construct(a, b, c, d);
2) All modified
//a,b,c,d modified
modify_or_construct(a, b, c, d);
3) All constructed via NRVO to obtain strong exception guarantee (assume
A, B, C, D's move constructors don't throw)
//Might throw
[A a2, B b2, C c2, D d2] -> modify_or_construct(a, b, c, d);
//No-throw guarantee
a = std::move(a2);
b = std::move(b2);
c = std::move(c2);
d = std::move(d2);
Best,
Ion
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk