constexpr underlying_type_t<_Tp>
to_underlying(_Tp __value) noexcept
{ return static_cast<underlying_type_t<_Tp>>(__value); }
+
+#define __cpp_lib_unreachable 202202L
+ /// Informs the compiler that program control flow never reaches this point.
+ /**
+ * Evaluating a call to this function results in undefined behaviour.
+ * This can be used as an assertion informing the compiler that certain
+ * conditions are impossible, for when the compiler is unable to determine
+ * that by itself.
+ *
+ * For example, it can be used to prevent warnings about reaching the
+ * end of a non-void function without returning.
+ *
+ * @since C++23
+ */
+ [[noreturn,__gnu__::__always_inline__]]
+ inline void
+ unreachable()
+ {
+#ifdef _GLIBCXX_DEBUG
+ std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr);
+#elif defined _GLIBCXX_ASSERTIONS
+ __builtin_trap();
+#else
+ __builtin_unreachable();
+#endif
+ }
#endif // C++23
#endif // C++20
#endif // C++17
# define __cpp_lib_string_resize_and_overwrite 202110L
#endif
#define __cpp_lib_to_underlying 202102L
+#define __cpp_lib_unreachable 202202L
#endif
#endif // C++2b
#endif // C++20
__glibcxx_assert_fail(const char* file, int line,
const char* function, const char* condition) noexcept
{
- fprintf(stderr, "%s:%d: %s: Assertion '%s' failed.\n",
- file, line, function, condition);
+ if (file && function && condition)
+ fprintf(stderr, "%s:%d: %s: Assertion '%s' failed.\n",
+ file, line, function, condition);
+ else if (function)
+ fprintf(stderr, "%s: Undefined behavior detected.\n", function);
abort();
}
}
--- /dev/null
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <utility>
+
+#ifndef __cpp_lib_unreachable
+# error "Feature-test macro for unreachable missing in <utility>"
+#elif __cpp_lib_unreachable != 202202L
+# error "Feature-test macro for unreachable has wrong value in <utility>"
+#endif
+
+bool test01(int i)
+{
+ if (i == 4)
+ return true;
+ std::unreachable();
+} // { dg-bogus "control reaches end of non-void function" }
--- /dev/null
+// { dg-options "-std=gnu++23" }
+// { dg-do preprocess { target c++23 } }
+
+#include <version>
+
+#ifndef __cpp_lib_unreachable
+# error "Feature-test macro for unreachable missing in <version>"
+#elif __cpp_lib_unreachable != 202202L
+# error "Feature-test macro for unreachable has wrong value in <version>"
+#endif