Jonathan Wakely 85e5b80ee2 libstdc++: Avoid using std::__to_address with iterators
In r12-3935-g82626be2d633a9 I added the partial specialization
std::pointer_traits<__normal_iterator<It, Cont>> so that __to_address
would work with __normal_iterator objects. Soon after that, François
replaced it in r12-6004-g807ad4bc854cae with an overload of __to_address
that served the same purpose, but was less complicated and less wrong.

I now think that both commits were mistakes, and that instead of adding
hacks to make __normal_iterator work with __to_address, we should not be
using __to_address with iterators at all before C++20.

The pre-C++20 std::__to_address function should only be used with
pointer-like types, specifically allocator_traits<A>::pointer types.
Those pointer-like types are guaranteed to be contiguous iterators, so
that getting a raw memory address from them is OK.

For arbitrary iterators, even random access iterators, we don't know
that it's safe to lower the iterator to a pointer e.g. for std::deque
iterators it's not, because (it + n) == (std::to_address(it) + n) only
holds within the same block of the deque's storage.

For C++20, std::to_address does work correctly for contiguous iterators,
including __normal_iterator, and __to_address just calls std::to_address
so also works. But we have to be sure we have an iterator that satisfies
the std::contiguous_iterator concept for it to be safe, and we can't
check that before C++20.

So for pre-C++20 code the correct way to handle iterators that might be
pointers or might be __normal_iterator is to call __niter_base, and if
necessary use is_pointer to check whether __niter_base returned a real
pointer.

We currently have some uses of std::__to_address with iterators where
we've checked that they're either pointers, or __normal_iterator
wrappers around pointers, or satisfy std::contiguous_iterator. But this
seems a little fragile, and it would be better to just use
std::__niter_base for the pointers and __normal_iterator cases, and use
C++20 std::to_address when the C++20 std::contiguous_iterator concept is
satisfied. This patch does that.

libstdc++-v3/ChangeLog:

	* include/bits/basic_string.h (basic_string::assign): Replace
	use of __to_address with __niter_base or std::to_address as
	appropriate.
	* include/bits/ptr_traits.h (__to_address): Add comment.
	* include/bits/shared_ptr_base.h (__shared_ptr): Qualify calls
	to __to_address.
	* include/bits/stl_algo.h (find): Replace use of __to_address
	with __niter_base or std::to_address as appropriate. Only use
	either of them when the range is not empty.
	* include/bits/stl_iterator.h (__to_address): Remove overload
	for __normal_iterator.
	* include/debug/safe_iterator.h (__to_address): Remove overload
	for _Safe_iterator.
	* include/std/ranges (views::counted): Replace use of
	__to_address with std::to_address.
	* testsuite/24_iterators/normal_iterator/to_address.cc: Removed.
2024-10-22 17:08:32 +01:00
2024-05-09 10:58:01 +00:00
2024-04-17 00:18:45 +00:00
2024-10-10 00:19:03 +00:00
2024-07-12 00:17:52 +00:00
2024-07-08 00:17:01 +00:00
2024-04-16 00:18:06 +00:00
2024-10-08 00:19:04 +00:00
2024-01-03 12:19:35 +01:00
2024-09-03 00:21:29 +00:00
2024-10-19 00:19:43 +00:00
2024-09-21 00:16:55 +00:00
2024-10-20 00:17:57 +00:00
2024-04-03 00:17:29 +00:00
2024-10-19 00:19:43 +00:00
2024-10-19 00:19:43 +00:00
2024-10-08 00:19:04 +00:00
2024-05-30 00:16:44 +00:00
2024-08-02 00:18:55 +00:00
2024-10-11 00:17:48 +00:00
2024-09-24 00:18:14 +00:00
2024-09-24 00:18:14 +00:00
2024-09-03 00:21:29 +00:00
2024-08-29 00:19:25 +00:00
2024-09-06 00:19:10 +00:00
2024-05-09 10:58:01 +00:00
2024-09-03 00:21:29 +00:00
2024-08-24 00:18:13 +00:00
2024-07-20 00:17:53 +00:00
2024-09-12 22:51:00 +08:00
2024-10-19 00:19:43 +00:00
2024-09-18 11:51:45 -06:00
2024-09-18 11:51:45 -06:00

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.
Description
No description provided
Readme 1.5 GiB
Languages
C++ 30.7%
C 30%
Ada 14.5%
D 6.1%
Go 5.7%
Other 12.5%