diff --git a/Utilities/std/cm/utility b/Utilities/std/cm/utility index a81a99a322..a3ab807895 100644 --- a/Utilities/std/cm/utility +++ b/Utilities/std/cm/utility @@ -9,6 +9,13 @@ # define CMake_HAVE_CXX_IN_PLACE #endif +#if (__cplusplus >= 202302L || \ + (defined(_MSVC_LANG) && _MSVC_LANG >= 202302L)) && \ + defined(__cpp_lib_unreachable) && (__cpp_lib_unreachable >= 202202L) +# define CMake_HAVE_CXX_UNREACHABLE +#endif + +#include // IWYU pragma: export #include // IWYU pragma: export namespace cm { @@ -27,5 +34,32 @@ struct in_place_t constexpr in_place_t in_place{}; +#endif + +#if defined(CMake_HAVE_CXX_UNREACHABLE) + +using std::unreachable; + +#else + +[[noreturn]] inline void unreachable() +{ +# if defined(_MSC_VER) + __assume(0); + std::abort(); +# elif defined(__has_builtin) +# if __has_builtin(__builtin_unreachable) + __builtin_unreachable(); +# else + std::abort(); +# endif +# elif defined(__GNUC__) && defined(__GNUC_MINOR__) && \ + ((__GNUC__ * 100 + __GNUC_MINOR__) >= 405) + __builtin_unreachable(); +# else + std::abort(); +# endif +} + #endif }