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.
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.
}
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.
}
class C {
public:
C();
};
C::C() = delete; // Error, the deleted definition of function C must be
// the first declaration of the function.