In this article we will discuss how to use delete keyword with functions and its use cases.

Deleting Functions using ‘delete’ keyword

In C++11 a new feature of keyword delete is introduced. Now we can apply the delete keyword to functions to make them uncallable.

Its practical use cases are,

  • Deleting compiler generated functions like copy constructor, assignment operators, move constructor , move assignment operator and default constructor.
  • Deleting member functions to prevent data loss conversions
  • Restrict Object creation on Heap by deleting new operator for class
  • Delete specific template specilaizations

Let’s discuss them one by one i.e.

Deleting Copy Constructor and Assignment Operator

In the above class User, Copy Constructor and Assignment Operator are deleted. If anybody tries to call the deleted function then it will generate compile time error.
For example,

As copy constructor is deleted, therefore above lines will throw following compile time error i.e.

A part from applying delete keywords to compiler generated functions, we can apply delete keyword to other functions too.

Deleting member functions to prevent data loss conversions

Some times due to automatic type conversion we can call the functions with wrong parameters too. For example, for User class we have this parameterized constructor i.e.

Even if this constructor accepts id as int, still we can call this by double 7 char too i.e.

It will work but these are invalid conversions, with delete keyword we can prevent them too by declaring them deleted i.e.

Now on creating User object with double and char will throw compile time error i.e.

Restrict Object creation on Heap by deleting new operator for class

We can delete the new operator of class User i.e.

 

Now if we try to create User Object on heap using new, it will give compile time error i.e.

Compile Error:

Complete example is as follows,

Delete specific template specialisation

With delete keyword we can restrict certain template specialisations of template classes or functions. Let’s see how to do that,
Suppose we have a template class of Complex Number

We can use ComplexNumber with different types like int, double & char etc. i.e.

Now we want to restrict that no one can use CompleNumber class with double or char as template parameter. For that we need to delete certain specialized functions i.e.

Now on trying to use ComplexNumber<> class with double or char as template parameter will generate compile time error.

Complete ComplexNumber class with deleted specialisations is as follows,

Different between deleted function and private functions

By making functions private we can also restrict its calling but still marking function deleted with delete keyword has its own advantages like,

  • Private member functions can be called from other member functions, whereas, deleted functions can not be called even from other member functions.
  • Deleted functions exists in name lookup , if compiler finds a function is deleted then it will not lookup for other matching functions based on type lookups, hence prevents unneccesary data loss and bugs.

 

Click Here to Subscribe for more Articles / Tutorials like this.