|
Boost : |
Subject: Re: [boost] [move] interest: the pass-by-value and swap idiom, and explicit copy constructors
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2014-02-14 19:13:18
Hi,
Krzysztof Czainski wrote:
> Hello,
>
> I would like to ask about interest in an idea of mine. I described it in
> detail [1] and implemented it as part of my cz libraries [2].
>
> In short, the idea presents:
> - an alternative to implicitly copyable and movable types: making them
> explicitly copyable;
> - a protocol for explicitly stating in code, where a copy is to be made
> (boost::copy()), which compliments the protocol for explicitly stating in
> code, where a move is allowed (std::move() or boost::move());
> - a Boost.move extension for implementing movable types with use of the
> pass-by-value and swap idiom, backwards-compatible with C++98, which allows
> to choose copying policies (and eases changing between these policies
> during maintenance):
> - movable but not copyable,
> - movable and explicitly-copyable,
> - movable and copyable;
> - an example, showing how simply a CloneablePtr can thereby be implemented.
>
> This extension to Boost.move is hosted and developed as part of my cz
> libraries [2], but it isn't tied to them at all. It only consists of two
> headers. I extracted the two headers together with samples and docs, and
> they are available as a zip archive [3].
>
> I would be most excited if there was interest in adding this functionality
> to Boost.move, but in any case, all comments are much appreciated.
>
> Regards,
> Kris
>
> [1] http://kristoczaj.bitbucket.org/cz/copy_move_types.html
> [2] http://kristoczaj.bitbucket.org/cz/index.html
> [3] http://bitbucket.org/kristoczaj/cz/downloads/move-0.7.1.zip
>
Your proposal reminds me Scott Meyers' lecture
(http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler):
"std::copy() doesn't copy and std::forward() doesn't forward"
your boost::copy() is different because it indeed copies something ;).
I have a few questions.
1. I think that the results of the first example with Matrices
(implicitly declared ctors and assignments) are wrong. What compiler do
you use? I've tested it in GCC 4.7 and Clang 3.2 with -std=c++11.
a) can't move from a const object, this is a copy
Matrix const a;
Matrix c = boost::move(a);
b) here I have a copy as well, probably because Matrix has a non-static
data member without a move assignment operator and that is is not
trivially copyable
c = boost::move(b);
after declaring "proper" move assignment in the member class (instead of
copy and swap) or using copy and swap + move ctor in Matrix everything
works as expected.
Assuming that my understanding is correct, with copy and swap the move
assignment operator can't be implicitly declared. So you'll be forced to
declare it manually even in C++11. AFAIK the currently used move
emulation doesn't have this limitation. Or am I missing something?
2. About the example with Matrices addition and moves, the one with e.g.
this line:
Matrix e = boost::move(b) + c + d;
Is it just an example of using boost::move() needed by the next
complimentary example of using boost::copy(), showing that you could do
something like this? Or do you think that this is the right way of
implementing e.g. the addition to avoid temporary copies? I'm asking
because it's a little too clever on my taste. IMHO the way with the 4
overloads is more intuitive. On some review it could certainly increase
the value of WTF/minute ;) (http://www.osnews.com/story/19266/WTFs_m).
Regards,
Adam
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk