Boost logo

Boost :

Subject: Re: [boost] Movable but not copyable bug?
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2014-08-25 17:14:07


El 25/08/2014 18:09, Mostafa escribió:
> On Mon, 25 Aug 2014 03:23:59 -0700, Ion Gaztañaga <igaztanaga_at_[hidden]>
> wrote:
>
>> El 25/08/2014 2:29, Mostafa escribió:
>>> I had posted this on the user's list but I think this should be brought
>>> to the attention of the developers. The following code contains a
>>> templated copy constructible Boost.Move enabled class (Bar) with a
>>> single non-copy constructible Boost.Enabled member (Foo). If Bar's copy
>>> constructor is not explicitly defined then the code compiles with both
>>> g++ 4.8.2 and VS2005, else if it's explicitly defined then it fails to
>>> compile on g++ 4.8.2 I think this is due to the fact that g++ 4.8.2
>>> erroneously instantiates Bar's default copy constructor in the latter
>>> case. Is this a Boost.Move bug or a gcc bug?
>>
>> The copy constructor shall not be defined.
>
> ???

If you declare a class as BOOST_MOVABLE_BUT_NOT_COPYABLE(Foo) you must
not declare the assignment operator. Follow the steps explained here:

http://www.boost.org/doc/libs/1_56_0/doc/html/move/movable_only_classes.html

> Here's the updated code with error messages. Note, that on g++ 4.8.2 if
> (1) is commented out then it compiles fine but if uncommented then the
> error messages that follow the code are produced. In the latter case,
> the error messages are due to (2). Note also that (1) is the copy
> constructor for the movable *and* copyable type Bar which has a single
> member of type Foo that is movable but *not* copyable. Also note that
> Bar is a template class.

If Foo is not copyable, then you have a logic error in your code:

     Bar(Bar const & rhs)
     : f(rhs.f) <---- Tries to COPY Foo
     {}

If this copy constructor is instantiated, and in many compilers

   Bar<Foo> b(( Bar<Foo>() ));

requires the copy constructor, then you get a compilation error. Just
try this equivalent (but more inefficient) code:

   Bar<Foo> a;
   Bar<Foo> b(a);

Then the copy constructor of Bar<Foo> is called and tries to COPY Foo.
As Foo is noncopyable, a compilation error is triggered.

Maybe you should design your code in C++11 and port it to Boost.Move,
you might get better compiler help.

Best,

Ion


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