Deleted functions (C++11)

Note: IBM supports selected features of C++11, known as C++0x before its ratification. IBM will continue to develop and implement the features of this standard. The implementation of the language level is based on IBM's interpretation of the standard. Until IBM's implementation of all the C++11 features is complete, including the support of a new C++11 standard library, the implementation may change from release to release. IBM makes no attempt to maintain compatibility, in source, binary, or listings and other compiler interfaces, with earlier releases of IBM's implementation of the new C++11 features.

Deleted function declaration is a new form of function declaration that is introduced into the C++11 standard. To declare a function as a deleted function, you can append the =delete; specifier to the end of that function declaration. The compiler disables the usage of a deleted function.

You can declare an implicitly defined function as a deleted function if you want to prevent its usage. For example, you can declare the implicitly defined copy assignment operator and copy constructor of a class as deleted functions to prevent object copy of that class.
class A{    
public:
  A(int x) : m(x) {}
  A& operator = (const A &) = delete;     // Declare the copy assignment operator
                                          // as a deleted function.
  A(const A&) = delete;                   // Declare the copy constructor
                                          // as a deleted function.

private:
  int m;
};

int main(){
  A a1(1), a2(2), a3(3);
  a1 = a2;            // Error, the usage of the copy assignment operator is disabled.
  a3 = A(a2);         // Error, the usage of the copy constructor is disabled.
}
You can also prevent problematic conversions by declaring the undesirable conversion constructors and operators as deleted functions. The following example shows how to prevent undesirable conversions from double to a class type.
class B{
public:
  B(int){}                
  B(double) = delete;   // Declare the conversioin constructor as a deleted function
};

int main(){
  B b1(1);        
  B b2(100.1);          // Error, conversion from double to class B is disabled.
}
A deleted function is implicitly inline. A deleted definition of a function must be the first declaration of the function. For example:
class C {
public:  
  C();
};

C::C() = delete;    // Error, the deleted definition of function C must be
                    // the first declaration of the function.