C++C++11C++14C++17C++20Learn C++Syntax

What Is A Deleted Function In Modern C++?

What Is A Deleted Function In Modern C++

In programming, some of the most important parts are the functions that we create. They are like Lego parts that we can use in all places of the application. In modern C++ development, a function declaration is really important, and these functions gained some additional abilities after the C++11 standards. C++11 introduced two new features: defaulted and deleted functions. These are very useful in the function members of classes that we call methods. In this post, we explain what is deleted function is in modern C++.

What is a function in C++?

In modern C++ development, a function declaration is really important. In C++, a function declaration is made by using the function name and its return type A function definition associates the function name/type with the function body.

Since C++11, a function declaration syntax with the full options is as shown below.,

Here is a simple function that return sum of a and b variables.

What is a deleted function in modern C++?

C++11 introduced 2 new features for functions – defaulted and deleted functions. These are very useful in the function members of classes that we call methods. Here is the syntax how to delete a function in a class or struct.

A deleted function is a function that contains =delete; in its prototype. This construction, introduced with C++11, indicates that the function may not be used. This construction can be used to forbid the usage of default language facilities (like default constructors or default operators) or problematic conversions.

and here is an example,

this function is deleted and cannot be used in any future usage after this declaration.

Is there a simple deleted function example in C++?

A simple empty C++ class is perfectly equivalent to default implementations (Rule of Six) in a class, it has constructor and other 5 default members. For example, we can set delete for the default constructor of a class, in compilation this class will not have that default constructor. Here is how to do it:

If you don’t want your class copy:

Is there a full deleted function example in C++?

Now, let’s use delete keyword in all 6 rules of C++ classes,

While the delete keyword is easy to use, there are many different usage options. Please search deleted in LearnCPlusPlus.org for more examples about deleted methods.

For more information on this feature, see Deleted functions Proposal document.

What Is A Deleted Function In Modern C++ The C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?