libstdc++: Enable constexpr std::bitset for debug mode

We already disable all debug mode checks for C++11 and later, so we can
easily make everything constexpr. This fixes the FAIL results for the
new tests when using -D_GLIBCXX_DEBUG.

Also fix some other tests failing with non-default test flags.

libstdc++-v3/ChangeLog:

	* include/debug/bitset (__debug::bitset): Add constexpr to all
	member functions.
	(operator&, operator|, operator^): Add inline and constexpr.
	(operator>>, operator<<): Add inline.
	* testsuite/20_util/bitset/access/constexpr.cc: Only check using
	constexpr std::string for the cxx11 ABI.
	* testsuite/20_util/bitset/cons/constexpr_c++23.cc: Likewise.
	* testsuite/20_util/headers/bitset/synopsis.cc: Check constexpr
	for C++23.
This commit is contained in:
Jonathan Wakely 2022-09-23 10:12:09 +01:00
parent ffb03fa128
commit 4ceb5bc42d
4 changed files with 51 additions and 5 deletions

View file

@ -141,6 +141,7 @@ namespace __debug
: _Base(__val) { }
template<typename _CharT, typename _Traits, typename _Alloc>
_GLIBCXX23_CONSTEXPR
explicit
bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
@ -152,6 +153,7 @@ namespace __debug
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 396. what are characters zero and one.
template<class _CharT, class _Traits, class _Alloc>
_GLIBCXX23_CONSTEXPR
bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
__pos,
@ -160,10 +162,12 @@ namespace __debug
_CharT __zero, _CharT __one = _CharT('1'))
: _Base(__str, __pos, __n, __zero, __one) { }
_GLIBCXX23_CONSTEXPR
bitset(const _Base& __x) : _Base(__x) { }
#if __cplusplus >= 201103L
template<typename _CharT>
_GLIBCXX23_CONSTEXPR
explicit
bitset(const _CharT* __str,
typename std::basic_string<_CharT>::size_type __n
@ -173,6 +177,7 @@ namespace __debug
#endif
// 23.3.5.2 bitset operations:
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
{
@ -180,6 +185,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
{
@ -187,6 +193,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
{
@ -194,6 +201,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
{
@ -201,6 +209,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
{
@ -208,6 +217,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
set() _GLIBCXX_NOEXCEPT
{
@ -217,6 +227,7 @@ namespace __debug
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 186. bitset::set() second parameter should be bool
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
set(size_t __pos, bool __val = true)
{
@ -224,6 +235,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
reset() _GLIBCXX_NOEXCEPT
{
@ -231,6 +243,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
reset(size_t __pos)
{
@ -238,10 +251,12 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>
operator~() const _GLIBCXX_NOEXCEPT
{ return bitset(~_M_base()); }
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
flip() _GLIBCXX_NOEXCEPT
{
@ -249,6 +264,7 @@ namespace __debug
return *this;
}
_GLIBCXX23_CONSTEXPR
bitset<_Nb>&
flip(size_t __pos)
{
@ -259,6 +275,7 @@ namespace __debug
// element access:
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 11. Bitset minor problems
_GLIBCXX23_CONSTEXPR
reference
operator[](size_t __pos)
{
@ -288,6 +305,7 @@ namespace __debug
#endif
template <typename _CharT, typename _Traits, typename _Alloc>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, _Traits, _Alloc>
to_string() const
{ return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
@ -295,6 +313,7 @@ namespace __debug
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 396. what are characters zero and one.
template<class _CharT, class _Traits, class _Alloc>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, _Traits, _Alloc>
to_string(_CharT __zero, _CharT __one = _CharT('1')) const
{
@ -305,6 +324,7 @@ namespace __debug
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 434. bitset::to_string() hard to use.
template<typename _CharT, typename _Traits>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
to_string() const
{ return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
@ -312,12 +332,14 @@ namespace __debug
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 853. to_string needs updating with zero and one.
template<class _CharT, class _Traits>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
to_string(_CharT __zero, _CharT __one = _CharT('1')) const
{ return to_string<_CharT, _Traits,
std::allocator<_CharT> >(__zero, __one); }
template<typename _CharT>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, std::char_traits<_CharT>,
std::allocator<_CharT> >
to_string() const
@ -327,6 +349,7 @@ namespace __debug
}
template<class _CharT>
_GLIBCXX23_CONSTEXPR
std::basic_string<_CharT, std::char_traits<_CharT>,
std::allocator<_CharT> >
to_string(_CharT __zero, _CharT __one = _CharT('1')) const
@ -335,12 +358,14 @@ namespace __debug
std::allocator<_CharT> >(__zero, __one);
}
_GLIBCXX23_CONSTEXPR
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
to_string() const
{
return to_string<char,std::char_traits<char>,std::allocator<char> >();
}
_GLIBCXX23_CONSTEXPR
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
to_string(char __zero, char __one = '1') const
{
@ -351,6 +376,7 @@ namespace __debug
using _Base::count;
using _Base::size;
_GLIBCXX23_CONSTEXPR
bool
operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
{ return _M_base() == __rhs._M_base(); }
@ -366,45 +392,52 @@ namespace __debug
using _Base::any;
using _Base::none;
_GLIBCXX23_CONSTEXPR
bitset<_Nb>
operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
{ return bitset<_Nb>(_M_base() << __pos); }
_GLIBCXX23_CONSTEXPR
bitset<_Nb>
operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
{ return bitset<_Nb>(_M_base() >> __pos); }
_GLIBCXX23_CONSTEXPR
_Base&
_M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
_GLIBCXX23_CONSTEXPR
const _Base&
_M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
};
template<size_t _Nb>
bitset<_Nb>
_GLIBCXX23_CONSTEXPR
inline bitset<_Nb>
operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
{ return bitset<_Nb>(__x) &= __y; }
template<size_t _Nb>
bitset<_Nb>
_GLIBCXX23_CONSTEXPR
inline bitset<_Nb>
operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
{ return bitset<_Nb>(__x) |= __y; }
template<size_t _Nb>
bitset<_Nb>
_GLIBCXX23_CONSTEXPR
inline bitset<_Nb>
operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
{ return bitset<_Nb>(__x) ^= __y; }
template<typename _CharT, typename _Traits, size_t _Nb>
std::basic_istream<_CharT, _Traits>&
inline std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
{ return __is >> __x._M_base(); }
template<typename _CharT, typename _Traits, size_t _Nb>
std::basic_ostream<_CharT, _Traits>&
inline std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const bitset<_Nb>& __x)
{ return __os << __x._M_base(); }

View file

@ -27,6 +27,7 @@ test_indexing()
static_assert( test_indexing() );
#if _GLIBCXX_USE_CXX11_ABI
constexpr bool
test_to_string()
{
@ -35,6 +36,7 @@ test_to_string()
}
static_assert( test_to_string() );
#endif
constexpr bool
test_to_ulong()

View file

@ -24,6 +24,7 @@ constexpr bool test_ntbs()
static_assert( test_ntbs() );
#if _GLIBCXX_USE_CXX11_ABI
constexpr bool test_string()
{
using S = std::string;
@ -51,3 +52,4 @@ constexpr bool test_wstring()
}
static_assert( test_wstring() );
#endif

View file

@ -27,17 +27,26 @@
# define NOTHROW
#endif
#if __cplusplus > 202002L
# define CONSTEXPR constexpr
#else
# define CONSTEXPR
#endif
namespace std {
template <size_t N> class bitset;
// 23.3.5.3 bitset operations:
template <size_t N>
CONSTEXPR
bitset<N> operator&(const bitset<N>&, const bitset<N>&) NOTHROW;
template <size_t N>
CONSTEXPR
bitset<N> operator|(const bitset<N>&, const bitset<N>&) NOTHROW;
template <size_t N>
CONSTEXPR
bitset<N> operator^(const bitset<N>&, const bitset<N>&) NOTHROW;
template <class charT, class traits, size_t N>