From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 73847385841D for ; Fri, 26 Nov 2021 12:29:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 73847385841D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-464--DUJ7syCPI-k7coUdg0Qjw-1; Fri, 26 Nov 2021 07:29:27 -0500 X-MC-Unique: -DUJ7syCPI-k7coUdg0Qjw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F10B81926DA0; Fri, 26 Nov 2021 12:29:26 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6909760BE5; Fri, 26 Nov 2021 12:29:26 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Define std::__is_constant_evaluated() for internal use Date: Fri, 26 Nov 2021 12:29:25 +0000 Message-Id: <20211126122925.1626170-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2021 12:29:32 -0000 I've bored of having to do preprocessor checks before using is_constant_evaluated, so I've come up with this approach. Anybody got a better idea, or objections to this? An alternative to __is_constant_evaluated would be to define a function called __builtin_is_constant_evaluated when it isn't supported by the compiler, but that feels a bit icky. We shouldn't be using the compiler's "__builtin_" prefix for an ordinary function defined in the library. ... This adds std::__is_constant_evaluated() as a C++11 wrapper for __builtin_is_constant_evaluated, but just returning false if the built-in isn't supported by the compiler. This allows us to use it throughout the library without checking __has_builtin every time. The remaining checks of the __cpp_lib_is_constant_evaluated macro could now be replaced by checking __cplusplus >= 202002 instead, but there's no practical difference. We still need a preprocessor check in those cases. libstdc++-v3/ChangeLog: * include/bits/allocator.h (allocate, deallocate): Use std::__is_constant_evaluated() unconditionally, instead of checking whether std::is_constant_evaluated() (or the built-in) can be used. * include/bits/char_traits.h (char_traits): Likewise. * include/bits/ranges_algobase.h (__copy_or_move): Likewise. (__copy_or_move_backward, __fill_n_fn): Likewise. * include/bits/ranges_cmp.h (ranges::less): Likewise. * include/bits/stl_algobase.h (lexicographical_compare_three_way): Likewise. * include/bits/stl_construct.h (_Construct, _Destroy, _Destroy_n): Likewise. * include/bits/stl_function.h (greater, less, greater_equal) (less_equal): Likewise. * include/std/array (operator<=>): Likewise. * include/std/bit (__bit_ceil): Likewise. * include/std/type_traits (__is_constant_evaluated): Define new function for internal use. * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust dg-error lines. --- libstdc++-v3/include/bits/allocator.h | 8 +- libstdc++-v3/include/bits/char_traits.h | 127 +++++++++--------- libstdc++-v3/include/bits/ranges_algobase.h | 12 +- libstdc++-v3/include/bits/ranges_cmp.h | 5 +- libstdc++-v3/include/bits/stl_algobase.h | 5 +- libstdc++-v3/include/bits/stl_construct.h | 12 +- libstdc++-v3/include/bits/stl_function.h | 24 +--- libstdc++-v3/include/std/array | 4 +- libstdc++-v3/include/std/bit | 5 +- libstdc++-v3/include/std/type_traits | 12 ++ .../array/tuple_interface/get_neg.cc | 6 +- 11 files changed, 102 insertions(+), 118 deletions(-) diff --git a/libstdc++-v3/include/bits/allocator.h b/libstdc++-v3/include/bits/allocator.h index f83e6b87666..1c099514e18 100644 --- a/libstdc++-v3/include/bits/allocator.h +++ b/libstdc++-v3/include/bits/allocator.h @@ -178,10 +178,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr _Tp* allocate(size_t __n) { -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) + if (std::__is_constant_evaluated()) return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); -#endif return __allocator_base<_Tp>::allocate(__n, 0); } @@ -189,13 +187,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr void deallocate(_Tp* __p, size_t __n) { -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) + if (std::__is_constant_evaluated()) { ::operator delete(__p); return; } -#endif __allocator_base<_Tp>::deallocate(__p, __n); } #endif // C++20 diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h index b1cdc55ea61..e6afcc72860 100644 --- a/libstdc++-v3/include/bits/char_traits.h +++ b/libstdc++-v3/include/bits/char_traits.h @@ -38,6 +38,9 @@ #include // For streampos #include // For WEOF, wmemmove, wmemset, etc. +#if __cplusplus >= 201103L +# include +#endif #if __cplusplus >= 202002L # include # include @@ -101,8 +104,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX14_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -199,8 +202,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#if __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) { if (__s1 == __s2) // unlikely, but saves a lot of work return __s1; @@ -247,8 +250,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: copy(char_type* __s1, const char_type* __s2, std::size_t __n) { -#if __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) { for (std::size_t __i = 0; __i < __n; ++__i) std::construct_at(__s1 + __i, __s2[__i]); @@ -266,8 +269,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: assign(char_type* __s, std::size_t __n, char_type __a) { -#if __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) { for (std::size_t __i = 0; __i < __n; ++__i) std::construct_at(__s + __i, __a); @@ -338,8 +341,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -363,8 +366,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) { for (size_t __i = 0; __i < __n; ++__i) if (lt(__s1[__i], __s2[__i])) @@ -380,8 +383,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR size_t length(const char_type* __s) { -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::length(__s); #endif return __builtin_strlen(__s); @@ -392,8 +395,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::find(__s, __n, __a); #endif return static_cast(__builtin_memchr(__s, __a, __n)); @@ -404,8 +407,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::move(__s1, __s2, __n); #endif return static_cast(__builtin_memmove(__s1, __s2, __n)); @@ -416,8 +419,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::copy(__s1, __s2, __n); #endif return static_cast(__builtin_memcpy(__s1, __s2, __n)); @@ -428,8 +431,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::assign(__s, __n, __a); #endif return static_cast(__builtin_memset(__s, __a, __n)); @@ -476,8 +479,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -497,8 +500,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::compare(__s1, __s2, __n); #endif return wmemcmp(__s1, __s2, __n); @@ -507,8 +510,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR size_t length(const char_type* __s) { -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::length(__s); #endif return wcslen(__s); @@ -519,8 +522,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::find(__s, __n, __a); #endif return wmemchr(__s, __a, __n); @@ -531,8 +534,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::move(__s1, __s2, __n); #endif return wmemmove(__s1, __s2, __n); @@ -543,8 +546,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::copy(__s1, __s2, __n); #endif return wmemcpy(__s1, __s2, __n); @@ -555,8 +558,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::assign(__s, __n, __a); #endif return wmemset(__s, __a, __n); @@ -604,8 +607,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -625,8 +628,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::compare(__s1, __s2, __n); #endif return __builtin_memcmp(__s1, __s2, __n); @@ -635,8 +638,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR size_t length(const char_type* __s) { -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::length(__s); #endif size_t __i = 0; @@ -650,8 +653,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return 0; -#if __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 201703L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::find(__s, __n, __a); #endif return static_cast(__builtin_memchr(__s, __a, __n)); @@ -662,8 +665,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::move(__s1, __s2, __n); #endif return static_cast(__builtin_memmove(__s1, __s2, __n)); @@ -674,8 +677,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::copy(__s1, __s2, __n); #endif return static_cast(__builtin_memcpy(__s1, __s2, __n)); @@ -686,8 +689,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::assign(__s, __n, __a); #endif return static_cast(__builtin_memset(__s, __a, __n)); @@ -747,8 +750,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) noexcept { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -797,8 +800,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::move(__s1, __s2, __n); #endif return (static_cast @@ -810,8 +813,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::copy(__s1, __s2, __n); #endif return (static_cast @@ -868,8 +871,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) noexcept { -#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cpp_constexpr_dynamic_alloc + if (std::__is_constant_evaluated()) std::construct_at(__builtin_addressof(__c1), __c2); else #endif @@ -918,8 +921,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::move(__s1, __s2, __n); #endif return (static_cast @@ -931,8 +934,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__n == 0) return __s1; -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return __gnu_cxx::char_traits::copy(__s1, __s2, __n); #endif return (static_cast diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index 9929e5e828b..74b8e9c4d59 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -251,9 +251,7 @@ namespace ranges } else if constexpr (sized_sentinel_for<_Sent, _Iter>) { -#ifdef __cpp_lib_is_constant_evaluated - if (!std::is_constant_evaluated()) -#endif + if (!std::__is_constant_evaluated()) { if constexpr (__memcpyable<_Iter, _Out>::__value) { @@ -388,9 +386,7 @@ namespace ranges } else if constexpr (sized_sentinel_for<_Sent, _Iter>) { -#ifdef __cpp_lib_is_constant_evaluated - if (!std::is_constant_evaluated()) -#endif + if (!std::__is_constant_evaluated()) { if constexpr (__memcpyable<_Out, _Iter>::__value) { @@ -535,9 +531,7 @@ namespace ranges && __is_byte>::__value && integral<_Tp>) { -#ifdef __cpp_lib_is_constant_evaluated - if (!std::is_constant_evaluated()) -#endif + if (!std::__is_constant_evaluated()) { __builtin_memset(__first, static_cast(__value), diff --git a/libstdc++-v3/include/bits/ranges_cmp.h b/libstdc++-v3/include/bits/ranges_cmp.h index 1d7da30dddf..098022e63a2 100644 --- a/libstdc++-v3/include/bits/ranges_cmp.h +++ b/libstdc++-v3/include/bits/ranges_cmp.h @@ -119,10 +119,9 @@ namespace ranges { if constexpr (__detail::__less_builtin_ptr_cmp<_Tp, _Up>) { -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) + if (std::__is_constant_evaluated()) return __t < __u; -#endif + auto __x = reinterpret_cast<__UINTPTR_TYPE__>( static_cast(std::forward<_Tp>(__t))); auto __y = reinterpret_cast<__UINTPTR_TYPE__>( diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index f441165714b..984a5bf94b9 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1826,11 +1826,10 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO __glibcxx_requires_valid_range(__first1, __last1); __glibcxx_requires_valid_range(__first2, __last2); -#if __cpp_lib_is_constant_evaluated using _Cat = decltype(__comp(*__first1, *__first2)); static_assert(same_as, _Cat>); - if (!std::is_constant_evaluated()) + if (!std::__is_constant_evaluated()) if constexpr (same_as<_Comp, __detail::_Synth3way> || same_as<_Comp, compare_three_way>) if constexpr (__is_byte_iter<_InputIter1>) @@ -1847,7 +1846,7 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO } return __lencmp; } -#endif // is_constant_evaluated + while (__first1 != __last1) { if (__first2 == __last2) diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h index e53ed0d9f91..dd621f24bc2 100644 --- a/libstdc++-v3/include/bits/stl_construct.h +++ b/libstdc++-v3/include/bits/stl_construct.h @@ -108,8 +108,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void _Construct(_Tp* __p, _Args&&... __args) { -#if __cplusplus >= 202002L && __has_builtin(__builtin_is_constant_evaluated) - if (__builtin_is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) { // Allow std::_Construct to be used in constant expressions. std::construct_at(__p, std::forward<_Args>(__args)...); @@ -188,8 +188,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_destructible<_Value_type>::value, "value type is destructible"); #endif -#if __cplusplus > 201703L && defined __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return _Destroy_aux::__destroy(__first, __last); #endif std::_Destroy_aux<__has_trivial_destructor(_Value_type)>:: @@ -237,8 +237,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_destructible<_Value_type>::value, "value type is destructible"); #endif -#if __cplusplus > 201703L && defined __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#if __cplusplus >= 202002L + if (std::__is_constant_evaluated()) return _Destroy_n_aux::__destroy_n(__first, __count); #endif return std::_Destroy_n_aux<__has_trivial_destructor(_Value_type)>:: diff --git a/libstdc++-v3/include/bits/stl_function.h b/libstdc++-v3/include/bits/stl_function.h index 5de8c3234f7..92e9dc75a80 100644 --- a/libstdc++-v3/include/bits/stl_function.h +++ b/libstdc++-v3/include/bits/stl_function.h @@ -428,11 +428,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW { #if __cplusplus >= 201402L -#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) -#else - if (__builtin_constant_p(__x > __y)) -#endif + if (std::__is_constant_evaluated()) return __x > __y; #endif return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y; @@ -447,11 +443,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW { #if __cplusplus >= 201402L -#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) -#else - if (__builtin_constant_p(__x < __y)) -#endif + if (std::__is_constant_evaluated()) return __x < __y; #endif return (__UINTPTR_TYPE__)__x < (__UINTPTR_TYPE__)__y; @@ -466,11 +458,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW { #if __cplusplus >= 201402L -#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) -#else - if (__builtin_constant_p(__x >= __y)) -#endif + if (std::__is_constant_evaluated()) return __x >= __y; #endif return (__UINTPTR_TYPE__)__x >= (__UINTPTR_TYPE__)__y; @@ -485,11 +473,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW { #if __cplusplus >= 201402L -#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (__builtin_is_constant_evaluated()) -#else - if (__builtin_constant_p(__x <= __y)) -#endif + if (std::__is_constant_evaluated()) return __x <= __y; #endif return (__UINTPTR_TYPE__)__x <= (__UINTPTR_TYPE__)__y; diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 413f8e2be01..e632de15aff 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -310,14 +310,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr __detail::__synth3way_t<_Tp> operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) { -#ifdef __cpp_lib_is_constant_evaluated if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value) - if (!std::is_constant_evaluated()) + if (!std::__is_constant_evaluated()) { constexpr size_t __n = _Nm * sizeof(_Tp); return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0; } -#endif for (size_t __i = 0; __i < _Nm; ++__i) { diff --git a/libstdc++-v3/include/std/bit b/libstdc++-v3/include/std/bit index c5aae8bab03..cc73bcfec6b 100644 --- a/libstdc++-v3/include/std/bit +++ b/libstdc++-v3/include/std/bit @@ -265,12 +265,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // representable as a value of _Tp, and so the result is undefined. // Want that undefined behaviour to be detected in constant expressions, // by UBSan, and by debug assertions. -#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED - if (!__builtin_is_constant_evaluated()) + if (!std::__is_constant_evaluated()) { __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits ); } -#endif + using __promoted_type = decltype(__x << 1); if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value) { diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index d3693b1069e..cf4abe58e90 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3693,6 +3693,18 @@ template #endif // C++2a + // Internal version of std::is_constant_evaluated() for C++11. + // This can be used without checking if the compiler supports the built-in. + constexpr inline bool + __is_constant_evaluated() noexcept + { +#if __has_builtin(__builtin_is_constant_evaluated) + return __builtin_is_constant_evaluated(); +#else + return false; +#endif + } + /// @} group metaprogramming _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc index 70742c14a7d..776ff5f771f 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc @@ -26,6 +26,6 @@ int n1 = std::get<1>(a); int n2 = std::get<1>(std::move(a)); int n3 = std::get<1>(ca); -// { dg-error "static assertion failed" "" { target *-*-* } 398 } -// { dg-error "static assertion failed" "" { target *-*-* } 407 } -// { dg-error "static assertion failed" "" { target *-*-* } 416 } +// { dg-error "static assertion failed" "" { target *-*-* } 396 } +// { dg-error "static assertion failed" "" { target *-*-* } 405 } +// { dg-error "static assertion failed" "" { target *-*-* } 414 } -- 2.31.1