Boost logo

Boost :

Subject: Re: [boost] [move] auto-generated operator=
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-09-03 03:16:34


Jeffrey Hellrung escribió:
> Hi (again) Ion,
>
> I recently ran into the following issue. I defined a
> copyable-and-movable class X, and class Y has a member variable of type
> X and no other member variables. It thus (apparently) happens that Y's
> auto-generated operator= takes a non-const reference to Y, which I would
> guess is generally not desired, or at least an operator= overload taking
> a const reference to Y should be available.

Yes, I recently got the same issue in Interprocess. The standard says
that the auto-generated assignment takes non-const argument if any of
bases or members' assignment is non-const. Since we generated the
non-const version with Boost.Move, assignment operators are not
generated with const arguments. In this case the only solution is to
avoid compiler-generated assignment operator and write an explicit one.
Copy constructor does not suffer this because we rely on RVO to avoid
copies.

The fact that you need to add *both* const and non-const is strange, in
my case, just definining the const version in the higher-level class was
enough.

> The only point I want to make is that it seems worth mentioning this
> subtlety (at least it was a subtlety to me) in the move documentation
> somewhere, and perhaps how to most easily and portably address this (I'm
> not sure what that would be), as I would expect it to be a not uncommon
> situation.

Yes, I should warn about this issue.

> - Jeff

Ion


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