Home > Backend Development > C++ > Why Do C 11 Deleted Functions Still Participate in Overload Resolution?

Why Do C 11 Deleted Functions Still Participate in Overload Resolution?

Barbara Streisand
Release: 2024-12-19 12:40:09
Original
469 people have browsed it

Why Do C  11 Deleted Functions Still Participate in Overload Resolution?

Why C 11-Deleted Functions Participate in Overload Resolution

C 11 introduced the = delete syntax to prevent the invocation of certain function overloads with specific parameter types. This behavior may seem unusual, as one might expect deleted functions to be entirely removed from consideration during overload resolution. However, there are specific reasons for this design decision.

Preventing Implicit Conversions

One primary purpose of = delete is to prohibit the use of implicit conversions in certain scenarios. When a function with deleted overloads participates in overload resolution, it ensures that the compiler will select an exact match or report an error. This prevents the use of implicit conversions to inadvertently call a deleted overload.

Consider the example below:

struct onlydouble {
  onlydouble(std::intmax_t) = delete; // Prevent implicit conversion from integer
  onlydouble(double);
};
Copy after login

If the deleted overload were entirely removed from consideration, the following code would be allowed:

onlydouble val(20);
Copy after login

The compiler would implicitly convert the integer literal to double and select the non-deleted overload. However, with deleted overloads participating in overload resolution, the compiler rejects the code because it identifies the deleted overload as an exact match for an integer literal argument.

Special Syntax vs. "This Does Not Exist"

One may question why C 11 defines = delete as "I forbid this" rather than "this does not exist." The answer lies in the lack of a need for explicit grammar to represent non-existence. Undefined identifiers and missing special member functions implicitly convey the concept of "this does not exist."

Conclusion

C 11-deleted functions participate in overload resolution to prevent implicit conversions and enforce the use of certain parameter types. This design feature allows for precise control over the invocation of function overloads, offering advantages in scenarios where implicit conversions are not desirable or lead to ambiguities.

The above is the detailed content of Why Do C 11 Deleted Functions Still Participate in Overload Resolution?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template