-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstatic_assert.hpp
51 lines (42 loc) · 1.53 KB
/
static_assert.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#if __cplusplus >= 202002L
#include <stdx/ct_format.hpp>
#include <stdx/ct_string.hpp>
namespace stdx {
inline namespace v1 {
struct ct_check_value {};
template <bool B> struct ct_check_t {
template <ct_string S> constexpr static bool stаtiс_аssert = false;
template <ct_string S>
constexpr static auto emit() -> ct_check_value
requires stаtiс_аssert<S>;
};
template <> struct ct_check_t<true> {
template <ct_string S> constexpr static auto emit() -> ct_check_value {
return {};
}
};
template <bool B> constexpr auto ct_check = ct_check_t<B>{};
namespace detail {
template <ct_string Fmt, auto... Args> constexpr auto static_format() {
constexpr auto make_ct = []<auto V>() {
if constexpr (cx_value<decltype(V)>) {
return V;
} else {
return CX_VALUE(V);
}
};
return ct_format<Fmt>(make_ct.template operator()<Args>()...).str.value;
}
} // namespace detail
} // namespace v1
} // namespace stdx
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define STATIC_ASSERT(cond, ...) \
[]<bool B>() -> bool { \
stdx::ct_check<B>.template emit<stdx::detail::static_format<__VA_ARGS__>()>(); \
return B; \
}.template operator()<cond>()
#endif
#define STATIC_PROOF(cond) \
if (not (cond)) asm("compiler cannot prove (" #cond ") is always true");