Home >Backend Development >C++ >What Constitutes the 'Immediate Context' in C 11 SFINAE?
What Exactly Is the "Immediate Context" in C 11 SFINAE?
The concept of "immediate context" is crucial in C 11's Substitution Failure Is Not an Error (SFINAE) idiom. According to the C 11 Standard, only invalid types and expressions "in the immediate context" of the function type and its template parameter types can result in a deduction failure.
Initial Understanding
The Standard provides a limited hint in a note: "evaluation of substituted types and expressions can result in side effects such as class template specializations, function template specializations, implicit function generation, etc." These side effects are not considered part of the "immediate context."
Decision Procedure
To determine whether a substitution error occurs in the "immediate context," follow this procedure:
Concrete Examples
template<typename T> void func(typename T::type* arg);
func<A<int&>&>(nullptr); // T::type* is invalid, causing a hard error.
template<typename T> void func(typename T::type* arg);
template<> struct A<char> {};
func<A<char>>(nullptr); // T::type* is not defined, leading to a deduction failure.
Conclusion
The "immediate context" refers to expressions and types that can be resolved without requiring additional template or function generation beyond the necessary pre-instantiation step. Errors occurring in this pre-instantiation stage result in hard compilation errors, while errors in the subsequent substitution stage cause deduction failures.
The above is the detailed content of What Constitutes the 'Immediate Context' in C 11 SFINAE?. For more information, please follow other related articles on the PHP Chinese website!