|
17 | 17 | #pragma once
|
18 | 18 |
|
19 | 19 | #if _MSC_VER
|
20 |
| -#include "sus/assertions/check.h" |
| 20 | +# include "sus/assertions/check.h" |
| 21 | +# include "sus/num/cast.h" |
21 | 22 | #endif
|
22 | 23 |
|
23 | 24 | #if _MSC_VER && !defined(__clang__)
|
24 | 25 | /// Literal integer value.
|
25 |
| -#define _sus__integer_literal(Name, T) \ |
26 |
| - /* A `constexpr` workaround for MSVC bug that doesn't constant-evaluate \ |
| 26 | +# define _sus__integer_literal(Name, T) \ |
| 27 | + /* A `constexpr` workaround for MSVC bug that doesn't constant-evaluate \ |
27 | 28 | * user-defined literals in all cases: \
|
28 | 29 | * https://developercommunity.visualstudio.com/t/User-defined-literals-not-constant-expre/10108165 \
|
29 | 30 | * \
|
|
34 | 35 | * However that triggers a different MSVC bug when used with any \
|
35 | 36 | * unary/binary operator in a templated function: \
|
36 | 37 | * https://developercommunity.visualstudio.com/t/MSVC-Compiler-bug-with:-numeric-literal/10108160 \
|
37 |
| - */ \ |
38 |
| - T inline constexpr operator""_##Name(unsigned long long val) noexcept { \ |
39 |
| - sus_check(val <= static_cast<unsigned long long>(T::MAX_PRIMITIVE)); \ |
40 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
41 |
| - } |
| 38 | + */ \ |
| 39 | + T inline constexpr operator""_##Name(unsigned long long val) noexcept { \ |
| 40 | + sus_check(val <= static_cast<unsigned long long>(T::MAX_PRIMITIVE)); \ |
| 41 | + return T(static_cast<decltype(T::primitive_value)>(val)); \ |
| 42 | + } |
42 | 43 | #else
|
43 | 44 | /// Literal integer value.
|
44 |
| -#define _sus__integer_literal(Name, T) \ |
45 |
| - T inline consteval operator""_##Name(unsigned long long val) { \ |
46 |
| - if (val > static_cast<unsigned long long>(T::MAX_PRIMITIVE)) \ |
47 |
| - throw "Integer literal out of bounds for ##T##"; \ |
48 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
49 |
| - } |
| 45 | +# define _sus__integer_literal(Name, T) \ |
| 46 | + T inline consteval operator""_##Name(unsigned long long val) { \ |
| 47 | + if (val > static_cast<unsigned long long>(T::MAX_PRIMITIVE)) \ |
| 48 | + throw "Integer literal out of bounds for ##T##"; \ |
| 49 | + return T(static_cast<decltype(T::primitive_value)>(val)); \ |
| 50 | + } |
50 | 51 | #endif
|
51 | 52 |
|
52 | 53 | #if _MSC_VER
|
53 | 54 | /// Literal float value.
|
54 |
| -#define _sus__float_literal(Name, T) \ |
55 |
| - /* A `constexpr` workaround for MSVC bug that doesn't constant-evaluate \ |
| 55 | +# define _sus__float_literal(Name, T) \ |
| 56 | + /* A `constexpr` workaround for MSVC bug that doesn't constant-evaluate \ |
56 | 57 | * user-defined literals in all cases: \
|
57 | 58 | * https://developercommunity.visualstudio.com/t/User-defined-literals-not-constant-expre/10108165 \
|
58 | 59 | * \
|
|
63 | 64 | * However that triggers a different MSVC bug when used with any \
|
64 | 65 | * unary/binary operator in a templated function: \
|
65 | 66 | * https://developercommunity.visualstudio.com/t/MSVC-Compiler-bug-with:-numeric-literal/10108160 \
|
66 |
| - */ \ |
67 |
| - T inline constexpr operator""_##Name(long double val) noexcept { \ |
68 |
| - sus_check(val <= static_cast<long double>(T::MAX_PRIMITIVE)); \ |
69 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
70 |
| - } \ |
71 |
| - T inline constexpr operator""_##Name(unsigned long long val) noexcept { \ |
72 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
73 |
| - } |
| 67 | + */ \ |
| 68 | + T inline constexpr operator""_##Name(long double val) noexcept { \ |
| 69 | + sus_check(val <= static_cast<long double>(T::MAX_PRIMITIVE)); \ |
| 70 | + return T(static_cast<decltype(T::primitive_value)>(val)); \ |
| 71 | + } \ |
| 72 | + T inline constexpr operator""_##Name(unsigned long long val) noexcept { \ |
| 73 | + return T(::sus::cast<decltype(T::primitive_value)>(val)); \ |
| 74 | + } |
74 | 75 |
|
75 | 76 | #else
|
76 | 77 | /// Literal float value.
|
77 |
| -#define _sus__float_literal(Name, T) \ |
78 |
| - T inline consteval operator""_##Name(long double val) { \ |
79 |
| - if (val > static_cast<long double>(T::MAX_PRIMITIVE)) \ |
80 |
| - throw "Float literal out of bounds for ##T##"; \ |
81 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
82 |
| - } \ |
83 |
| - T inline consteval operator""_##Name(unsigned long long val) { \ |
84 |
| - return T(static_cast<decltype(T::primitive_value)>(val)); \ |
85 |
| - } |
| 78 | +# define _sus__float_literal(Name, T) \ |
| 79 | + T inline consteval operator""_##Name(long double val) { \ |
| 80 | + if (val > static_cast<long double>(T::MAX_PRIMITIVE)) \ |
| 81 | + throw "Float literal out of bounds for ##T##"; \ |
| 82 | + return T(static_cast<decltype(T::primitive_value)>(val)); \ |
| 83 | + } \ |
| 84 | + T inline consteval operator""_##Name(unsigned long long val) { \ |
| 85 | + return T(::sus::cast<decltype(T::primitive_value)>(val)); \ |
| 86 | + } |
86 | 87 | #endif
|
0 commit comments