Allow using polymorphic_downcast and polymorphic_cast in constexpr (#30)

This commit is contained in:
Antony Polukhin
2025-06-27 20:42:54 +03:00
committed by GitHub
parent 452adac97f
commit b74a4c34cb
3 changed files with 56 additions and 6 deletions
+42
View File
@@ -38,6 +38,48 @@ namespace
};
}
constexpr bool compile_time_polymorphic_cast_check() {
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201907L
Derived derived;
Base* base = &derived;
return polymorphic_cast<Derived*>(base) != nullptr;
#endif
return true;
}
static_assert(
compile_time_polymorphic_cast_check(),
"polymorphic_cast does not work at compile time"
);
constexpr bool compile_time_polymorphic_downcast_check() {
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201907L
Derived derived;
Base* base = &derived;
return polymorphic_downcast<Derived*>(base) != nullptr;
#endif
return true;
}
static_assert(
compile_time_polymorphic_downcast_check(),
"polymorphic_downcast does not work at compile time"
);
constexpr bool compile_time_polymorphic_downcast2_check() {
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201907L
Derived derived;
Base& base = derived;
Derived& derived_again = polymorphic_downcast<Derived&>(base);
(void)derived_again;
#endif
return true;
}
static_assert(
compile_time_polymorphic_downcast2_check(),
"polymorphic_downcast does not work at compile time"
);
int main( int argc, char * argv[] )
{