[libcxx-commits] [libcxx] [libc++] P2641R4: Checking if a `union` alternative is active (`std::is_within_lifetime`) (PR #107450)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 1 14:12:07 PDT 2024


================
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H
+#define _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H
+
+#include <__config>
+#include <__type_traits/is_function.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26 && __has_builtin(__builtin_is_within_lifetime)
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI consteval bool is_within_lifetime(const _Tp* __p) noexcept {
+  if constexpr (is_function_v<_Tp>) {
+    // Avoid multiple diagnostics
+    static_assert(!is_function_v<_Tp>, "std::is_within_lifetime<T> cannot explicitly specify T as a function type");
+    return false;
+  } else {
----------------
ldionne wrote:

This is clever, but I think I'd rather use the naive way we consistently do this, and just have `static_assert` at the start of the function. If we think the duplicate diagnostics are bad, that's something we can (and should) fix in Clang (e.g. when it hits a `static_assert` it should probably stop).

There have been past discussions about this and I'm not really ready to start working around the issue in the library, I don't think that's the right place to do this.

https://github.com/llvm/llvm-project/pull/107450


More information about the libcxx-commits mailing list