deal.II version GIT relicensing-2055-gd703fda1ad 2024-10-30 20:00:00+00:00
|
This class provides a local alias type
that is equal to the template argument but only if the template argument corresponds to a scalar type (i.e., one of the floating point types, signed or unsigned integer, or a complex number). If the template type T
is not a scalar, then no class EnableIfScalar<T>
is declared and, consequently, no local alias is available.
The purpose of the class is to disable certain template functions if one of the arguments is not a scalar number. By way of (nonsensical) example, consider the following function:
This function can be called with any two arguments of the same type T
. This includes arguments for which this clearly makes no sense. Consequently, one may want to restrict the function to only scalars, and this can be written as
At a place where you call the function, the compiler will deduce the type T
from the arguments. For example, in
it will deduce T
to be double
, and because EnableIfScalar<double>::type
equals double
, the compiler will instantiate a function double multiply(const double, const
double)
from the template above. On the other hand, in a context like
the compiler will deduce T
to be std::vector<char>
but because EnableIfScalar<std::vector<char>>::type
does not exist the compiler does not consider the template for instantiation. This technique is called "Substitution Failure is not an Error (SFINAE)". It makes sure that the template function can not even be called, rather than leading to a later error about the fact that the operation t1*t2
is not defined (or may lead to some nonsensical result). It also allows the declaration of overloads of a function such as multiply
for different types of arguments, without resulting in ambiguous call errors by the compiler.
Definition at line 568 of file template_constraints.h.