views:

173

answers:

3

The working draft explicitly calls out that defaulted-functions must be special member functions (eg copy-constructor, default-constructor, etc, (§8.4.2.1-1)). Which makes perfect sense.

However, I don't see any such restriction on deleted-functions(§8.4.3). Is that right?

Or in other words are these three examples valid c++0?

struct Foo
{
   // 1
   int bar( int ) = delete;
};


// 2
int baz( int ) = delete;


template< typename T >
int boo( T t );

// 3
template<>
int boo<int>(int t) = delete;
A: 

From what I understand from the definition of "deleted" member functions, is that it applies only to special member functions (constructor, copy, assignment ) that can be created automatically by the compiler and not to ordinary member functions (which makes no sense at all IMO, to declare functions to be "deleted", so just do not declare them anyway)

Max
It can be used for any function that has a default. For example, to disable promoting.
a1ex07
+4  A: 

I think they're all OK.

= delete is good for ensuring an overload isn't used (§8.4.3/2), which is useful outside classes.

Now 5 months later I look at the other answers… delete isn't only useful for functions with implicit definitions. It's the clean alternative to a comment saying "no implementation — using this is a linker error." It provides an explicit way to not implement something, for example a base template where only explicit specializations will actually exist. The compiler will complain before link time.

For a slightly odd, but totally reasonable example, consider

class abc {
protected:
    inline virtual ~abc() = 0;
    inline virtual void do_something() = 0;
};

abc::~abc() {}
void abc::do_something = delete;

Both = 0 and = delete can be used on the same function. Without = delete, the user can give an accidental courtesy call to abc::do_something().

I wouldn't be surprised if the next iteration of C++ after C++0x adds explicitly deleted classes.

Potatoswatter
+4  A: 
KennyTM