Home >Backend Development >C++ >What is the 'Immediate Context' in C 11 SFINAE and How Does it Affect Substitution Failures?
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:
Examples
Consider the following template and fall-back function:
template<typename T> void func(typename T::type* arg); template<> void func(...);
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!