Home >Backend Development >C++ >Why Do Deleted C 11 Functions Still Participate in Overload Resolution?
Why Overloaded Resolution Includes C 11-Deleted Functions
C 11 introduced the = delete syntax, which marks functions as "deleted," preventing their invocation. This raises the question of why these functions still participate in overload resolution instead of being completely eliminated.
The primary purpose of = delete is to prevent the invocation of specific functions with certain parameters. It aims to prohibit implicit conversions, particularly in scenarios where they could lead to unintended consequences. Consequently, participation in overload resolution is essential for = delete to effectively forbid particular overloads.
Consider the following example:
struct onlydouble { onlydouble(std::intmax_t) = delete; onlydouble(double); };
If = delete entirely removed the function, it would have the same effect as this:
struct onlydouble2 { onlydouble2(double); };
In this case, the following code would be valid:
onlydouble2 val(20);
The compiler would attempt to invoke the constructor that takes an integer, but would implicitly convert 20 to a double and successfully invoke the constructor that takes a double.
However, with = delete, the invocation of the onlydouble constructor that takes an intmax_t is explicitly forbidden. The compiler will not attempt implicit conversions and will instead issue an error, preventing the use of this forbidden overload.
Furthermore, = delete implies "I forbid this" rather than "this does not exist." The C specification does not define the concept of a "non-existent" function. Instead, it provides a mechanism to explicitly prohibit certain invocations, allowing the programmer to control the behavior of the compiler and prevent unwanted implicit conversions.
The above is the detailed content of Why Do Deleted C 11 Functions Still Participate in Overload Resolution?. For more information, please follow other related articles on the PHP Chinese website!