Alle, während der Compiler den Code erfolgreich kompiliert, sogar die Bits, die die Funktion nicht kompilieren sollte?
Code: Select all
template
constexpr void problematic_code()
{
if constexpr (std::is_integral_v) {
static_assert(false, "This code should not compile!");
}
}
< /code>
Der Code kompiliert nicht, wenn ich eine der folgenden < /p>
schreibe ">problematic_code();
problematic_code();
// ...
< /code>
Das ist in Ordnung! Aber gibt es einen Mechanismus in C ++ 20/33, mit dem ich eine Funktion wie
schreiben kanntemplate
constexpr bool will_problematic_code_compile() {
if (calling problematic_code() reaches static_assert(false)) {
return false;
}
return true;
}
< /code>
Dann möchte ich einen Test wie < /p>
schreibenTEST_CASE("") {
CHECK(not will_problematic_code_compile());
CHECK(not will_problematic_code_compile());
CHECK(will_problematic_code_compile());
}
Code: Select all
template
constexpr bool will_problematic_code_compile()
{
return requires { problematic_code(); };
}