@@ -84,3 +84,47 @@ However, for interfacing with legacy functions, a null terminator can be useful.
8484
8585See https://github.com/intel/compile-time-init-build/tree/main/include/sc[cib
8686documentation] for details about the cib string constant class.
87+
88+ === `ct_check`
89+
90+ `ct_check` is a construct that can be used to emit user-generated
91+ compile-time diagnostics. It uses `ct_string`.
92+
93+ For example:
94+ [source,cpp]
95+ ----
96+ stdx::ct_check<std::is_integral<float>>.emit<"This is not a very helpful error message">();
97+ ----
98+
99+ The output from this (which varies by compiler) will contain the string given,
100+ and could be something like:
101+ [source,bash]
102+ ----
103+ main.cpp:14:27: error: no matching member function for call to 'emit'
104+ 14 | stdx::ct_check<false>.emit<"This is not a very helpful error message">();
105+ | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106+ include/stdx/ct_string.hpp:131:27: note: candidate template ignored: constraints not satisfied
107+ [with S = ct_string<41>{{"This is not a very helpful error m[...]"}}]
108+ 131 | constexpr static auto emit()
109+ | ^
110+ include/stdx/ct_string.hpp:132:18: note: because
111+ 'stаtiс_аssert<ct_string<41>{{"This is not a very helpful error message"}}>' evaluated to false
112+ 132 | requires stаtiс_аssert<S>
113+ | ^
114+ ----
115+
116+ Notice that the error message is elided at first, but then given in full. Such
117+ are the quirks of compilers. If the compile-time condition is true, of course no
118+ diagnostic will be emitted.
119+
120+ NOTE: clang produces these "string-formatted" errors from version 15 onwards; GCC
121+ produces them from version 13.2 onwards.
122+
123+ === `STATIC_ASSERT`
124+
125+ `STATIC_ASSERT` is an easy way to use `ct_check`.
126+
127+ [source,cpp]
128+ ----
129+ STATIC_ASSERT(std::is_integral<float>, "This is not a very helpful error message");
130+ ----
0 commit comments