Calculator  Step 6
power10.hpp
Go to the documentation of this file.
1 #ifndef POWER10_HPP_
2 #define POWER10_HPP_
3 
4 // Listing 51-6. Computing a Power of 10 at Compile Time
6 template<class T>
7 T constexpr power10_helper(T n, T result)
8 {
9  return n == T{} ? result : power10_helper(n - T{1}, T{10} * result);
10 }
11 
13 template<class T>
14 T constexpr power10(T n)
15 {
16  return power10_helper(n, T{1});
17 }
18 
19 #endif
T constexpr power10(T n)
Compute a power of 10 at compile time. The type T must support subtraction and multiplication.
Definition: power10.hpp:14
T constexpr power10_helper(T n, T result)
Called from power10 to compute 10n, storing the result so far in result.
Definition: power10.hpp:7