C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
c++20/exercise-3-0.hpp
Go to the documentation of this file.
1
// ===-- chapter-3/c++20/exercise-3-0.hpp ----------------- -*- C++ -*- --=== //
9
#ifndef EXERCISE_3_0
10
#define EXERCISE_3_0
11
12
#include <cstdint>
13
14
15
namespace
exercise_3_0
{
16
19
inline
namespace
cpp20 {
20
28
consteval
uint32_t
checked_binary
(uint64_t
const
N)
29
{
30
// Even though "N" is a compile-time construct in consteval functions, it
31
// cannot be used in things like static_assert or class template parameters.
32
// An ad-hoc way of simulating the static_assert is to throw an exception,
33
// which will give a compilation error with the provided string.
34
if
((N % 10) > 1) {
throw
"N is not a binary number"
; }
35
36
return
(N == 0) ? 0 : (N % 10) + (2 *
checked_binary
(N/10));
37
}
38
39
}
// inline namespace cpp20
40
}
// namespace exercise_3_0
41
42
#endif
// EXERCISE_3_0
exercise_3_0
Encapsulate solution for Exercise 3-0.
Definition:
c++20/exercise-3-0.hpp:15
exercise_3_0::checked_binary
Compile-time binary to decimal number translation.
Definition:
exercise-3-0.hpp:38
chapter-3
c++20
exercise-3-0.hpp
Generated on Fri Mar 8 2024 12:11:47 for C++ Template Metaprogramming by
1.9.6