Boost logo

Boost :

From: Andrea Torsello (torsello_at_[hidden])
Date: 2004-04-15 13:34:19


Hi, I have been investigating move semantics and think that I came up with a
solution that is strongly influenced by both David Abrahams' move approach
and Andrei Alexandrescu's mojo, but solves some of the problems I have
encountered with them.
The basic idea is to use the explicit keyword to force the compiler to use
the X(X const &) constructor only in two cases.
1) When explicitly copying a const object i.e.
    X const x;
    X y(x); //use explicit copy constructor
2) When passing by const ref
    void f(X const &);
    X makeX();
    void foo()
    {
        f(makeX()); //use explicit copy constructor
    }
In all other cases the other constructor take precedence.
Note that the template trick David Abrahams uses in his move approach would
lead to an error on a conforming compiler when confronted with case #2
(unless more recent versions corrected this).

I chose to keep the temporary and constant subtypes from mojo because they
allow me to discriminate between const values, non-const rvalues and
non-const rvalues.
The lack of this capability was the major problem I had with David Abrahams'
move.
My use-case was a vector-like container that increases but never decreases
the size of its backing store.
In order to provide the never decreasing backing-store semantic, operator =
moves the backing store from a temporary only if the new data does not fit
in the existing space.
Hence, operator = must be able to discriminate between temporaries and
non-temporaries.

My solution fullfills almost all the requirements I had:
1) passing by value moves non-const rvalues and copies everithing else.
2) its is transparent to the user of the class.
    Alexandrescu's Mojo fails this because of the special return type, while
Abrahams' move fails it because of the pass-by-const-ref problem described
above.
3) it allows to discriminate between constants, non-const lvalues, and
non-const rvalues.
     Alexandrescu's Mojo allows it, while Abrahams' move does not.
4) Provides move semantics to templated classes with (almost) no odd
side-effect.
    The only side-effect is that template type deduction does not work with
temporary discrimination. Hence in the sample code the template type of the
"discriminate" function must be specified explicitly.

please find attached a sample file illustrating my move approach.
I tested the code with gcc 3.3.1 and successfully compiled it with comeau
online.
What do you think?

Andrea Torsello

P.S. please cc me as I am not subscribed and I check the mailinglist
archives sporadically

begin 666 move_test.cc
M(VEN8VQU9&4\:6]S=')E86T^#0H-"@T*=7-I;F<@<W1D.CIC;W5T.PT*=7-I
M;F<@<W1D.CIE;F1L.PT*#0IN86UE<W!A8V4@;6]V90T*>PT*"71E;7!L871E
M(#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"F=E=%DH;6%K95D\:6YT/B_at_I
M*3L@+R]P87-S+6)Y+79A;'5E(&YO;BUC;VYS="!T96UP;W)A<GD-"F=E=%DH
M;6%K95EC;VYS=#QI;G0^*"DI.R O+W!A<W,M8GDM=F%L=64@;F]N+6-O;G-T
M('1E;7!O<F%R>0T*#0ID:7-C<FEM:6YA=&4\:6YT/BAY,2D[("\O;F]N+6-O
M;G-T(&QV86QU90T*9&ES8W)I;6EN871E/&EN=#XH>3(I.R O+V-O;G-T(&QV
M86QU90T*9&ES8W)I;6EN871E/&EN=#XH;6%K95D\:6YT/B_at_I*3L@+R]N;VXM
M8V]N<W0@=&5M<&]R87)Y#0ID:7-C<FEM:6YA=&4\:6YT/BAM86ME66-O;G-T
M/&EN=#XH*2D[("\O8V]N<W0@=&5M<&]R87)Y#0H-"@T*<F5T=7)N(# [#0I]
`
end


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk