diff mbox series

libstdc++: Define std::__is_constant_evaluated() for internal use

Message ID 20211126122925.1626170-1-jwakely@redhat.com
State Superseded
Headers show
Series libstdc++: Define std::__is_constant_evaluated() for internal use | expand

Commit Message

Jonathan Wakely Nov. 26, 2021, 12:29 p.m. UTC
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(-)

Comments

Ville Voutilainen Nov. 26, 2021, 12:38 p.m. UTC | #1
On Fri, 26 Nov 2021 at 14:29, Jonathan Wakely via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> 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?

None here, I like this improvement.
Jakub Jelinek Nov. 26, 2021, 12:39 p.m. UTC | #2
On Fri, Nov 26, 2021 at 12:29:25PM +0000, Jonathan Wakely via Gcc-patches wrote:
> +  // 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
> +  {

When you have such a nice one spot, shouldn't it:
#if __cpp_if_consteval >= 202106L 
  if consteval
    {
      return true;
    }
  else
    {
      return false;
    }
#elif __has_builtin(__builtin_is_constant_evaluated)
...

Theoretically not all compilers need to support the builtin and in C++23
mode if consteval should be slightly more efficient.

One disadvantage of std::__is_constant_evaluated() is that Marek's
warning for if constexpr (std::is_constant_evaluated()) will not trigger
if __is_constant_evaluated() is used instead.  But I'd hope testsuite
coverage would discover it quickly if such bug would appear...

	Jakub
Jonathan Wakely Nov. 26, 2021, 12:43 p.m. UTC | #3
On Fri, 26 Nov 2021 at 12:39, Jakub Jelinek wrote:
>
> On Fri, Nov 26, 2021 at 12:29:25PM +0000, Jonathan Wakely via Gcc-patches wrote:
> > +  // 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
> > +  {
>
> When you have such a nice one spot, shouldn't it:
> #if __cpp_if_consteval >= 202106L
>   if consteval
>     {
>       return true;
>     }
>   else
>     {
>       return false;
>     }
> #elif __has_builtin(__builtin_is_constant_evaluated)
> ...
>
> Theoretically not all compilers need to support the builtin and in C++23
> mode if consteval should be slightly more efficient.

Yes, good idea. We actually still have two spots, because we still
have std::is_constant_evaluated as well, which is only defined if it
actually works. But we can use the same implementation in there (or
make it call std::__is_constant_evaluated()). I'll prepare a new patch
soon.

> One disadvantage of std::__is_constant_evaluated() is that Marek's
> warning for if constexpr (std::is_constant_evaluated()) will not trigger
> if __is_constant_evaluated() is used instead.  But I'd hope testsuite
> coverage would discover it quickly if such bug would appear...

Yes, and I hope I won't make that mistake anyway, or miss it in reviews.

It's a good warning for users, but I like to think I don't need it
(and now we start the sweepstake on how many days until I push a patch
making exactly that mistake ;-)
Jakub Jelinek Nov. 26, 2021, 12:49 p.m. UTC | #4
On Fri, Nov 26, 2021 at 12:43:44PM +0000, Jonathan Wakely wrote:
> > On Fri, Nov 26, 2021 at 12:29:25PM +0000, Jonathan Wakely via Gcc-patches wrote:
> > > +  // 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
> > > +  {
> >
> > When you have such a nice one spot, shouldn't it:
> > #if __cpp_if_consteval >= 202106L
> >   if consteval
> >     {
> >       return true;
> >     }
> >   else
> >     {
> >       return false;
> >     }
> > #elif __has_builtin(__builtin_is_constant_evaluated)
> > ...
> >
> > Theoretically not all compilers need to support the builtin and in C++23
> > mode if consteval should be slightly more efficient.
> 
> Yes, good idea. We actually still have two spots, because we still
> have std::is_constant_evaluated as well, which is only defined if it
> actually works. But we can use the same implementation in there (or
> make it call std::__is_constant_evaluated()). I'll prepare a new patch
> soon.

While calling the latter might be more maintainable, I think having it
implemented twice would be better, so that std::is_constant_evaluated()
doesn't need to hop through another inline call.
I'd expect people to use std::is_constant_evaluated() in their code quite a
lot, it will take time until they start to use if consteval instead.

	Jakub
diff mbox series

Patch

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 <bits/postypes.h>      // For streampos
 #include <cwchar>               // For WEOF, wmemmove, wmemset, etc.
+#if __cplusplus >= 201103L
+# include <type_traits>
+#endif
 #if __cplusplus >= 202002L
 # include <compare>
 # include <bits/stl_construct.h>
@@ -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<char_type>::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<char_type>::find(__s, __n, __a);
 #endif
 	return static_cast<const char_type*>(__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<char_type>::move(__s1, __s2, __n);
 #endif
 	return static_cast<char_type*>(__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<char_type>::copy(__s1, __s2, __n);
 #endif
 	return static_cast<char_type*>(__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<char_type>::assign(__s, __n, __a);
 #endif
 	return static_cast<char_type*>(__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<char_type>::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<char_type>::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<char_type>::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<char_type>::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<char_type>::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<char_type>::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<char_type>::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<char_type>::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<char_type>::find(__s, __n, __a);
 #endif
 	return static_cast<const char_type*>(__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<char_type>::move(__s1, __s2, __n);
 #endif
 	return static_cast<char_type*>(__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<char_type>::copy(__s1, __s2, __n);
 #endif
 	return static_cast<char_type*>(__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<char_type>::assign(__s, __n, __a);
 #endif
 	return static_cast<char_type*>(__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<char_type>::move(__s1, __s2, __n);
 #endif
 	return (static_cast<char_type*>
@@ -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<char_type>::copy(__s1, __s2, __n);
 #endif
 	return (static_cast<char_type*>
@@ -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<char_type>::move(__s1, __s2, __n);
 #endif
 	return (static_cast<char_type*>
@@ -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<char_type>::copy(__s1, __s2, __n);
 #endif
 	return (static_cast<char_type*>
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<remove_pointer_t<_Out>>::__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<unsigned char>(__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<const volatile void*>(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<common_comparison_category_t<_Cat>, _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<false>::__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<false>::__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<typename _Ret, typename _Fn, typename... _Args>
 
 #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 }