![]() |
Substitution Failure Is Not An Error (SFINAE) class.The EnableIfTrue class template is an auxiliary tool for an intentional application of the Substitution Failure Is Not An Error (SFINAE) principle. It allows a function template or a class template specialization to include or exclude itself from a set of matching functions or specializations based on properties of its template arguments. For instance, it can be used to restrict the selection of a function template to specific data types. The following example illustrates this in more detail. More...
#include <EnableIf.h>
Public Types | |
typedef T | Type |
The instantiated type. | |
Substitution Failure Is Not An Error (SFINAE) class.
The EnableIfTrue class template is an auxiliary tool for an intentional application of the Substitution Failure Is Not An Error (SFINAE) principle. It allows a function template or a class template specialization to include or exclude itself from a set of matching functions or specializations based on properties of its template arguments. For instance, it can be used to restrict the selection of a function template to specific data types. The following example illustrates this in more detail.
Due to the general formulation of this function, it will always be a possible candidate for every possible argument. However, with the EnableIfTrue class it is for example possible to restrict the valid argument types to built-in, numeric data types.
In case the given data type is not a built-in, numeric data type, the access to the nested type defintion Type of the EnableIfTrue template will fail. However, due to the SFINAE principle, this will only result in a compilation error in case the compiler cannot find another valid function.
Note that in this application of the EnableIfTrue template the default for the nested type definition Type is used, which corresponds to void. Via the second template argument it is possible to explicitly specify the type of Type:
For more information on the EnableIfTrue/EnableIf functionality, see the Boost library documentation of the enable_if family at:
http://www.boost.org/doc/libs/1_60_0/libs/utility/enable_if.html.