5

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;
deft_code
  • 57,255
  • 29
  • 141
  • 224
  • 1
    What even is a deleted function? – Puppy May 20 '10 at 16:06
  • 1
    A deleted function is one that would have existed if it had not been deleted. E.g. you can delete the class copy ctor. This is slightly superior to making it private unimplemented, for two reasons. 1. it's more obvious when reading the code and 2. you can get an clearer error message. – MSalters May 21 '10 at 08:52
  • 2
    @MSalters: You can also delete functions which do not exist, preventing their use. E.g. given `void f(double); void f(int) = delete;`, f(42) is now an error rather than using implicit conversion. –  Oct 12 '10 at 16:14

3 Answers3

4

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
  • 134,909
  • 25
  • 265
  • 421
4

The C++0x spec (§[dcl.fct.def.delete]) doesn't deny such constructs, and g++ 4.5 recognize all 3 of them.

x.cpp: In function 'int main()':
x.cpp:4:8: error: deleted function 'int Foo::bar(int)'
x.cpp:21:11: error: used here
x.cpp:9:5: error: deleted function 'int baz(int)'
x.cpp:22:2: error: used here
x.cpp:9:5: error: deleted function 'int baz(int)'
x.cpp:22:8: error: used here
x.cpp:17:5: error: deleted function 'int boo(T) [with T = int]'
x.cpp:23:7: error: used here
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
-3

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
  • 3,128
  • 1
  • 24
  • 24