Home >Backend Development >C++ >What is the 'Immediate Context' in C 11 SFINAE and How Does it Affect Substitution Failures?

What is the 'Immediate Context' in C 11 SFINAE and How Does it Affect Substitution Failures?

Barbara Streisand
Barbara StreisandOriginal
2024-12-12 19:10:11968browse

What is the

Understanding the "Immediate Context" in C 11 SFINAE

The C 11 Standard specifies conditions for when a substitution failure results in a hard compilation error or a soft error that simply discards a template from overload resolution candidates. One key concept in this determination is the "immediate context."

Definition of "Immediate Context"

The term "immediate context" is briefly mentioned in the Standard, but its exact definition is not explicitly provided. However, it is often found in conjunction with the following text:

Note: The evaluation of the substituted types and expressions can result in side effects such as instantiation of template specializations, generation of implicitly-defined functions, etc. Such side effects are not in the “immediate context” and can result in the program being ill-formed.

This note indicates that any side effects occurring during the substitution process, such as template instantiations or implicit function definitions, are not considered part of the immediate context.

Determining Substitution Errors in the Immediate Context

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

  1. Identify the side effects: Imagine instantiating all templates and defining all implicit functions required for template argument substitution.
  2. Check for errors: If any errors occur during this "preparation" stage, they are not part of the immediate context and result in hard compilation errors.
  3. Substitute arguments: Once all necessary instantiations and definitions have been generated without error, substitute the arguments into the function template's signature.
  4. Check for errors during substitution: If any errors occur during this final substitution step, they are not true errors but rather deduction failures.

Examples

Consider the following template and fall-back function:

template<typename T>
void func(typename T::type* arg);

template<>
void func(...);
  1. Case 1: Assuming A is a template with a type member, a call to func::type*>(nullptr) will fail with a hard compilation error because instantiating A (during preparation) creates an invalid pointer to a reference.
  2. Case 2: If A has an explicit specialization for char, a call to func::type*>(nullptr) will instantiate A (preparation succeeds), but the subsequent substitution of A::type (final step) fails because it doesn't exist. This causes a deduction failure, and the fall-back function is used.

Conclusion

By understanding the concept of immediate context, you can better identify when a substitution error will result in a hard compilation error or a soft deduction failure, enabling the effective use of SFINAE in C 11.

The above is the detailed content of What is the 'Immediate Context' in C 11 SFINAE and How Does it Affect Substitution Failures?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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