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

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.

Some uses in std::vector and std::string can only be constexpr when the
std::is_constant_evaluated() function actually works, so we might as
well guard them with a relevant macro and call that function directly,
rather than the built-in or std::__is_constant_evaluated().

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 some kind of preprocessor check
there anyway.

libstdc++-v3/ChangeLog:

	* doc/doxygen/user.cfg.in (PREDEFINED): Change macro name.
	* 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/basic_string.h: Check new macro. call
	std::is_constant_evaluated() directly in C++20-only code that is
	guarded by a suitable macro.
	* include/bits/basic_string.tcc: Likewise.
	* include/bits/c++config (__is_constant_evaluated): Define.
	(_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED): Replace with ...
	(_GLIBCXX_HAVE_IS_CONSTANT_EVALUATED): New macro.
	* include/bits/char_traits.h (char_traits): Replace conditional
	calls to std::is_constant_evaluated with unconditional calls to
	std::__is_constant_evaluated.
	* include/bits/cow_string.h: Use new macro.
	* include/bits/ranges_algobase.h (__copy_or_move): Replace
	conditional calls to std::is_constant_evaluated with unconditional
	calls to std::__is_constant_evaluated.
	(__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_bvector.h: Call std::is_constant_evaluated
	directly in C++20-only code that is guarded by a suitable macro.
	* include/bits/stl_construct.h (_Construct, _Destroy, _Destroy_n):
	Replace is_constant_evaluated with __is_constant_evaluated.
	* include/bits/stl_function.h (greater, less, greater_equal)
	(less_equal): Replace __builtin_is_constant_evaluated and
	__builtin_constant_p with __is_constant_evaluated.
	* include/bits/stl_vector.h: Call std::is_constant_evaluated()
	in C++20-only code.
	* include/debug/helper_functions.h (__check_singular): Use
	__is_constant_evaluated instead of built-in, or remove check
	entirely.
	* include/std/array (operator<=>): Use __is_constant_evaluated
	unconditionally.
	* include/std/bit (__bit_ceil): Likewise.
	* include/std/type_traits (is_constant_evaluated): Define using
	'if consteval' if possible.
	* include/std/version: Use new macro.
	* libsupc++/compare: Use __is_constant_evaluated instead of
	__builtin_is_constant_evaluated.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc:
	Adjust dg-error lines.
This commit is contained in:
Jonathan Wakely 2021-11-26 23:25:03 +00:00
parent 2b83bc6097
commit 74d14778e7
21 changed files with 142 additions and 157 deletions

View File

@ -2399,7 +2399,7 @@ PREDEFINED = __cplusplus=202002L \
"__has_builtin(x)=1" \
_GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP \
_GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE \
_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED \
_GLIBCXX_HAVE_IS_CONSTANT_EVALUATED \
_GLIBCXX_HAVE_BUILTIN_LAUNDER \
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this

View File

@ -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

View File

@ -59,7 +59,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
#ifdef __cpp_lib_is_constant_evaluated
// Support P0980R1 in C++20.
# define __cpp_lib_constexpr_string 201907L
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
// Support P0426R1 changes to char_traits in C++17.
# define __cpp_lib_constexpr_string 201611L
#endif
@ -101,7 +101,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
allocate(_Char_alloc_type& __a, typename _Base::size_type __n)
{
pointer __p = _Base::allocate(__a, __n);
if (__builtin_is_constant_evaluated())
if (std::is_constant_evaluated())
// Begin the lifetime of characters in allocated storage.
for (size_type __i = 0; __i < __n; ++__i)
std::construct_at(__builtin_addressof(__p[__i]));
@ -352,7 +352,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_M_use_local_data() _GLIBCXX_NOEXCEPT
{
#if __cpp_lib_is_constant_evaluated
if (__builtin_is_constant_evaluated())
if (std::is_constant_evaluated())
_M_local_buf[0] = _CharT();
#endif
return _M_local_data();

View File

@ -488,7 +488,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const size_type __how_much = __old_size - __pos - __len1;
#if __cpp_lib_is_constant_evaluated
if (__builtin_is_constant_evaluated())
if (std::is_constant_evaluated())
{
auto __newp = _Alloc_traits::allocate(_M_get_allocator(),
__new_size);
@ -571,7 +571,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__p = _M_create(__n, __capacity);
this->_S_copy(__p, _M_data(), length()); // exclude trailing null
#if __cpp_lib_is_constant_evaluated
if (__builtin_is_constant_evaluated())
if (std::is_constant_evaluated())
traits_type::assign(__p + length(), __n - length(), _CharT());
#endif
_M_dispose();

View File

@ -495,6 +495,27 @@ namespace std
#endif // _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && IEEE128
namespace std
{
// Internal version of std::is_constant_evaluated().
// This can be used without checking if the compiler supports the feature.
// The macro _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED can be used to check if
// the compiler support is present to make this function work as expected.
_GLIBCXX_CONSTEXPR inline bool
__is_constant_evaluated() _GLIBCXX_NOEXCEPT
{
#if __cpp_if_consteval >= 202106L
# define _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED 1
if consteval { return true; } else { return false; }
#elif __cplusplus >= 201103L && __has_builtin(__builtin_is_constant_evaluated)
# define _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED 1
return __builtin_is_constant_evaluated();
#else
return false;
#endif
}
}
// Debug Mode implies checking assertions.
#if defined(_GLIBCXX_DEBUG) && !defined(_GLIBCXX_ASSERTIONS)
# define _GLIBCXX_ASSERTIONS 1
@ -507,9 +528,9 @@ namespace std
#endif
#if __has_builtin(__builtin_is_constant_evaluated)
#if _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
# define __glibcxx_constexpr_assert(cond) \
if (__builtin_is_constant_evaluated() && !bool(cond)) \
if (std::__is_constant_evaluated() && !bool(cond)) \
__builtin_unreachable() /* precondition violation detected! */
#else
# define __glibcxx_constexpr_assert(unevaluated)
@ -762,10 +783,6 @@ namespace std
# define _GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE 1
#endif
#if _GLIBCXX_HAS_BUILTIN(__builtin_is_constant_evaluated)
# define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
#endif
#if _GLIBCXX_HAS_BUILTIN(__is_same)
# define _GLIBCXX_HAVE_BUILTIN_IS_SAME 1
#endif
@ -776,7 +793,6 @@ namespace std
#undef _GLIBCXX_HAS_BUILTIN
// PSTL configuration
#if __cplusplus >= 201703L

View File

@ -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);
@ -299,7 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifdef __cpp_lib_is_constant_evaluated
// Unofficial macro indicating P1032R1 support in C++20
# define __cpp_lib_constexpr_char_traits 201811L
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
// Unofficial macro indicating P0426R1 support in C++17
# define __cpp_lib_constexpr_char_traits 201611L
#endif
@ -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*>

View File

@ -37,7 +37,7 @@
#ifdef __cpp_lib_is_constant_evaluated
// Support P1032R1 in C++20 (but not P0980R1 for COW strings).
# define __cpp_lib_constexpr_string 201811L
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
// Support P0426R1 changes to char_traits in C++17.
# define __cpp_lib_constexpr_string 201611L
#endif

View File

@ -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),

View File

@ -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__>(

View File

@ -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)

View File

@ -625,8 +625,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_M_allocate(size_t __n)
{
_Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n));
#if __has_builtin(__builtin_is_constant_evaluated)
if (__builtin_is_constant_evaluated())
#if __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated())
{
__n = _S_nword(__n);
for (size_t __i = 0; __i < __n; ++__i)
@ -1524,8 +1524,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
inline void
__fill_bvector_n(_Bit_type* __p, size_t __n, bool __x) _GLIBCXX_NOEXCEPT
{
#if __has_builtin(__builtin_is_constant_evaluated)
if (__builtin_is_constant_evaluated())
#if __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated())
{
for (size_t __i = 0; __i < __n; ++__i)
__p[__i] = __x ? ~0ul : 0ul;

View File

@ -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)>::

View File

@ -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;

View File

@ -195,8 +195,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
static _GLIBCXX20_CONSTEXPR void
_S_adjust(_Vector_impl& __impl, pointer __prev, pointer __curr)
{
#if __has_builtin(__builtin_is_constant_evaluated)
if (!__builtin_is_constant_evaluated())
#if __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated())
return;
#endif
__sanitizer_annotate_contiguous_container(__impl._M_start,
__impl._M_end_of_storage, __prev, __curr);

View File

@ -124,11 +124,8 @@ namespace __gnu_debug
inline bool
__check_singular(_Iterator const& __x)
{
return
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
__builtin_is_constant_evaluated() ? false :
#endif
__check_singular_aux(std::__addressof(__x));
return ! std::__is_constant_evaluated()
&& __check_singular_aux(std::__addressof(__x));
}
/** Non-NULL pointers are nonsingular. */
@ -136,13 +133,7 @@ namespace __gnu_debug
_GLIBCXX_CONSTEXPR
inline bool
__check_singular(_Tp* const& __ptr)
{
return
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
__builtin_is_constant_evaluated() ? false :
#endif
__ptr == 0;
}
{ return __ptr == 0; }
/** We say that integral types for a valid range, and defer to other
* routines to realize what to do with integral types instead of

View File

@ -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)
{

View File

@ -315,12 +315,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)
{

View File

@ -3510,15 +3510,20 @@ template<typename _Ret, typename _Fn, typename... _Args>
#endif // C++23
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
#if _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
#define __cpp_lib_is_constant_evaluated 201811L
/// Returns true only when called during constant evaluation.
/// @since C++20
constexpr inline bool
is_constant_evaluated() noexcept
{ return __builtin_is_constant_evaluated(); }
{
#if __cpp_if_consteval >= 202106L
if consteval { return true; } else { return false; }
#else
return __builtin_is_constant_evaluated();
#endif
}
#endif
/// @cond undocumented

View File

@ -130,7 +130,7 @@
#define __cpp_lib_chrono 201611
#define __cpp_lib_clamp 201603
#if __cplusplus == 201703L // N.B. updated value in C++20
# if _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
# if _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
# define __cpp_lib_constexpr_char_traits 201611L
# define __cpp_lib_constexpr_string 201611L
# endif
@ -195,7 +195,7 @@
#endif
#define __cpp_lib_endian 201907L
#define __cpp_lib_int_pow2 202002L
#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
#ifdef _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
# define __cpp_lib_is_constant_evaluated 201811L
#endif
#define __cpp_lib_is_nothrow_convertible 201806L

View File

@ -555,7 +555,7 @@ namespace std
{
auto __pt = static_cast<const volatile void*>(__t);
auto __pu = static_cast<const volatile void*>(__u);
if (__builtin_is_constant_evaluated())
if (std::__is_constant_evaluated())
return __pt <=> __pu;
auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt);
auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu);

View File

@ -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 }