9 #ifndef CPROVER_UTIL_NARROW_H
10 #define CPROVER_UTIL_NARROW_H
12 #include <type_traits>
18 template <
typename output_type,
typename input_type>
22 std::is_arithmetic<input_type>::value &&
23 std::is_arithmetic<output_type>::value,
24 "narrow_cast is intended only for numeric conversions");
25 return static_cast<output_type
>(value);
33 template <
typename output_type,
typename input_type>
36 const auto output =
static_cast<output_type
>(input);
37 INVARIANT(
static_cast<input_type
>(output) == input,
"Data loss detected");
43 template <
typename output_type,
typename input_type>
46 auto const result = narrow_cast<input_type>(input);
49 throw std::out_of_range{
"narrowing gave a different value than expected"};
output_type narrow(input_type input)
Run-time checked narrowing cast.
output_type narrow_or_throw_out_of_range(input_type input)
Run-time checked narrow cast.
output_type narrow_cast(input_type value)
Alias for static_cast intended to be used for numeric casting Rationale: Easier to grep than static_c...
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.