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++.
Table of Contents
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.,
1 2 3 |
noptr_declarator ( parameter_list ) cv (optional) ref (optional) except (optional) attr (optional) -> trailing |
Here is a simple function that return sum of a and b variables.
1 2 3 4 5 6 |
int sum( int a, int b) { return a+b; } |
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.
1 2 3 |
function_declaration = delete; |
and here is an example,
1 2 3 |
f() = delete; |
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:
1 2 3 4 5 6 |
class A { A() = delete; // constructor is deleted }; |
If you don’t want your class copy:
1 2 3 4 5 6 |
class A { A& operator = (A & a) = delete; // copy assignment operator is deleted }; |
1 2 3 4 5 6 7 8 9 |
struct st_x { void* operator new(std::size_t) = delete; void* operator new[](std::size_t) = delete; }; st_x* p = new st_x; // ERROR: attempts to call deleted st_x::operator new |
Is there a full deleted function example in C++?
Now, let’s use delete
keyword in all 6 rules of C++ classes,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Tx { public: Tx() = delete; // 1. constructor Tx(Tx const& other) = delete; // 2. copy constructor Tx(Tx&& other) = delete; // 3. move constructor Tx& operator=(Tx const& other) = delete; // 4. copy assignment operator Tx& operator=(Tx&& other) = delete; // 5. move assignment operator ~Tx() = delete; // 6. destructor }; int main() { Tx o; // ERROR: see errors for each delete options } |
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.
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.