gcc/libstdc++-v3/include
Jonathan Wakely 64c986b495 libstdc++: Diagnose broken allocator rebind members
This adds a static assertion to std::allocator_traits::rebind_alloc to
diagnose violations of the rule that rebinding an allocator to its own
value type yields the same allocator type.

This helps to catch the easy mistake of deriving from std::allocator but
forgetting to override the rebind behaviour (no longer an issue in C++20
as std::allocator doesn't have a rebind member that can be inherited).
It also catches bugs like in 23_containers/vector/52591.cc where a typo
means the rebound allocator is a completely different type.

I initially wanted to put this static assert into the body of
allocator_traits:

      static_assert(is_same<rebind_alloc<value_type>, _Alloc>::value,
		    "rebind_alloc<value_type> must be Alloc");

However, this causes a regression in the test for PR libstdc++/72792.
It seems that instantiating std::allocator_traits should be allowed for
invalid allocator types as long as you don't try to rebind them. To
support that, only assert in the __allocator_traits_base::__rebind class
template (in both the primary template and the partial specialization).
As a result, the bug in 20_util/scoped_allocator/outermost.cc is not
diagnosed, because nothing in that test rebinds the allocator.

libstdc++-v3/ChangeLog:

	* include/bits/alloc_traits.h (__allocator_traits_base::__rebind):
	Add static assert for rebind requirement.
	* testsuite/20_util/allocator_traits/members/rebind_alloc.cc:
	Fix invalid rebind member in test allocator.
	* testsuite/20_util/allocator_traits/requirements/rebind_neg.cc:
	New test.
	* testsuite/20_util/scoped_allocator/outermost.cc: Add rebind to
	test allocator.
	* testsuite/23_containers/forward_list/48101_neg.cc: Prune new
	static assert error.
	* testsuite/23_containers/unordered_multiset/48101_neg.cc:
	Likewise.
	* testsuite/23_containers/unordered_set/48101_neg.cc:
	Likewise.
	* testsuite/23_containers/vector/52591.cc: Fix typo in rebind.
2022-12-16 20:58:09 +00:00
..
backward libstdc++: Add preprocessor conditions for freestanding [PR106953] 2022-09-16 21:47:19 +01:00
bits libstdc++: Diagnose broken allocator rebind members 2022-12-16 20:58:09 +00:00
c Update copyright years. 2022-01-03 10:42:10 +01:00
c_compatibility libstdc++: Add _Exit to <stdlib.h> for freestanding 2022-09-21 08:44:21 +01:00
c_global libstdc++: Small extended float support tweaks 2022-10-31 16:49:04 +01:00
c_std libstdc++: Declare std::c8rtomb and std::mbrtoc8 if provided by the C library 2022-01-18 16:31:02 +00:00
debug libstdc++: Pass error handler to libbacktrace functions 2022-12-07 19:54:45 +00:00
decimal Update copyright years. 2022-01-03 10:42:10 +01:00
experimental libstdc++: Call predicate with non-const values in std::erase_if [PR107850] 2022-11-25 15:06:25 +00:00
ext libstdc++: Fix some -Wunused warnings in tests 2022-12-09 00:33:46 +00:00
parallel Update copyright years. 2022-01-03 10:42:10 +01:00
precompiled libstdc++: Implement C++20 <format> [PR104166] 2022-11-13 01:10:44 +00:00
pstl libstdc++: Declare const global variables inline 2022-11-06 11:30:47 -05:00
std libstdc++: Fix size passed to operator delete [PR108097] 2022-12-14 14:11:13 +00:00
tr1 libstdc++: Mark headers that must be hosted as such [PR103626] 2022-10-03 15:43:48 +01:00
tr2 Update copyright years. 2022-01-03 10:42:10 +01:00
Makefile.am libstdc++: add experimental Contracts support 2022-11-18 21:40:29 -05:00
Makefile.in libstdc++: add experimental Contracts support 2022-11-18 21:40:29 -05:00