File tree Expand file tree Collapse file tree 4 files changed +55
-0
lines changed Expand file tree Collapse file tree 4 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 11
2+ == `ct_check.hpp`
3+
4+ `ct_check` is a construct that can be used to emit user-generated
5+ compile-time diagnostics. It uses `ct_string`.
6+
7+ For example:
8+ [source,cpp]
9+ ----
10+ stdx::ct_check<std::is_integral<float>>.emit<"This is not a very helpful error message">();
11+ ----
12+
13+ The output from this (which varies by compiler) will contain the string given,
14+ and could be something like:
15+ [source,bash]
16+ ----
17+ main.cpp:14:27: error: no matching member function for call to 'emit'
18+ 14 | stdx::ct_check<false>.emit<"This is not a very helpful error message">();
19+ | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+ include/stdx/ct_string.hpp:131:27: note: candidate template ignored: constraints not satisfied
21+ [with S = ct_string<41>{{"This is not a very helpful error m[...]"}}]
22+ 131 | constexpr static auto emit()
23+ | ^
24+ include/stdx/ct_string.hpp:132:18: note: because
25+ 'diagnostic<ct_string<41>{{"This is not a very helpful error message"}}>' evaluated to false
26+ 132 | requires diagnostic<S>
27+ | ^
28+ ----
29+
30+ Notice that the error message is elided at first, but then given in full. Such
31+ are the quirks of compilers. If the compile-time condition is true, of course no
32+ diagnostic will be emitted.
33+
234== `ct_conversions.hpp`
335
436https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/ct_conversions.hpp[`ct_conversions.hpp`]
Original file line number Diff line number Diff line change @@ -124,6 +124,17 @@ template <ct_string S> CONSTEVAL auto operator""_cts() { return S; }
124124} // namespace ct_string_literals
125125} // namespace literals
126126
127+ template <bool B> struct ct_check_t {
128+ template <ct_string S> constexpr static bool diagnostic = false ;
129+ template <ct_string S>
130+ constexpr static auto emit () -> void
131+ requires diagnostic<S>;
132+ };
133+ template <> struct ct_check_t <true > {
134+ template <ct_string S> constexpr static auto emit () -> void {}
135+ };
136+ template <bool B> constexpr auto ct_check = ct_check_t <B>{};
137+
127138} // namespace v1
128139} // namespace stdx
129140
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ add_fail_tests(
2626
2727if (${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
2828 add_fail_tests(
29+ ct_check
2930 dynamic_span_no_ct_capacity
3031 dynamic_container_no_ct_capacity
3132 tuple_index_out_of_bounds
Original file line number Diff line number Diff line change 1+ #include < stdx/ct_string.hpp>
2+
3+ // EXPECT: 01234567890123456789012345678901234567890123456789
4+
5+ constexpr auto msg =
6+ stdx::ct_string{" 01234567890123456789012345678901234567890123456789" };
7+
8+ auto main () -> int {
9+ stdx::ct_check<true >.emit <" not emitted" >();
10+ stdx::ct_check<false >.emit <msg>();
11+ }
You can’t perform that action at this time.
0 commit comments