We can delete a function by using delete keyword as shown below.
#include <iostream>
void print(int a) = delete;
int main(int argc, char const *argv[])
{
print(1);
return 0;
}When print(1) is invoked and compiler finds that the matched function print is deleted, it raises compilation error.
Why It Is Useful?
We can understand the usefulness of this feature with the help of an example:
#include <iostream>
void print(int a)
{
std::cout << a << "\n";
}
int main(int argc, char const *argv[])
{
print(1);
print('b');
print(true);
return 0;
}print(1) is the exact match for the call, second call print('b') also has match when char is promoted to int and in third call print(true), bool is promoted to int and so has a match call.
So, we get values 1, 97, and 1. However, we would not want calls other than int to happen as they may not make any sense. To achieve this, we can use delete those matches as shown below.
#include <iostream>
void print(int a)
{
std::cout << a << "\n";
}
void print(char a) = delete;
void print(bool a) = delete;
int main(int argc, char const *argv[])
{
print(1);
print('a');
print(true);
return 0;
}When we compile this code, we shall see compilation error as shown below.
main.cpp: In function 'int main(int, const char**)':
main.cpp:14:10: error: use of deleted function 'void print(char)'
14 | print('a');
| ~~~~~^~~~~
main.cpp:8:6: note: declared here
8 | void print(char a) = delete;
| ^~~~~
main.cpp:14:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
14 | print('a');
| ~~~~~^~~~~
main.cpp:15:10: error: use of deleted function 'void print(bool)'
15 | print(true);
| ~~~~~^~~~~~
main.cpp:9:6: note: declared here
9 | void print(bool a) = delete;
| ^~~~~
main.cpp:15:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
15 | print(true);
| ~~~~~^~~~~~
Info
It is to be noted that
= deletedoes not mean that function does not exist. It means that it is not allowed to be invoked and doing so will result in compilation error.Deleted functions are still part of overload resolution process.
Call Only an Exact Match Function
If we want to make sure that there should be one function to be exactly matched and called, we need to delete overload functions. This can be verbose to specify the deletions of all other functions. We can achieve this by deleting the function template.
#include <iostream>
void print(int a)
{
std::cout << a << "\n";
}
template <typename T>
void print(T x) = delete;
int main(int argc, char const *argv[])
{
print(1);
print('a');
print(true);
print(5L);
print(5u);
// we can call more functions with other types.
return 0;
}When we compile this program, we shall get compilation error as shown below.
template.cpp: In function 'int main(int, const char**)':
template.cpp:15:10: error: use of deleted function 'void print(T) [with T = char]'
15 | print('a');
| ~~~~~^~~~~
template.cpp:9:6: note: declared here
9 | void print(T x) = delete;
| ^~~~~
template.cpp:15:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
15 | print('a');
| ~~~~~^~~~~
template.cpp:16:10: error: use of deleted function 'void print(T) [with T = bool]'
16 | print(true);
| ~~~~~^~~~~~
template.cpp:9:6: note: declared here
9 | void print(T x) = delete;
| ^~~~~
template.cpp:16:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
16 | print(true);
| ~~~~~^~~~~~
template.cpp:17:10: error: use of deleted function 'void print(T) [with T = long int]'
17 | print(5L);
| ~~~~~^~~~
template.cpp:9:6: note: declared here
9 | void print(T x) = delete;
| ^~~~~
template.cpp:17:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
17 | print(5L);
| ~~~~~^~~~
template.cpp:18:10: error: use of deleted function 'void print(T) [with T = unsigned int]'
18 | print(5u);
| ~~~~~^~~~
template.cpp:9:6: note: declared here
9 | void print(T x) = delete;
| ^~~~~
template.cpp:18:10: note: use '-fdiagnostics-all-candidates' to display considered candidates
18 | print(5u);