Learn How To Solve The C++ SFINAE Problem For Expressions In Windows Development With C++ Builder

Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters is not in itself an error. We’re talking here about something related to templates, template substitution rules and metaprogramming…

A quick example:

This example is rejected by all major compilers because template deduction/substitution has historically used a simplified model of semantic checking, i.e., the SFINAE rules (which are mostly about types), instead of full semantic checking. But in C++ 11, fully general expressions allowed, and that most errors in such expressions be treated as SFINAE failures rather than errors.

There’s a continuum of errors, some errors being clearly SFINAE failures, and some clearly “real” errors, with lots of unclear cases in between. We decided it’s easier to write the definition by listing the errors that are not treated as SFINAE failures, and the list we came up with is as follows:

  • errors that occur while processing some entity external to the expression, e.g., an instantiation of a template or the generation of the definition of an implicitly-declared copy constructor.
  • errors due to implementation limits (it’s probably a category error to list these here, since they’re not errors in the normal sense, but we wanted to make it very clear that compilers don’t have to take steps to capture and recover from violations of implementation limits; such violations cause hard errors, compiler crashes, etc., the same as anywhere else in a program).
  • errors due to access violations (this is a judgment call, but the philosophy of access has always been that it doesn’t affect visibility)Everything else produces a SFINAE failure rather than a hard error.

At certain points in the template argument deduction process it is is necessary to take a function type that makes use of template parameters and replace those template parameters with the corresponding template arguments. This is done at the beginning of template argument deduction when any explicitly specified template arguments are substituted into the function type, and again at the end of template argument deduction when any template arguments that were deduced or obtained from default arguments are substituted.

The substitution occurs in all types and expressions that are used in the function type and in template parameter declarations. The expressions include not only constant expressions such as those that appear in array bounds or as nontype template arguments but also general expressions (i.e., non-constant expressions) inside sizeof, decltype, and other contexts that allow non-constant expressions. [Note: The equivalent substitution in exception specifications is done only when the function is instantiated, at which point a program is ill-formed if the substitution results in an invalid type or expression.] For example,

Head over and check out more information about SFINAE problems for expressions in Windows Development.