Skip to content

Commit

Permalink
libstdc++: Implement constexpr std::vector for C++20
Browse files Browse the repository at this point in the history
This implements P1004R2 ("Making std::vector constexpr") for C++20.

For now, debug mode vectors are not supported in constant expressions.
To make that work we might need to disable all attaching/detaching of
safe iterators. That can be fixed later.

Co-authored-by: Josh Marshall <joshua.r.marshall.1991@gmail.com>

libstdc++-v3/ChangeLog:

	* include/bits/alloc_traits.h (_Destroy): Make constexpr for
	C++20 mode.
	* include/bits/allocator.h (__shrink_to_fit::_S_do_it):
	Likewise.
	* include/bits/stl_algobase.h (__fill_a1): Declare _Bit_iterator
	overload constexpr for C++20.
	* include/bits/stl_bvector.h (_Bit_type, _S_word_bit): Move out
	of inline namespace.
	(_Bit_reference, _Bit_iterator_base, _Bit_iterator)
	(_Bit_const_iterator, _Bvector_impl_data, _Bvector_base)
	(vector<bool, A>>): Add constexpr to every member function.
	(_Bvector_base::_M_allocate): Initialize storage during constant
	evaluation.
	(vector<bool, A>::_M_initialize_value): Use __fill_bvector_n
	instead of memset.
	(__fill_bvector_n): New helper function to replace memset during
	constant evaluation.
	* include/bits/stl_uninitialized.h (__uninitialized_copy<false>):
	Move logic to ...
	(__do_uninit_copy): New function.
	(__uninitialized_fill<false>): Move logic to ...
	(__do_uninit_fill): New function.
	(__uninitialized_fill_n<false>): Move logic to ...
	(__do_uninit_fill_n): New function.
	(__uninitialized_copy_a): Add constexpr. Use __do_uninit_copy.
	(__uninitialized_move_a, __uninitialized_move_if_noexcept_a):
	Add constexpr.
	(__uninitialized_fill_a): Add constexpr. Use __do_uninit_fill.
	(__uninitialized_fill_n_a): Add constexpr. Use
	__do_uninit_fill_n.
	(__uninitialized_default_n, __uninitialized_default_n_a)
	(__relocate_a_1, __relocate_a): Add constexpr.
	* include/bits/stl_vector.h (_Vector_impl_data, _Vector_impl)
	(_Vector_base, vector): Add constexpr to every member function.
	(_Vector_impl::_S_adjust): Disable ASan annotation during
	constant evaluation.
	(_Vector_base::_S_use_relocate): Disable bitwise-relocation
	during constant evaluation.
	(vector::_Temporary_value): Use a union for storage.
	* include/bits/vector.tcc (vector, vector<bool>): Add constexpr
	to every member function.
	* include/std/vector (erase_if, erase): Add constexpr.
	* testsuite/23_containers/headers/vector/synopsis.cc: Add
	constexpr for C++20 mode.
	* testsuite/23_containers/vector/bool/cmp_c++20.cc: Change to
	compile-only test using constant expressions.
	* testsuite/23_containers/vector/bool/capacity/29134.cc: Adjust
	namespace for _S_word_bit.
	* testsuite/23_containers/vector/bool/modifiers/insert/31370.cc:
	Likewise.
	* testsuite/23_containers/vector/cmp_c++20.cc: Likewise.
	* testsuite/23_containers/vector/cons/89164.cc: Adjust errors
	for C++20 and move C++17 test to ...
	* testsuite/23_containers/vector/cons/89164_c++17.cc: ... here.
	* testsuite/23_containers/vector/bool/capacity/constexpr.cc: New test.
	* testsuite/23_containers/vector/bool/cons/constexpr.cc: New test.
	* testsuite/23_containers/vector/bool/element_access/constexpr.cc: New test.
	* testsuite/23_containers/vector/bool/modifiers/assign/constexpr.cc: New test.
	* testsuite/23_containers/vector/bool/modifiers/constexpr.cc: New test.
	* testsuite/23_containers/vector/bool/modifiers/swap/constexpr.cc: New test.
	* testsuite/23_containers/vector/capacity/constexpr.cc: New test.
	* testsuite/23_containers/vector/cons/constexpr.cc: New test.
	* testsuite/23_containers/vector/data_access/constexpr.cc: New test.
	* testsuite/23_containers/vector/element_access/constexpr.cc: New test.
	* testsuite/23_containers/vector/modifiers/assign/constexpr.cc: New test.
	* testsuite/23_containers/vector/modifiers/constexpr.cc: New test.
	* testsuite/23_containers/vector/modifiers/swap/constexpr.cc: New test.
  • Loading branch information
jwakely committed Nov 12, 2021
1 parent b39265d commit 1ae8edf
Show file tree
Hide file tree
Showing 28 changed files with 2,606 additions and 202 deletions.
2 changes: 2 additions & 0 deletions libstdc++-v3/include/bits/alloc_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*/

template<typename _ForwardIterator, typename _Allocator>
_GLIBCXX20_CONSTEXPR
void
_Destroy(_ForwardIterator __first, _ForwardIterator __last,
_Allocator& __alloc)
Expand All @@ -847,6 +848,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}

template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline void
_Destroy(_ForwardIterator __first, _ForwardIterator __last,
allocator<_Tp>&)
Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/include/bits/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct __shrink_to_fit_aux<_Tp, true>
{
_GLIBCXX20_CONSTEXPR
static bool
_S_do_it(_Tp& __c) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/include/bits/stl_algobase.h
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
const _VTp&);

_GLIBCXX20_CONSTEXPR
void
__fill_a1(_GLIBCXX_STD_C::_Bit_iterator, _GLIBCXX_STD_C::_Bit_iterator,
const bool&);
Expand Down
Loading

0 comments on commit 1ae8edf

Please sign in to comment.