C++11 =default and =delete keywords

Two new keywords are added in C++11: =default =delete. These keywords help command the compiler to either generate a function or make the function noncallable in your program. These two keywords are helpful for easier specification of what the compiler should do with the functions defined with it. The two keywords are explained in detail below.


C++11 =default

The =default keyword commands the compiler to generate a synthesize version of certain function. For instance, if you do not want to define the default constructor explicitly, we can define the function as ‘=default’. The compiler will then generate the default constructor for us without any questions ask.

class A
{
int i;

public:
A( ) =default; //compiler will generate a default constructor for us

A(int ii):i(ii) //user-defined constructor
{ }

int get const ( ) { return 0; }

~A() =default; //compiler will generate a default destructor for us
};

int main( )
{
A a; //calls the synthesized default constructor

A a1(90) ; //calls the user-defined constructor

cout<< a1.get() << endl ;

cin.get() ;
return 0 ;
}

Output

90


Some points to note about the =default

Using =default only with functions that can be generated by the compiler is meaningful. Using it with programmer’s own defined functions or functions which the compiler cannot generate wouldn’t make any sense as such functions cannot or is not liable to be generated by the compiler. So, it can only be used with constructors, copy constructor and destructor.

class A
{
string st;

public:
A() =default; //work fine

A(string s):st(s) { }

A(const A &a) =default; //work fine

int get() =default; //error! definitely!

~A() =default; //work fine
};

Defining the get() function as =default is nonsensical, as the compiler has no information of what this function do and so it cannot generate the definition of the function. Hence, defining get() as =default is an error.



C++11 =delete

The =delete keyword tells the compiler to make the function noncallable. In other words, defining the function as =delete is a way of telling the compiler to prohibit any call to that function. So, trying to call any function defined as =delete will give you an error.

class A
{
char c;

public:
A() =delete;

A(char cc):c(cc) { }

char get() const { return 0; }

~A() =default;
};

int main( )
{
A a; //error! default constructor is uncallable

A a1(‘B’); //work fine

cout&l;&l; a1.get( ) ;

return 0;
}

Output

B

The default constructor is defined as =delete, so we cannot call it.


Some points to note about =delete

Unlike the =default keyword, =delete can be also used with the function defined by the programmer besides those functions which the compiler could synthesize. Also note, functions defined as =delete can have its overloaded version.

A program is provided below to take away all your confusion.

class B
{
string str;

public:
B() =delete;

B(string s):str(s) { }

B(const B &b) =delete; //copy constructor

B(const B &b ,int) :str(b.str) { } //Overloaded version of the copy constructor

string get( ) =delete; //work fine, defined by us

string get(int) const //work fine, an overloaded version
{ return str; }

~A( ) =default;
};

int main( )
{
A a(“New string”);

cout<< a.get(0) << endl ; //calls the overloaded get() version

//cout<< a.get() ; //error!

A a1(a); //calls the overloaded copy constructor version

cout<< a1.get(2) ;

cin.get();
return 0;
}

Output

New string
New string