Reference documentation for deal.II version 9.6.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
EnableIfScalar< T > Struct Template Reference

Detailed Description

template<typename T>
struct EnableIfScalar< T >

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:

template <typename T>
T multiply (const T t1, const T t2)
{
return t1*t2;
}

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

template <typename T>
multiply (const T t1, const T t2)
{
return t1*t2;
}

At a place where you call the function, the compiler will deduce the type T from the arguments. For example, in

multiply(1.234, 2.345);

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

std::vector<char> v1, v2;
multiply(v1, v2);
const unsigned int v1

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 567 of file template_constraints.h.


The documentation for this struct was generated from the following file: