Boost logo

Boost :

Subject: Re: [boost] rvalue ref best practices?
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2012-06-09 20:09:14


On Jun 9, 2012, at 6:58 PM, Howard Hinnant wrote:

> On Jun 9, 2012, at 6:44 PM, Daniel Larimer wrote:
>
>> In my particular case I am passing 'messages' around and they only ever exist in one place at a time. I want the compiler to tell me when I try to make a copy. The effect is the same as using unique_ptr<Message> except that Message has value semantics and there is no 'extra' heap allocation for Message which is essentially a pointer to the real data.
>
> Perhaps in your particular case a move-only message, or a message with an explicit copy member is a good idea. You might be willing to sacrifice usability with generic-copy-algorithms for this class to get the benefit of being assured that copies either can't happen or are suitably rare.
>
> But I wouldn't recommend an explicit copy for more general purpose types, because a general purpose type has to be usable in such a wide variety use cases. Obviously though we do have general purpose move-only types.

More thought: A type with an explicit copy member is effectively a move-only type as far as general purpose / generic clients are concerned. And C++11 has pretty good support for move only types (you can return them from factory functions, and use them in sequence-permuting generic algorithms such as sort and reverse). So as long as your clients that need a copy are a niche clientele that is a small enough group to be able to know how to copy your class, maybe this isn't so restrictive. Truly general purpose / generic clients can get a lot done with a move-only type.

Howard


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