Templates and generic programming
T.123
Use `constexpr` functions to compute values at compile time
Reason
A function is the most obvious and conventional way of expressing the computation of a value. Often a constexpr function implies less compile-time overhead than alternatives.
Note
"Traits" techniques are mostly replaced by template aliases to compute types and constexpr functions to compute values.
Example
template<typename T>
// requires Number<T>
constexpr T pow(T v, int n) // power/exponential
{
T res = 1;
while (n--) res *= v;
return res;
}
constexpr auto f7 = pow(pi, 7);
Enforcement
- Flag template metaprograms yielding a value. These should be replaced with
constexprfunctions.