From: DeMarcus on
Hi,

I'm working on a new project using C++0x.

Before I have always made disabled functions private, like this.

class SomeClass
{
public:
SomeClass() {}

private:
// Disable copying.
SomeClass( const SomeClass& );
SomeClass& operator=( const SomeClass& );
};

Is your recommendation to do the following instead?

class SomeClass
{
public:
SomeClass() {}
SomeClass( const SomeClass& ) = delete;
SomeClass& operator=( const SomeClass& ) = delete;
};

Is there anything that speaks against converting to using delete?


Thanks,
Daniel

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jeffrey Schwab on
On 3/3/10 3:29 PM, DeMarcus wrote:
> Hi,
>
> I'm working on a new project using C++0x.
>
> Before I have always made disabled functions private, like this.
>
> class SomeClass
> {
> public:
> SomeClass() {}
>
> private:
> // Disable copying.
> SomeClass( const SomeClass& );
> SomeClass& operator=( const SomeClass& );
> };
>
> Is your recommendation to do the following instead?
>
> class SomeClass
> {
> public:
> SomeClass() {}
> SomeClass( const SomeClass& ) = delete;
> SomeClass& operator=( const SomeClass& ) = delete;
> };

My preference, in either version of the language, is a non-copyable base
type, like boost::noncopyable. The non-copyable base approach makes the
client code particularly clean and readable.

/* Library code; do this once. */
class noncopyable_t
{
noncopyable_t( noncopyable_t const& );
noncopyable_t& operator=(noncopyable_t const&);
public:
noncopyable_t( ) { }
};

/* Client code; do this as many times as you want. */
struct some_t
: private noncopyable_t
{
some_t( ) { }
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]