Skip to content

Commit 4bfddda

Browse files
Switch to use internal type trait
1 parent 417b08a commit 4bfddda

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

libcxx/include/__type_traits/reference_constructs_from_temporary.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool reference_constructs_from_tempo
3030

3131
#endif
3232

33+
#if __has_builtin(__reference_constructs_from_temporary)
34+
template <class _Tp, class _Up>
35+
inline const bool __reference_constructs_from_temporary_v = __reference_constructs_from_temporary(_Tp, _Up);
36+
#elif __has_builtin(__reference_binds_to_temporary)
37+
// TODO: Remove this once all support compilers have __reference_constructs_from_temporary implemented.
38+
template <class _Tp, class _Up>
39+
inline const bool __reference_constructs_from_temporary_v = __reference_binds_to_temporary(_Tp, _Up);
40+
#else
41+
// TODO: Remove this once https://github.com/llvm/llvm-project/issues/111477 no longer affects supported compilers.
42+
template <class _Tp, class _Up>
43+
inline const bool __reference_constructs_from_temporary_v = false;
44+
#endif
45+
3346
_LIBCPP_END_NAMESPACE_STD
3447

3548
#endif // _LIBCPP___TYPE_TRAITS_REFERENCE_CONSTRUCTS_FROM_TEMPORARY_H

libcxx/include/tuple

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ template <class... Types>
258258
# include <__type_traits/maybe_const.h>
259259
# include <__type_traits/nat.h>
260260
# include <__type_traits/negation.h>
261+
# include <__type_traits/reference_constructs_from_temporary.h>
261262
# include <__type_traits/remove_cv.h>
262263
# include <__type_traits/remove_cvref.h>
263264
# include <__type_traits/remove_reference.h>
@@ -308,17 +309,6 @@ template <size_t _Ip, class _Hp, bool>
308309
class __tuple_leaf {
309310
_Hp __value_;
310311

311-
template <class _Tp>
312-
static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() {
313-
# if __has_builtin(__reference_constructs_from_temporary)
314-
return !__reference_constructs_from_temporary(_Hp, _Tp);
315-
# elif __has_keyword(__reference_binds_to_temporary)
316-
return !__reference_binds_to_temporary(_Hp, _Tp);
317-
# else
318-
return true;
319-
# endif
320-
}
321-
322312
public:
323313
_LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_leaf& operator=(const __tuple_leaf&) = delete;
324314

@@ -348,15 +338,15 @@ public:
348338
_LIBCPP_HIDE_FROM_ABI
349339
_LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(_Tp&& __t) noexcept(is_nothrow_constructible<_Hp, _Tp>::value)
350340
: __value_(std::forward<_Tp>(__t)) {
351-
static_assert(__can_bind_reference<_Tp&&>(),
341+
static_assert(!__reference_constructs_from_temporary_v<_Hp, _Tp&&>,
352342
"Attempted construction of reference element binds to a temporary whose lifetime has ended");
353343
}
354344

355345
template <class _Tp, class _Alloc>
356346
_LIBCPP_HIDE_FROM_ABI
357347
_LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
358348
: __value_(std::forward<_Tp>(__t)) {
359-
static_assert(__can_bind_reference<_Tp&&>(),
349+
static_assert(!__reference_constructs_from_temporary_v<_Hp, _Tp&&>,
360350
"Attempted construction of reference element binds to a temporary whose lifetime has ended");
361351
}
362352

libcxx/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.verify.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ template <class T> struct CannotDeduce {
3838
template <class ...Args>
3939
void F(typename CannotDeduce<std::tuple<Args...>>::type const&) {}
4040

41+
#if TEST_HAS_BUILTIN(__reference_constructs_from_temporary)
42+
# define TEST_HAS_REFERENCE_BINDING_TRAIT 1
43+
#elif TEST_HAS_BUILTIN(__reference_binds_to_temporary)
44+
# define TEST_HAS_REFERENCE_BINDING_TRAIT 1
45+
#else
46+
# define TEST_HAS_REFERENCE_BINDING_TRAIT 0
47+
#endif
4148

4249
void f() {
43-
#if TEST_HAS_BUILTIN_IDENTIFIER(__reference_constructs_from_temporary)
50+
#if TEST_HAS_REFERENCE_BINDING_TRAIT
4451
// Test that we emit our diagnostic from the library.
4552
// expected-error@tuple:* 8 {{Attempted construction of reference element binds to a temporary whose lifetime has ended}}
4653

libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/PR20855_tuple_ref_binding_diagnostics.pass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
#include <cassert>
1919
#include "test_macros.h"
2020

21-
#if TEST_HAS_BUILTIN_IDENTIFIER(__reference_constructs_from_temporary)
21+
#if TEST_HAS_BUILTIN(__reference_constructs_from_temporary)
2222
# define ASSERT_REFERENCE_BINDS_TEMPORARY(...) static_assert(__reference_constructs_from_temporary(__VA_ARGS__), "")
2323
# define ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(...) \
2424
static_assert(!__reference_constructs_from_temporary(__VA_ARGS__), "")
25+
#elif TEST_HAS_BUILTIN(__reference_binds_to_temporary)
26+
# define ASSERT_REFERENCE_BINDS_TEMPORARY(...) static_assert(__reference_binds_to_temporary(__VA_ARGS__), "")
27+
# define ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(...) static_assert(!__reference_binds_to_temporary(__VA_ARGS__), "")
2528
#else
2629
# define ASSERT_REFERENCE_BINDS_TEMPORARY(...) static_assert(true, "")
2730
# define ASSERT_NOT_REFERENCE_BINDS_TEMPORARY(...) static_assert(true, "")

0 commit comments

Comments
 (0)