Class template SFINAE
This pattern is licensed under the CC0 Public Domain Dedication.
Conditionally instantiate a class template depending on the template arguments.
We provide two partial specializations of the
foo class template:
- The template on lines 6–8 will only be instantiated when
Tis an integral type.
- The template on lines 10–12 will only be instantiated when
Tis a floating point type.
This allows us to provide different implementations of the
class depending on the template arguments it is instantiated with.
We have used
std::enable_if on line 7 and
line 11 to force instantiation to succeed only for the appropriate
template arguments. This relies on Substitution Failure Is Not An
(SFINAE), which states that
failing to instantiate a template with some particular
template arguments does not result in an error and simply discards
If you want to simply prevent a template from being instantiated
for certain template arguments, consider using
09 December 2017