Skip to content

Commit e88424c

Browse files
committed
[libc++] P2641R4: Checking if a union alternative is active (std::is_within_lifetime)
1 parent 4d819da commit e88424c

File tree

14 files changed

+214
-10
lines changed

14 files changed

+214
-10
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ Status
446446
---------------------------------------------------------- -----------------
447447
``__cpp_lib_is_virtual_base_of`` ``202406L``
448448
---------------------------------------------------------- -----------------
449-
``__cpp_lib_is_within_lifetime`` *unimplemented*
449+
``__cpp_lib_is_within_lifetime`` ``202306L``
450450
---------------------------------------------------------- -----------------
451451
``__cpp_lib_linalg`` *unimplemented*
452452
---------------------------------------------------------- -----------------

libcxx/docs/ReleaseNotes/20.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Implemented Papers
4141
- P2747R2: ``constexpr`` placement new (`Github <https://github.com/llvm/llvm-project/issues/105427>`__)
4242
- P2609R3: Relaxing Ranges Just A Smidge (`Github <https://github.com/llvm/llvm-project/issues/105253>`__)
4343
- P2985R0: A type trait for detecting virtual base classes (`Github <https://github.com/llvm/llvm-project/issues/105432>`__)
44+
- P2641R4: Checking if a ``union`` alternative is active (`Github <https://github.com/llvm/llvm-project/issues/105381>`__)
4445

4546

4647
Improvements and New Features

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"`P2874R2 <https://wg21.link/P2874R2>`__","Mandating Annex D Require No More","2023-06 (Varna)","","",""
1919
"`P2757R3 <https://wg21.link/P2757R3>`__","Type-checking format args","2023-06 (Varna)","","",""
2020
"`P2637R3 <https://wg21.link/P2637R3>`__","Member ``visit``","2023-06 (Varna)","|Complete|","19.0",""
21-
"`P2641R4 <https://wg21.link/P2641R4>`__","Checking if a ``union`` alternative is active","2023-06 (Varna)","","",""
21+
"`P2641R4 <https://wg21.link/P2641R4>`__","Checking if a ``union`` alternative is active","2023-06 (Varna)","|Complete|","20.0",""
2222
"`P1759R6 <https://wg21.link/P1759R6>`__","Native handles and file streams","2023-06 (Varna)","|Complete|","18.0",""
2323
"`P2697R1 <https://wg21.link/P2697R1>`__","Interfacing ``bitset`` with ``string_view``","2023-06 (Varna)","|Complete|","18.0",""
2424
"`P1383R2 <https://wg21.link/P1383R2>`__","More ``constexpr`` for ``<cmath>`` and ``<complex>``","2023-06 (Varna)","","",""

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ set(files
822822
__type_traits/is_valid_expansion.h
823823
__type_traits/is_void.h
824824
__type_traits/is_volatile.h
825+
__type_traits/is_within_lifetime.h
825826
__type_traits/lazy.h
826827
__type_traits/make_32_64_or_128_bit.h
827828
__type_traits/make_const_lvalue_ref.h
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H
10+
#define _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H
11+
12+
#include <__config>
13+
#include <__type_traits/is_function.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
#if _LIBCPP_STD_VER >= 26 && __has_builtin(__builtin_is_within_lifetime)
22+
template <class _Tp>
23+
_LIBCPP_HIDE_FROM_ABI inline consteval bool is_within_lifetime(const _Tp* __p) noexcept {
24+
if constexpr (is_function_v<_Tp>) {
25+
// Avoid multiple diagnostics
26+
static_assert(!is_function_v<_Tp>, "std::is_within_lifetime<T> cannot explicitly specify T as a function type");
27+
return true;
28+
} else {
29+
return __builtin_is_within_lifetime(__p);
30+
}
31+
}
32+
#endif
33+
34+
_LIBCPP_END_NAMESPACE_STD
35+
36+
#endif // _LIBCPP___TYPE_TRAITS_IS_WITHIN_LIFETIME_H

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,7 @@ module std_private_type_traits_is_void [system
20082008
export std_private_type_traits_integral_constant
20092009
}
20102010
module std_private_type_traits_is_volatile [system] { header "__type_traits/is_volatile.h" }
2011+
module std_private_type_traits_is_within_lifetime [system] { header "__type_traits/is_within_lifetime.h" }
20112012
module std_private_type_traits_lazy [system] { header "__type_traits/lazy.h" }
20122013
module std_private_type_traits_make_32_64_or_128_bit [system] { header "__type_traits/make_32_64_or_128_bit.h" }
20132014
module std_private_type_traits_make_const_lvalue_ref [system] { header "__type_traits/make_const_lvalue_ref.h" }

libcxx/include/type_traits

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ namespace std
416416
template<class B>
417417
inline constexpr bool negation_v = negation<B>::value; // C++17
418418
419+
// [meta.const.eval], constant evaluation context
420+
constexpr bool is_constant_evaluated() noexcept; // C++20
421+
template<class T>
422+
consteval bool is_within_lifetime(const T*) noexcept; // C++26
419423
}
420424
421425
*/
@@ -517,6 +521,10 @@ namespace std
517521
# include <__type_traits/unwrap_ref.h>
518522
#endif
519523

524+
#if _LIBCPP_STD_VER >= 26
525+
# include <__type_traits/is_within_lifetime.h>
526+
#endif
527+
520528
#include <version>
521529

522530
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/version

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,9 @@ __cpp_lib_void_t 201411L <type_traits>
539539
# if __has_builtin(__builtin_is_virtual_base_of)
540540
# define __cpp_lib_is_virtual_base_of 202406L
541541
# endif
542-
// # define __cpp_lib_is_within_lifetime 202306L
542+
# if __has_builtin(__builtin_is_within_lifetime)
543+
# define __cpp_lib_is_within_lifetime 202306L
544+
# endif
543545
// # define __cpp_lib_linalg 202311L
544546
# undef __cpp_lib_mdspan
545547
# define __cpp_lib_mdspan 202406L

libcxx/modules/std/type_traits.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ export namespace std {
314314

315315
// [meta.const.eval], constant evaluation context
316316
using std::is_constant_evaluated;
317+
#if _LIBCPP_STD_VER >= 26 && __has_builtin(__builtin_is_within_lifetime)
318+
using std::is_within_lifetime;
319+
#endif
317320

318321
// [depr.meta.types]
319322
using std::aligned_storage;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03
10+
11+
// <type_traits>
12+
13+
// LWG4138 <https://cplusplus.github.io/LWG/issue4138>
14+
// std::is_within_lifetime shouldn't work when a function type is
15+
// explicitly specified, even if it isn't evaluated
16+
17+
#include <type_traits>
18+
#include <cassert>
19+
20+
#include "test_macros.h"
21+
22+
void fn();
23+
24+
int main(int, char**) {
25+
#ifdef __cpp_lib_is_within_lifetime
26+
constexpr bool _ = true ? false : std::is_within_lifetime<void()>(&fn);
27+
// expected-error@*:* {{static assertion failed due to requirement '!is_function_v<void ()>': std::is_within_lifetime<T> cannot explicitly specify T as a function type}}
28+
#else
29+
// expected-no-diagnostics
30+
#endif
31+
return 0;
32+
}

0 commit comments

Comments
 (0)