Home > Backend Development > C++ > What Determines Whether a C 11 Template Substitution Error Is a Hard or Soft Error?

What Determines Whether a C 11 Template Substitution Error Is a Hard or Soft Error?

Patricia Arquette
Release: 2024-12-18 19:03:11
Original
338 people have browsed it

What Determines Whether a C  11 Template Substitution Error Is a Hard or Soft Error?

Immediate Context in C 11 Template Substitution

The C 11 Standard introduces Substitution Failure Is Not An Error (SFINAE), which allows for the suppression of hard compilation errors during template argument substitution. However, the standard defines a specific "immediate context" for which SFINAE applies, leaving some uncertainty about when a substitution error results in a hard or soft error.

Immediate Context Definition

The Standard defines the immediate context as:

"Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure."

This definition is vague, but a non-normative note provides a hint:

"The evaluation of the substituted types and expressions can result in side effects such as... Such side effects are not in the “immediate context” and can result in the program being ill-formed."

Decision Procedure

To determine whether a substitution error occurs in the immediate context, consider the following decision procedure:

  • Step 1: Identify all templates and implicitly-defined functions required to determine the result of the substitution.
  • Step 2: Imagine these entities are generated before substitution starts.
  • Step 3: If errors occur during this hypothetical generation, they are not in the immediate context and will result in hard errors.
  • Step 4: If all entities are generated without error, any subsequent errors during substitution are not errors but result in argument deduction failures.

Examples

  • Hard error: func> - Instantiation of A fails because pointers to references are not allowed.
  • Argument deduction failure: func> - Instantiation of A succeeds, but A::type does not exist.
  • Argument deduction failure: class B { void f(int); }; func::f - Implicit definition of f yields a deleted member function.

Conclusion

By understanding the concept of immediate context and using the above decision procedure, programmers can better predict when substitution errors will result in hard or soft errors, enabling more effective use of SFINAE in C 11 templates.

The above is the detailed content of What Determines Whether a C 11 Template Substitution Error Is a Hard or Soft Error?. 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