Learn How to Use C++ Deleted Functions For Windows Development In C++Builder
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.
Deleted functions example
class A { A() = delete; A& operator = (A & a) = delete; };
The definition form
indicates that the function may not be used. However, all lookup and overload resolution occurs before the deleted definition is noted. That is, it is the definition that is deleted, not the symbol; overloads that resolve to that definition are ill-formed.=delete;
The primary power of this approach is twofold. First, use of default language facilities can be made an error by deleting the definition of functions that they require. Second, problematic conversions can be made an error by deleting the definition for the offending conversion (or overloaded function).
This approach of checking for a delete definition late has two benefits. First, it achieves the goal of making a bad overload visible. Second, it is relatively easy to implement, requiring no change to the already complicated lookup and overload rules.
- The deleted definition of a function must be its first declaration. This rule prevents inconsistency in interpretation.
- The deleted definition is an inline definition, thus requiring consistent definition throughout the program via the one-definition rule.
- The deleted definition mechanism is orthogonal to access specifiers, though accessibility is somewhat moot if the function has been deleted.
- One can define a template with a deleted definition. Specialization and argument deduction occur as they would with a regular template function, but explicit specialization is not permitted.
- One cannot use a deleted function in a
sizeof
expression. We believe the existing language rules will prevent this. - A deleted virtual function may not override a non-deleted virtual function and vice-versa.
Head over and check out all of the modern C++ language features found in C++Builder.
Leave Your Comment