Version: SMASH-3.1
numeric_cast.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2022
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_NUMERIC_CAST_H_
11 #define SRC_INCLUDE_SMASH_NUMERIC_CAST_H_
12 
13 #include <limits>
14 
15 namespace smash {
16 
30 template <typename D, typename S>
31 constexpr D numeric_cast(const S value);
32 
41 template <>
42 constexpr uint32_t numeric_cast(const size_t value) {
43  // Conversion from unsigned to unsigned
44  // => the only possible problem is a positive overflow.
45  if (value > std::numeric_limits<uint32_t>::max()) {
46  throw std::overflow_error(
47  "Input value overflows the target uint32_t type!");
48  }
49  return static_cast<uint32_t>(value);
50 }
51 
52 } // namespace smash
53 
54 #endif // SRC_INCLUDE_SMASH_NUMERIC_CAST_H_
Definition: action.h:24
constexpr D numeric_cast(const S value)
Function template to perform a safe numeric conversion between types.
#define S(x, n)
Definition: sha256.cc:54