232 Commits
Author SHA1 Message Date
Patrick Palka 8ba1e2077f libstdc++: Add [[no_unique_address]] to repeat_view::_M_value [PR121402]
PR libstdc++/121402

libstdc++-v3/ChangeLog:

	* include/std/ranges (repeat_view::_M_value): Add
	[[no_unique_address]].
	* testsuite/std/ranges/adaptors/sizeof.cc
	[__cpp_lib_ranges_repeat]: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2026-02-18 10:13:51 -05:00
Tomasz Kamiński b58cf25443 libstdc++: Make function_ref non-dangling for stateless wrappers
This patch makes the function_ref non-dangling for the stateless
wrappers:
* any functor for which operator() selected for arguments is static,
* standard functors, including pre-C++20 ones.
In other words, any function_ref fr, that is constructed from stateless
wrapper w, can be still called after the object w is destroyed, e.g.:
  std::function_ref<bool(int, int)> fr(std::ranges::less{});
  fr(1, 2); // OK, previously UB because fr referred to already destroyed
            // temporary
As function_ref's operator() is not constexpr, we test the change by checking
if the above declaration can be made constexpr, as such variable cannot contain
dangling pointer values.

We adjust the function_ref generic constructor from any functor, to use more
specialized invoker:
* _S_static (newly added) if the called operator() overload is static,
  after changes r16-5624-g0ea9d760fbf44c, this covers all post-c++20 functors;
* _S_nttp<_Fd{}> for pre-C++20 standard functors.
In both above cases the value of _M_ptrs is ignored and simply set to nullptr.

This follows same technique (checking _Fd::operator()(args...)), and support
the same set of types, as for one used for the transform views iterators in
r16-5625-g9ed821d107f7a1.

As after this change we provide well-defined behavior for the code, that
previous was undefined, this changes is pure quality-of-implementation.
As illustrated by the test cases, it has observable side effects, where
non-longer dangling constructs can be used to define constexpr function_ref.
However, the standard does not define when the constructors defined constexpr
are actually usable at compile time, and the already have precedent in form
of SSO string for validity such constructs being implementation specific.

libstdc++-v3/ChangeLog:

	* include/bits/funcref_impl.h (function_ref::function_ref(_Fn&&)):
	Use _S_static and _S_nttp invokers.
	* include/bits/funcwrap.h (_Base_invoker::_S_static):
	Define.
	* include/bits/stl_function.h (std::__is_std_op_template)
	(std::__is_std_op_wrapper) [__cplusplus > 201703L]:
	Moved from std/ranges.
	* include/std/ranges (__detail::__is_std_op_template)
	(__detail::__is_std_op_wrapper): Moved to bits/stl_function.h.
	* testsuite/20_util/function_ref/dangling.cc: New test.
	* testsuite/20_util/function_ref/dangling_neg.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2026-02-11 17:54:09 +01:00
Jakub Jelinek 254a858ae7 Update copyright years. 2026-01-02 09:56:11 +01:00
Patrick Palka 5a2a527b26 libstdc++: Use deducing this in range adaptors even in C++20 [PR111550]
Use deducing this to implement perfect forwarding even in C++20 mode
by using the _GLIBCXX_EXPLICIT_THIS_PARAMETER internal FTM instead of
the standard __cpp_explicit_this_parameter.  This fixes the original
testcase from this PR even in C++20 mode.

	PR libstdc++/111550

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__adaptor::_Partial::operator())
	[_GLIBCXX_EXPLICIT_THIS_PARAMETER]: Also use deducing this
	in C++20 mode when possible.
	(views::__adaptor::_Pipe::Operator())
	[_GLIBCXX_EXPLICIT_THIS_PARAMETER]: Likewise.
	* testsuite/std/ranges/adaptors/take.cc (test07): New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-12-05 12:16:30 -05:00
Tomasz Kamiński 2c1e896ac9 libstdc++: Fix exposure of TU-local lambda in __detail::__func_handle_t.
The lambda is considered to be TU-local entity, use a named function
instead.

As drive-by, a functor stored inside __func_handle::_Inplace is renamed
to _M_fn, as we no longer limit the functor type to function pointers.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__func_handle::__select): Named function
	extracted from local lambda.
	(__detail::__func_handle_t): Define using __func_handle::__select.
	(__func_handle::_Inplace): Raname _M_ptr to _M_fn.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-11-27 14:59:33 +01:00
Tomasz Kamiński 9ed821d107 libstdc++: Optimize functor storage for transform views iterators.
The iterators for transform views (views::transform, views::zip_transform,
and views::adjacent_transform) now store a function handle from (from
__detail::__func_handle namespace) instead of a pointer to the view object
(_M_parent).

The following handle templates are defined in __func_handle namespace:
* _Inplace: Used if the functor is a function pointer or standard operator
  wrapper (std::less<>, etc). The functor is  stored directly in __func_handle
  and the iterator. This avoid double indirection through a pointer to the
  function pointer, and reduce the size of iterator for std wrappers.
* _InplaceMemPtr: Used for data or function member pointers. This behaves
  similarly to _Inplace, but uses __invoke for invocations.
* _StaticCall: Used if the operator() selected by overload resolution
  for the iterator reference is static. In this case, __func_handle is empty,
  reducing the iterator size.
* _ViaPointer: Used for all remaining cases. __func_handle stores a pointer
  to the functor object stored within the view. Only for this template the
  cv-qualification of the functor template parameter (_Fn) relevant, and
  specialization for both const and mutable types are generated.

As a consequence of these changes, the iterators of transform views no longer
depend on the view object when handle other than __func_handle::_ViaPointer
is used. The corresponding views are not marked as borrowed_range, as they
are not marked as such in the standard.

The use of _Inplace is limited to only set of pre-C++20 standard functors,
as for once introduced later operator() was retroactively made static.
We do not extent to to any empty fuctor, as it's oprator may still depend on
value of this pointer as illustrated by test12 in
std/ranges/adaptors/transform.cc test file.

Storing function member pointers directly increases the iterator size in that
specific case, but this is deemed beneficial for consistent treatment of
function and data member pointers.

To avoid materializing temporaries when the underlying iterator(s) return a
prvalue, the _M_call_deref and _M_call_subscript methods of handles are
defined to accept the iterator(s), which are then dereferenced as arguments
of the functor.

Using _Fd::operator()(*__iters...) inside requires expression is only
supported since clang-20, however at the point of GCC-16 release, clang-22
should be already available.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::__is_std_op_template)
	(__detail::__is_std_op_wrapper, __func_handle::_Inplace)
	(__func_handle::_InplaceMemPtr, __func_handle::_ViaPointer)
	(__func_handle::_StaticCall, __detail::__func_handle_t): Define.
	(transform_view::_Iterator, zip_transform_view::_Iterator)
	(adjacent_tranform_view::_Iterator): Replace pointer to view
	(_M_parent) with pointer to functor (_M_fun). Update constructors
	to construct _M_fun from *__parent->_M_fun. Define operator* and
	operator[] in terms of _M_call_deref and _M_call_subscript.
	* testsuite/std/ranges/adaptors/adjacent_transform/1.cc: New tests.
	* testsuite/std/ranges/adaptors/transform.cc: New tests.
	* testsuite/std/ranges/zip_transform/1.cc: New tests.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Reviewed-by: Patrick Palka <ppalka@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-11-26 12:06:50 +01:00
Tomasz Kamiński 35f05d04f3 libstdc++: Optimize handling of optional for views: take, drop, reverse and as_const.
This implements P3913R1: Optimize for std::optional in range adaptors.

Specifically, for an opt of type optional<T> that is a view:
* views::reverse(opt), views::take(opt, n), and views::drop(opt, n) returns
   optional<T>.
* views::as_const(opt), optional<T&> is converted into optional<const T&>.
  optional<T const> is not used in the non-reference case because, such
  type is not move assignable, and thus not a view.

libstdc++-v3/ChangeLog:

	* include/std/optional (__is_optional_ref): Define.
	* include/std/ranges (_Take::operator(), _Drop::operator())
	(_Reverse::operator()):	Handle optional<T> that are view.
	(_AsConst::operator()): Handle optional<T&>.
	* testsuite/20_util/optional/range.cc: New tests.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-11-13 14:35:11 +01:00
Jonathan Wakely 437ed2fec7 libstdc++: Add missing constraints to views::indices
Calling views::indices(n) should be expression equivalent to
views::iota(decltype(n)(0), n), which means it should have the same
constraints as views::iota and be SFINAE-friendly.

libstdc++-v3/ChangeLog:

	* include/std/ranges (indices::operator()): Constrain using
	__can_iota_view concept.
	* testsuite/std/ranges/indices/1.cc: Check SFINAE-friendliness
	required by expression equivalence. Replace unused <vector>
	header with <stddef.h> needed for size_t.
2025-10-22 09:59:36 +01:00
Yuao Ma 7aefb48bb0 libstdc++: Implement P3060R3: Add std::views::indices(n)
This patch adds the views::indices function using iota.

libstdc++-v3/ChangeLog:

	* include/bits/version.def: Add ranges_indices FTM.
	* include/bits/version.h: Regenerate.
	* include/std/ranges: Implement views::indices.
	* testsuite/std/ranges/indices/1.cc: New test.
2025-10-21 00:18:23 +08:00
Jonathan Wakely 5d774ec80b libstdc++: Optimize determination of std::tuple_cat return type
The std::tuple_cat function has to determine a std::tuple return type
from zero or more tuple-like arguments. This uses the __make_tuple class
template to transform a tuple-like type into a std::tuple, and the
__combine_tuples class template to combine zero or more std::tuple types
into a single std::tuple type.

This change optimizes the __make_tuple class template to use an
_Index_tuple and pack expansion instead of recursive instantiation, and
optimizes __combine_tuples to use fewer levels of recursion.

For ranges::adjacent_view's __detail::__repeated_tuple helper we can
just use the __make_tuple class template directly, instead of doing
overload resolution on std::tuple_cat to get its return type.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::__repeated_tuple): Use
	__make_tuple helper alias directly, instead of doing overload
	resolution on std::tuple_cat.
	* include/std/tuple (__make_tuple_impl): Remove.
	(__do_make_tuple): Replace recursion with _Index_tuple and pack
	expansion.
	(__make_tuple): Adjust to new __do_make_tuple definition.
	(__combine_tuples<tuple<T1s...>, tuple<T2s...>, Rem...>): Replace
	with a partial specialization for exactly two tuples and a
	partial specialization for three or more tuples.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
2025-09-16 18:25:02 +01:00
Jonathan Wakely 0c762f79a9 libstdc++: Fix missing change to views::pairwise from P2165R4 [PR121956]
ranges::adjacent_view::_Iterator::value_type should have been changed by
r14-8710-g65b4cba9d6a9ff to always produce std::tuple, even for the
N == 2 views::pairwise specialization.

libstdc++-v3/ChangeLog:

	PR libstdc++/121956
	* include/std/ranges (adjacent_view::_Iterator::value_type):
	Always define as std::tuple<T, N>, not std::pair<T, T>.
	* testsuite/std/ranges/adaptors/adjacent/1.cc: Check value type
	of views::pairwise.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
2025-09-16 17:38:41 +01:00
Patrick Palka 8e971d665a libstdc++: Make join_view::_Iterator::_M_get_inner noexcept [PR121804]
Since this helper (added in r16-3576-g7f7f1878eedd80) is used in the
noexcept-spec of iter_move and iter_swap, it in turn needs an accurate
noexcept-spec.

	PR libstdc++/121804

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_get_inner):
	Mark noexcept.
	* testsuite/std/ranges/adaptors/join.cc (test16): New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-09-05 13:14:16 -04:00
Patrick Palka 7f7f1878ee libstdc++: Conditionalize LWG 3569 changes to join_view
LWG 3569 adjusted join_view's iterator specification to handle non
default-constructible iterators by wrapping the corresponding data member
in std::optional, which we followed suit in r13-2649-g7aa80c82ecf3a3.

But this wrapping is unnecessary for iterators that are already
default-constructible.  Rather than unconditionally using std::optional
here, which introduces time/space overhead, this patch conditionalizes
our LWG 3569 changes on the iterator in question being non-forward (and
thus non default-constructible).  We check forwardness instead of
default-constructibility in order to accommodate input-only iterators
that satisfy but do not model default_initializable, e.g. whose default
constructor is underconstrained.

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_satisfy):
	Adjust to handle non-std::optional _M_inner as per before LWG 3569.
	(join_view::_Iterator::_M_get_inner): New.
	(join_view::_Iterator::_M_inner): Don't wrap in std::optional if
	the iterator is forward.  Initialize.
	(join_view::_Iterator::operator*): Use _M_get_inner instead
	of *_M_inner.
	(join_view::_Iterator::operator++): Likewise.
	(join_view::_Iterator::iter_move): Likewise.
	(join_view::_Iterator::iter_swap): Likewise.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-09-04 09:57:36 -04:00
Tomasz Kamiński 1a41e52d7e libstdc++: Reuse _Bind_back_t functor in ranges::_Partial
This patch refactors ranges::_Partial to be implemented using _Bind_back_t.
This allows it to benefit from the changes in r16-3398-g250dd5b5604fbc,
specifically making the closure trivially copyable. Since _Bind_back_t
already provides an optimized implementation for a single bound argument,
specializations for _Partial with a single argument are now removed.

We still preserve a specialization of _Partial for trivially copy-constructible
arguments that define only a const overload of operator(). To avoid
re-checking invocability constraints, this specialization calls the now-public,
unconstrained _Binder::_S_call static method instead of the constrained
_Binder::operator().

The primary specialization of _Partial retains its operator(), which
uses a simpler __adaptor_invocable constraint that does not consider
member pointers, as they are not relevant here. This implementation also
calls _Binder::_S_call to avoid re-performing overload resolution and
invocability checks for _Binder::operator().

Finally, the _M_binder member (_Bind_back_t) is now marked
[[no_unique_address]]. This is beneficial as ranges::_Partial is used with
ranges::to, which commonly has zero or empty bound arguments (e.g., stateless
allocators, comparators, or hash functions).

libstdc++-v3/ChangeLog:

	* include/bits/binders.h (_Binder::_S_call): Make public.
	* include/std/ranges (ranges::_Partial<_Adaptor, _Args...>):
	Replace tuple<_Args...> with _Bind_back_t<_Adaptor, _Args...>.
	(ranges::_Partial<_Adaptor, _Arg>): Remove.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-09-04 15:30:00 +02:00
Patrick Palka 06e627e1e2 libstdc++/ranges: Prefer using offset-based _CachedPosition
The offset-based partial specialization of _CachedPosition for
random-access iterators is currently only selected if the offset type is
smaller than the iterator type.  Before r12-1018-g46ed811bcb4b86 this
made sense since the main partial specialization only stored the
iterator (incorrectly).  After that bugfix, the main partial
specialization now effectively stores a std::optional<iter> so the
size constraint is inaccurate.  And this main partial specialization
must invalidate itself upon copy/move unlike the offset-based partial
specialization.  So I think we should just always prefer the
offset-based _CachedPosition for a random-access iterator, even if the
offset type happens to be larger than the iterator type.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::_CachedPosition): Remove
	additional size constraint on the offset-based partial
	specialization.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-08-26 15:45:57 -04:00
Patrick Palka 0828600f58 libstdc++: Add missing initializers for __maybe_present_t members [PR119962]
Data members of type __maybe_present_t where the conditionally present
type might be an aggregate or fundamental type need to be explicitly
value-initialized (rather than implicitly default-initialized), so that
default-initialization of the containing class always results in an
completely initialized object.

	PR libstdc++/119962

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_outer): Initialize.
	(lazy_split_view::_OuterIter::_M_current): Initialize.
	(join_with_view::_Iterator::_M_outer_it): Initialize.
	* testsuite/std/ranges/adaptors/join.cc (test15): New test.
	* testsuite/std/ranges/adaptors/join_with/1.cc (test05): New test.
	* testsuite/std/ranges/adaptors/lazy_split.cc (test13): New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-07-15 15:17:23 -04:00
Patrick Palka c5a17e92eb libstdc++: Update LWG 4166 changes to concat_view::end() [PR120934]
In r15-4555-gf191c830154565 we proactively implemented the initial
proposed resolution for LWG 4166 which later turned out to be
insufficient, since we must also require equality_comparable of the
underlying iterators before concat_view could be a common range.

This patch implements the updated P/R, requiring all underlying
iterators to be forward (which implies equality_comparable) before
making concat_view common, which fixes the testcase from this PR.

	PR libstdc++/120934

libstdc++-v3/ChangeLog:

	* include/std/ranges (concat_view::end): Refine condition
	for returning an iterator instead of default_sentinel as
	per the updated P/R for LWG 4166.
	* testsuite/std/ranges/concat/1.cc (test05): New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-07-03 10:55:17 -04:00
Nathan Myers 3aff13cfab libstdc++: make range view ctors explicit (P2711) [PR119744]
Make range view constructors explicit, per P2711. Technically, this
is a breaking change, but it is unlikely to break any production
code, as reliance on non-explicit construction is unidiomatic..

libstdc++-v3/ChangeLog
	PR libstdc++/119744
	* include/std/ranges: View ctors become explicit.
2025-07-02 07:04:18 -04:00
Jonathan Wakely 1197f896ae libstdc++: Fix some Clang -Wsystem-headers warnings in <ranges>
libstdc++-v3/ChangeLog:

	* include/std/ranges (_ZipTransform::operator()): Remove name of
	unused parameter.
	(chunk_view::_Iterator, stride_view::_Iterator): Likewise.
	(join_with_view): Declare _Iterator and _Sentinel as class
	instead of struct.
	(repeat_view): Declare _Iterator as class instead of struct.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-19 10:44:38 +01:00
Jonathan Wakely 25775e73ea libstdc++: Do not define __cpp_lib_ranges_iota in <ranges>
In r14-7153-gadbc46942aee75 we removed a duplicate definition of
__glibcxx_want_range_iota from <ranges>, but __cpp_lib_ranges_iota
should be defined in <ranges> at all.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__glibcxx_want_ranges_iota): Do not
	define.
2025-04-15 17:34:34 +01:00
Jonathan Wakely 479a0a8644 libstdc++: Allow std::ranges::to to create unions
LWG 4229 points out that the std::ranges::to wording refers to class
types, but I added an assertion using std::is_class_v which only allows
non-union class types. LWG consensus is that unions should be allowed,
so this additionally uses std::is_union_v.

libstdc++-v3/ChangeLog:

	* include/std/ranges (ranges::to): Allow unions as well as
	non-union class types.
	* testsuite/std/ranges/conv/lwg4229.cc: New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-03-25 17:44:25 +00:00
Tomasz Kamiński 5abe571e02 libstdc++: Fix views::zip_transform constraints for empty range pack [PR111138]
Add missing move_constructible && regular_invocable constrains on functor type,
and is_object on functor result type for invocations of views::zip_transform
without range arguments.

	PR libstdc++/111138

libstdc++-v3/ChangeLog:

	* include/std/ranges (_ZipTransform::operator()):
	Create separate overload for calls with empty range pack,
	and add move_constructible, regular_invocable and
	is_object_v<invoke_result_t<...>>> constraints.
	* testsuite/std/ranges/zip_transform/1.cc: New tests

Reviewed-by: Patrick Palka <ppalka@redhat.com>
	Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-03-14 14:28:29 +01:00
Patrick Palka 50359c0a44 libstdc++: Fix ref_view branch of views::as_const [PR119135]
Unlike for span<X> and empty_view<X>, the range_reference_t of
ref_view<X> doesn't correspond to X.  This patch fixes the ref_view
branch of views::as_const to correctly query its underlying range
type X.

	PR libstdc++/119135

libstdc++-v3/ChangeLog:

	* include/std/ranges: Include <utility>.
	(views::__detail::__is_ref_view): Replace with ...
	(views::__detail::__is_constable_ref_view): ... this.
	(views::_AsConst::operator()): Replace bogus use of element_type
	in the ref_view branch.
	* testsuite/std/ranges/adaptors/as_const/1.cc (test03): Extend
	test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-03-13 09:15:21 -04:00
Patrick Palka cfb20f17bd libstdc++: Implement P3137R3 views::to_input for C++26
libstdc++-v3/ChangeLog:

	* include/bits/version.def (ranges_to_input): Define.
	* include/bits/version.h: Regenerate.
	* include/std/ranges (ranges::to_input_view): Define for C++26.
	(views::__detail::__can_to_input): Likewise.
	(views::_ToInput, views::to_input): Likewise.
	* testsuite/std/ranges/adaptors/to_input/1.cc: New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-03-12 16:09:42 -04:00
Jonathan Wakely c7449f1b15 libstdc++: Make enumerate_view::iterator::operator- noexcept
Implement LWG 3912, approved in Varna, June 2023.

libstdc++-v3/ChangeLog:

	* include/std/ranges (enumerate_view::_Iterator::operator-):
	Add noexcept, as per LWG 3912.
	* testsuite/std/ranges/adaptors/enumerate/1.cc: Check iterator
	difference is noexcept.
2025-03-05 22:11:06 +00:00
Patrick Palka 410a4c1264 libstdc++: Implement P3138R5 views::cache_latest
libstdc++-v3/ChangeLog:

	* include/bits/version.def (ranges_cache_latest): Define.
	* include/bits/version.h: Regenerate.
	* include/std/ranges (__detail::__non_propagating_cache::_M_reset):
	Export from base class _Optional_base.
	(cache_latest_view): Define for C++26.
	(cache_latest_view::_Iterator): Likewise.
	(cache_latest_view::_Sentinel): Likewise.
	(views::__detail::__can_cache_latest): Likewise.
	(views::_CacheLatest, views::cache_latest): Likewise.
	* testsuite/std/ranges/adaptors/cache_latest/1.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-03-05 16:46:15 -05:00
Patrick Palka 81c6c99efa libstdc++: Some concat_view bugfixes [PR115215, PR115218, LWG 4082]
- Use __builtin_unreachable to suppress a false-positive "control
  reaches end of non-void function" warning in the recursive lambda
  (which the existing tests failed to notice since test01 wasn't
  being called at runtime)
- Relax the constraints on views::concat in the single-argument case
  as per PR115215
- Add an input_range requirement to that same case as per LWG 4082
- In the const-converting constructor of concat_view's iterator,
  don't require the first iterator to be default constructible

	PR libstdc++/115215
	PR libstdc++/115218

libstdc++-v3/ChangeLog:

	* include/std/ranges
	(concat_view::iterator::_S_invoke_with_runtime_index): Use
	__builtin_unreachable in recursive lambda to certify it always
	exits via 'return'.
	(concat_view::iterator::iterator): In the const-converting
	constructor, direct initialize _M_it.
	(views::_Concat::operator()): Adjust constraints in the
	single-argument case as per LWG 4082.
	* testsuite/std/ranges/concat/1.cc (test01): Call it at runtime
	too.
	(test04): New test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-03-05 11:11:35 -05:00
Jonathan Wakely a9cfcd0d9e libstdc++: Add static_assertions to ranges::to adaptor factory [PR112803]
The standard requires that we reject attempts to create a ranges::to
adaptor for cv-qualified types and non-class types. Currently we only
diagnose it once the adaptor is used in a pipeline.

This adds static assertions to diagnose it immediately.

libstdc++-v3/ChangeLog:

	PR libstdc++/112803
	* include/std/ranges (ranges::to): Add static assertions to
	enforce Mandates conditions.
	* testsuite/std/ranges/conv/112803.cc: New test.
2025-02-28 21:35:27 +00:00
Patrick Palka 49bc1cf6c6 libstdc++: Rename concat_view::iterator to ::_Iterator
Even though 'iterator' is a reserved macro name, we can't use it as the
name of this implementation detail type since it could introduce name
lookup ambiguity in valid code, e.g.

  struct A { using iterator = void; }
  struct B : concat_view<...>, A { using type = iterator; };

libstdc++-v3/ChangeLog:

	* include/std/ranges (concat_view::iterator): Rename to ...
	(concat_view::_Iterator): ... this throughout.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-02-19 23:05:40 -05:00
Patrick Palka 8543dc52d8 libstdc++: Sync concat_view with final P2542 revision [PR115209]
Our concat_view implementation is accidentally based off of an older
revision of the paper, P2542R7 instead of R8.  As far as I can tell the
only semantic change in the final revision is the relaxed constraints on
the iterator's iter/sent operator- overloads, which this patch updates.

This patch also simplifies the concat_view::end wording via C++26 pack
indexing as per the final revision.  In turn we make the availability of
this library feature conditional on __cpp_pack_indexing.  (Note pack
indexing is implemented in GCC 15 and Clang 19).

	PR libstdc++/115209

libstdc++-v3/ChangeLog:

	* include/bits/version.def (ranges_concat): Depend on
	__cpp_pack_indexing.
	* include/bits/version.h: Regenerate.
	* include/std/ranges (__detail::__last_is_common): Remove.
	(__detail::__all_but_first_sized): New.
	(concat_view::end): Use C++26 pack indexing instead of
	__last_is_common as per R8 of P2542.
	(concat_view::iterator::operator-): Update constraints on
	iter/sent overloads as per R8 of P2542.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-02-19 22:59:49 -05:00
Patrick Palka 09d1cbee10 libstdc++: Fix views::transform(move_only_fn{}) forwarding [PR118413]
The range adaptor perfect forwarding simplification mechanism is currently
only enabled for trivially copyable bound arguments, to prevent undesirable
copies of complex objects.  But "trivially copyable" is the wrong property
to check for here, since a move-only type with a trivial move constructor
is considered trivially copyable, and after P2492R2 we can't assume copy
constructibility of the bound arguments.  This patch makes the mechanism
more specifically check for trivial copy constructibility instead so
that it's properly disabled for move-only bound arguments.

	PR libstdc++/118413

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__adaptor::_Partial): Adjust
	constraints on the "simple" partial specializations to require
	is_trivially_copy_constructible_v instead of
	is_trivially_copyable_v.
	* testsuite/std/ranges/adaptors/adjacent_transform/1.cc (test04):
	Extend P2494R2 test.
	* testsuite/std/ranges/adaptors/transform.cc (test09): Likewise.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-01-29 10:02:28 -05:00
Jakub Jelinek 6441eb6dc0 Update copyright years. 2025-01-02 11:59:57 +01:00
Patrick Palka 56029c91dc libstdc++/ranges: make _RangeAdaptorClosure befriend operator|
This declares the range adaptor pipe operators a friend of the
_RangeAdaptorClosure base class so that the std module doesn't need to
export them for ADL to find them.

Note that we deliberately don't define these pipe operators as hidden
friends, see r14-3293-g4a6f3676e7dd9e.

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__adaptor::_RangeAdaptorClosure):
	Befriend both operator| overloads.
	* src/c++23/std.cc.in: Don't export views::__adaptor::operator|.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-11-27 11:59:38 -05:00
Patrick Palka 361050589b libstdc++: Implement LWG 3563 changes to keys_view and values_view
This LWG issue corrects the definition of these alias templates to make
them suitable for alias CTAD.

libstdc++-v3/ChangeLog:

	* include/std/ranges (keys_view): Adjust as per LWG 3563.
	(values_view): Likewise.
	* testsuite/std/ranges/adaptors/elements.cc (test08): New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-11-14 13:27:41 -05:00
Patrick Palka 7f622ee83f libstdc++: Fix complexity of drop_view::begin() const [PR112641]
Views are required to have a amortized O(1) begin(), but our drop_view's
const begin overload is O(n) for non-common ranges with a non-sized
sentinel.  This patch reimplements it so that it's O(1) always.  See
also LWG 4009.

	PR libstdc++/112641

libstdc++-v3/ChangeLog:

	* include/std/ranges (drop_view::begin): Reimplement const
	overload so that it's O(1) always.
	* testsuite/std/ranges/adaptors/drop.cc (test10): New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-10-29 09:26:19 -04:00
Patrick Palka f191c83015 libstdc++: Implement LWG 4166 changes to concat_view::end()
This patch proactively implements the proposed resolution for this LWG
issue, which seems straightforward and slated to get approved as-is.

(No _GLIBCXX_RESOLVE_LIB_DEFECTS code comment is added since concat_view
is C++26, so this isn't a defect against a published standard.)

libstdc++-v3/ChangeLog:

	* include/std/ranges (concat_view::begin): Add space after
	'requires' starting a requires-clause.
	(concat_view::end): Likewise.  Refine condition for returning an
	iterator rather than default_sentinel as per LWG 4166.
	* testsuite/std/ranges/concat/1.cc (test03): Verify LWG 4166
	example.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-10-22 17:01:59 -04:00
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
Jonathan Wakely 7f65f94917 libstdc++: Implement LWG 3798 for range adaptors [PR106676]
LWG 3798 modified the iterator_category of the iterator types for
transform_view, join_with_view, zip_transform_view and
adjacent_transform_view, to allow the iterator's reference type to be an
rvalue reference.

libstdc++-v3/ChangeLog:

	PR libstdc++/106676
	* include/bits/iterator_concepts.h (__cpp17_fwd_iterator): Use
	is_reference instead of is_value_reference.
	rvalue references.
	* include/std/ranges (transform_view:__iter_cat::_S_iter_cat):
	Likewise.
	(zip_transform_view::__iter_cat::_S_iter_cat): Likewise.
	(adjacent_transform_view::__iter_cat::_S_iter_cat): Likewise.
	(join_with_view::__iter_cat::_S_iter_cat): Likewise.
	* testsuite/std/ranges/adaptors/transform.cc: Check
	iterator_category when the transformation function returns an
	rvalue reference type.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
2024-10-15 14:46:44 +01:00
Jonathan Wakely dde19c600c libstdc++: Implement LWG 3564 for ranges::transform_view
The _Iterator<true> type returned by begin() const uses const F& to
transform the elements, so it should use const F& to determine the
iterator's value_type and iterator_category as well.

This was accepted into the WP in July 2022.

libstdc++-v3/ChangeLog:

	* include/std/ranges (transform_view:_Iterator): Use const F&
	to determine value_type and iterator_category of
	_Iterator<true>, as per LWG 3564.
	* testsuite/std/ranges/adaptors/transform.cc: Check value_type
	and iterator_category.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
2024-10-14 22:07:28 +01:00
Patrick Palka 20165d0107 libstdc++/ranges: Implement various small LWG issues
This implements the following small LWG issues:

  3848. adjacent_view, adjacent_transform_view and slide_view missing base accessor
  3851. chunk_view::inner-iterator missing custom iter_move and iter_swap
  3947. Unexpected constraints on adjacent_transform_view::base()
  4001. iota_view should provide empty
  4012. common_view::begin/end are missing the simple-view check
  4013. lazy_split_view::outer-iterator::value_type should not provide default constructor
  4035. single_view should provide empty
  4053. Unary call to std::views::repeat does not decay the argument
  4054. Repeating a repeat_view should repeat the view

libstdc++-v3/ChangeLog:

	* include/std/ranges (single_view::empty): Define as per LWG 4035.
	(iota_view::empty): Define as per LWG 4001.
	(lazy_split_view::_OuterIter::value_type): Remove default
	constructor and make other constructor private as per LWG 4013.
	(common_view::begin): Disable non-const overload for simple
	views as per LWG 4012.
	(common_view::end): Likewise.
	(adjacent_view::base): Define as per LWG 3848.
	(adjacent_transform_view::base): Likewise.
	(chunk_view::_InnerIter::iter_move): Define as per LWG 3851.
	(chunk_view::_InnerIter::itep_swap): Likewise.
	(slide_view::base): Define as per LWG 3848.
	(repeat_view): Adjust deduction guide as per LWG 4053.
	(_Repeat::operator()): Adjust single-parameter overload as per
	LWG 4054.
	* testsuite/std/ranges/adaptors/adjacent/1.cc: Verify existence
	of base member function.
	* testsuite/std/ranges/adaptors/adjacent_transform/1.cc: Likewise.
	* testsuite/std/ranges/adaptors/chunk/1.cc: Test LWG 3851 example.
	* testsuite/std/ranges/adaptors/slide/1.cc: Verify existence of
	base member function.
	* testsuite/std/ranges/iota/iota_view.cc: Test LWG 4001 example.
	* testsuite/std/ranges/repeat/1.cc: Test LWG 4053/4054 examples.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-10-04 10:01:39 -04:00
Jason Merrill 63a598deb0 libstdc++: #ifdef out #pragma GCC system_header
In r15-3714-gd3a7302ec5985a I added -Wsystem-headers to the libstdc++ build
flags to help catch problems in the library.  This patch takes a different
approach, of disabling the #pragma system_header unless _GLIBCXX_SYSHDR is
defined.  As a result, the testsuites will treat them as non-system-headers
to get better warning coverage during regression testing of both gcc and
libstdc++, not just when building the library.

My rationale for the #ifdef instead of just removing the #pragma is the
three G++ tests that want to test libstdc++ system header behavior, so we
need a way to select it.

This doesn't affect installed libraries, as they get their
system-header status from the lookup path.  But testsuite_flags
--build-includes gives -I directives rather than -isystem.

This patch doesn't change the headers in config/ because I'm not compiling
with most of them, so won't see any warnings that need fixing.  Adjusting
them could happen later, or we can not bother.

libstdc++-v3/ChangeLog:

	* acinclude.m4 (WARN_FLAGS): Remove -Wsystem-headers.
	* configure: Regenerate.
	* include/bits/algorithmfwd.h: #ifdef out #pragma GCC system_header.
	* include/bits/atomic_base.h
	* include/bits/atomic_futex.h
	* include/bits/atomic_timed_wait.h
	* include/bits/atomic_wait.h
	* include/bits/basic_ios.h
	* include/bits/basic_string.h
	* include/bits/boost_concept_check.h
	* include/bits/char_traits.h
	* include/bits/charconv.h
	* include/bits/chrono.h
	* include/bits/chrono_io.h
	* include/bits/codecvt.h
	* include/bits/concept_check.h
	* include/bits/cpp_type_traits.h
	* include/bits/elements_of.h
	* include/bits/enable_special_members.h
	* include/bits/erase_if.h
	* include/bits/forward_list.h
	* include/bits/functional_hash.h
	* include/bits/gslice.h
	* include/bits/gslice_array.h
	* include/bits/hashtable.h
	* include/bits/indirect_array.h
	* include/bits/invoke.h
	* include/bits/ios_base.h
	* include/bits/iterator_concepts.h
	* include/bits/locale_classes.h
	* include/bits/locale_facets.h
	* include/bits/locale_facets_nonio.h
	* include/bits/localefwd.h
	* include/bits/mask_array.h
	* include/bits/max_size_type.h
	* include/bits/memory_resource.h
	* include/bits/memoryfwd.h
	* include/bits/move_only_function.h
	* include/bits/node_handle.h
	* include/bits/ostream_insert.h
	* include/bits/out_ptr.h
	* include/bits/parse_numbers.h
	* include/bits/postypes.h
	* include/bits/quoted_string.h
	* include/bits/range_access.h
	* include/bits/ranges_base.h
	* include/bits/refwrap.h
	* include/bits/sat_arith.h
	* include/bits/semaphore_base.h
	* include/bits/slice_array.h
	* include/bits/std_abs.h
	* include/bits/std_function.h
	* include/bits/std_mutex.h
	* include/bits/std_thread.h
	* include/bits/stl_iterator_base_funcs.h
	* include/bits/stl_iterator_base_types.h
	* include/bits/stl_tree.h
	* include/bits/stream_iterator.h
	* include/bits/streambuf_iterator.h
	* include/bits/stringfwd.h
	* include/bits/this_thread_sleep.h
	* include/bits/unique_lock.h
	* include/bits/uses_allocator_args.h
	* include/bits/utility.h
	* include/bits/valarray_after.h
	* include/bits/valarray_array.h
	* include/bits/valarray_before.h
	* include/bits/version.h
	* include/c_compatibility/fenv.h
	* include/c_compatibility/inttypes.h
	* include/c_compatibility/stdint.h
	* include/decimal/decimal.h
	* include/experimental/bits/net.h
	* include/experimental/bits/shared_ptr.h
	* include/ext/aligned_buffer.h
	* include/ext/alloc_traits.h
	* include/ext/atomicity.h
	* include/ext/concurrence.h
	* include/ext/numeric_traits.h
	* include/ext/pod_char_traits.h
	* include/ext/pointer.h
	* include/ext/stdio_filebuf.h
	* include/ext/stdio_sync_filebuf.h
	* include/ext/string_conversions.h
	* include/ext/type_traits.h
	* include/ext/vstring.h
	* include/ext/vstring_fwd.h
	* include/ext/vstring_util.h
	* include/parallel/algorithmfwd.h
	* include/parallel/numericfwd.h
	* include/tr1/functional_hash.h
	* include/tr1/hashtable.h
	* include/tr1/random.h
	* libsupc++/exception.h
	* libsupc++/hash_bytes.h
	* include/bits/basic_ios.tcc
	* include/bits/basic_string.tcc
	* include/bits/fstream.tcc
	* include/bits/istream.tcc
	* include/bits/locale_classes.tcc
	* include/bits/locale_facets.tcc
	* include/bits/locale_facets_nonio.tcc
	* include/bits/ostream.tcc
	* include/bits/sstream.tcc
	* include/bits/streambuf.tcc
	* include/bits/string_view.tcc
	* include/bits/version.tpl
	* include/experimental/bits/string_view.tcc
	* include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp
	* include/ext/random.tcc
	* include/ext/vstring.tcc
	* include/tr2/bool_set.tcc
	* include/tr2/dynamic_bitset.tcc
	* include/bits/c++config
	* include/c/cassert
	* include/c/cctype
	* include/c/cerrno
	* include/c/cfloat
	* include/c/ciso646
	* include/c/climits
	* include/c/clocale
	* include/c/cmath
	* include/c/csetjmp
	* include/c/csignal
	* include/c/cstdarg
	* include/c/cstddef
	* include/c/cstdio
	* include/c/cstdlib
	* include/c/cstring
	* include/c/ctime
	* include/c/cuchar
	* include/c/cwchar
	* include/c/cwctype
	* include/c_global/cassert
	* include/c_global/ccomplex
	* include/c_global/cctype
	* include/c_global/cerrno
	* include/c_global/cfenv
	* include/c_global/cfloat
	* include/c_global/cinttypes
	* include/c_global/ciso646
	* include/c_global/climits
	* include/c_global/clocale
	* include/c_global/cmath
	* include/c_global/csetjmp
	* include/c_global/csignal
	* include/c_global/cstdalign
	* include/c_global/cstdarg
	* include/c_global/cstdbool
	* include/c_global/cstddef
	* include/c_global/cstdint
	* include/c_global/cstdio
	* include/c_global/cstdlib
	* include/c_global/cstring
	* include/c_global/ctgmath
	* include/c_global/ctime
	* include/c_global/cuchar
	* include/c_global/cwchar
	* include/c_global/cwctype
	* include/c_std/cassert
	* include/c_std/cctype
	* include/c_std/cerrno
	* include/c_std/cfloat
	* include/c_std/ciso646
	* include/c_std/climits
	* include/c_std/clocale
	* include/c_std/cmath
	* include/c_std/csetjmp
	* include/c_std/csignal
	* include/c_std/cstdarg
	* include/c_std/cstddef
	* include/c_std/cstdio
	* include/c_std/cstdlib
	* include/c_std/cstring
	* include/c_std/ctime
	* include/c_std/cuchar
	* include/c_std/cwchar
	* include/c_std/cwctype
	* include/debug/array
	* include/debug/bitset
	* include/debug/deque
	* include/debug/forward_list
	* include/debug/list
	* include/debug/map
	* include/debug/set
	* include/debug/string
	* include/debug/unordered_map
	* include/debug/unordered_set
	* include/debug/vector
	* include/decimal/decimal
	* include/experimental/algorithm
	* include/experimental/any
	* include/experimental/array
	* include/experimental/buffer
	* include/experimental/chrono
	* include/experimental/contract
	* include/experimental/deque
	* include/experimental/executor
	* include/experimental/filesystem
	* include/experimental/forward_list
	* include/experimental/functional
	* include/experimental/internet
	* include/experimental/io_context
	* include/experimental/iterator
	* include/experimental/list
	* include/experimental/map
	* include/experimental/memory
	* include/experimental/memory_resource
	* include/experimental/net
	* include/experimental/netfwd
	* include/experimental/numeric
	* include/experimental/propagate_const
	* include/experimental/ratio
	* include/experimental/regex
	* include/experimental/scope
	* include/experimental/set
	* include/experimental/socket
	* include/experimental/string
	* include/experimental/string_view
	* include/experimental/synchronized_value
	* include/experimental/system_error
	* include/experimental/timer
	* include/experimental/tuple
	* include/experimental/type_traits
	* include/experimental/unordered_map
	* include/experimental/unordered_set
	* include/experimental/vector
	* include/ext/algorithm
	* include/ext/cmath
	* include/ext/functional
	* include/ext/iterator
	* include/ext/memory
	* include/ext/numeric
	* include/ext/random
	* include/ext/rb_tree
	* include/ext/rope
	* include/parallel/algorithm
	* include/std/algorithm
	* include/std/any
	* include/std/array
	* include/std/atomic
	* include/std/barrier
	* include/std/bit
	* include/std/bitset
	* include/std/charconv
	* include/std/chrono
	* include/std/codecvt
	* include/std/complex
	* include/std/concepts
	* include/std/condition_variable
	* include/std/coroutine
	* include/std/deque
	* include/std/execution
	* include/std/expected
	* include/std/filesystem
	* include/std/format
	* include/std/forward_list
	* include/std/fstream
	* include/std/functional
	* include/std/future
	* include/std/generator
	* include/std/iomanip
	* include/std/ios
	* include/std/iosfwd
	* include/std/iostream
	* include/std/istream
	* include/std/iterator
	* include/std/latch
	* include/std/limits
	* include/std/list
	* include/std/locale
	* include/std/map
	* include/std/memory
	* include/std/memory_resource
	* include/std/mutex
	* include/std/numbers
	* include/std/numeric
	* include/std/optional
	* include/std/ostream
	* include/std/print
	* include/std/queue
	* include/std/random
	* include/std/ranges
	* include/std/ratio
	* include/std/regex
	* include/std/scoped_allocator
	* include/std/semaphore
	* include/std/set
	* include/std/shared_mutex
	* include/std/span
	* include/std/spanstream
	* include/std/sstream
	* include/std/stack
	* include/std/stacktrace
	* include/std/stdexcept
	* include/std/streambuf
	* include/std/string
	* include/std/string_view
	* include/std/syncstream
	* include/std/system_error
	* include/std/text_encoding
	* include/std/thread
	* include/std/tuple
	* include/std/type_traits
	* include/std/typeindex
	* include/std/unordered_map
	* include/std/unordered_set
	* include/std/utility
	* include/std/valarray
	* include/std/variant
	* include/std/vector
	* include/std/version
	* include/tr1/array
	* include/tr1/cfenv
	* include/tr1/cinttypes
	* include/tr1/cmath
	* include/tr1/complex
	* include/tr1/cstdbool
	* include/tr1/cstdint
	* include/tr1/cstdio
	* include/tr1/cstdlib
	* include/tr1/cwchar
	* include/tr1/cwctype
	* include/tr1/functional
	* include/tr1/memory
	* include/tr1/random
	* include/tr1/regex
	* include/tr1/tuple
	* include/tr1/type_traits
	* include/tr1/unordered_map
	* include/tr1/unordered_set
	* include/tr1/utility
	* include/tr2/bool_set
	* include/tr2/dynamic_bitset
	* include/tr2/type_traits
	* libsupc++/atomic_lockfree_defines.h
	* libsupc++/compare
	* libsupc++/cxxabi.h
	* libsupc++/cxxabi_forced.h
	* libsupc++/cxxabi_init_exception.h
	* libsupc++/exception
	* libsupc++/initializer_list
	* libsupc++/new
	* libsupc++/typeinfo: Likewise.
	* testsuite/20_util/ratio/operations/ops_overflow_neg.cc
	* testsuite/23_containers/array/tuple_interface/get_neg.cc
	* testsuite/23_containers/vector/cons/destructible_debug_neg.cc
	* testsuite/24_iterators/operations/prev_neg.cc
	* testsuite/ext/type_traits/add_unsigned_floating_neg.cc
	* testsuite/ext/type_traits/add_unsigned_integer_neg.cc
	* testsuite/ext/type_traits/remove_unsigned_floating_neg.cc
	* testsuite/ext/type_traits/remove_unsigned_integer_neg.cc: Adjust
	line numbers.

gcc/testsuite/ChangeLog

	* g++.dg/analyzer/fanalyzer-show-events-in-system-headers-default.C
	* g++.dg/analyzer/fanalyzer-show-events-in-system-headers-no.C
	* g++.dg/diagnostic/disable.C: #define _GLIBCXX_SYSHDR.
2024-09-25 08:20:45 -04:00
Jonathan Wakely dc47add792 libstdc++: add default template parameters to algorithms
This implements P2248R8 + P3217R0, both approved for C++26.
The changes are mostly mechanical; the struggle is to keep readability
with the pre-P2248 signatures.

* For containers, "classic STL" algorithms and their parallel versions,
  introduce a macro and amend their declarations/definitions with it.
  The macro either expands to the defaulted parameter or to nothing
  in pre-C++26 modes.

* For range algorithms, we need to reorder their template parameters.
  I've done so unconditionally, because users cannot rely on template
  parameters of algorithms (this is explicitly authorized by
  [algorithms.requirements]/15). The defaults are then hidden behind
  another macro.

libstdc++-v3/ChangeLog:

	* include/bits/iterator_concepts.h: Add projected_value_t.
	* include/bits/algorithmfwd.h: Add the default template
	parameter to the relevant forward declarations.
	* include/pstl/glue_algorithm_defs.h: Likewise.
	* include/bits/ranges_algo.h: Add the default template
	parameter to range-based algorithms.
	* include/bits/ranges_algobase.h: Likewise.
	* include/bits/ranges_util.h: Likewise.
	* include/bits/ranges_base.h: Add helper macros.
	* include/bits/stl_iterator_base_types.h: Add helper macro.
	* include/bits/version.def: Add the new feature-testing macro.
	* include/bits/version.h: Regenerate.
	* include/std/algorithm: Pull the feature-testing macro.
	* include/std/ranges: Likewise.
	* include/std/deque: Pull the feature-testing macro, add
	the default for std::erase.
	* include/std/forward_list: Likewise.
	* include/std/list: Likewise.
	* include/std/string: Likewise.
	* include/std/vector: Likewise.
	* testsuite/23_containers/default_template_value.cc: New test.
	* testsuite/25_algorithms/default_template_value.cc: New test.

Signed-off-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
2024-09-22 17:45:05 +01:00
Jonathan Wakely a59f1cc31c libstdc++: Remove unused typedef in <ranges>
This local typedef should have been removed in r14-6199-g45630fbcf7875b.

libstdc++-v3/ChangeLog:

	* include/std/ranges (to): Remove unused typedef.
2024-08-28 21:34:21 +01:00
Patrick Palka 1066a95aa3 libstdc++: fix uses of explicit object parameter [PR116038]
The type of an implicit object parameter is always the current class.
For an explicit object parameter however, its deduced type can be a
derived class of the current class.  So when combining multiple
implicit-object overloads into a single explicit-object overload we need
to account for this possibility.  For example when accessing a member of
the current class through an explicit object parameter, it may now be a
derived class from which the member is not accessible, as in the below
testcases.

This pitfall is discussed[1] in the deducing this paper.  The general
solution is to cast the explicit object parameter to (a reference to)
the current class rather than e.g. using std::forward which preserves
the deduced type.

This patch corrects the existing problematic uses of explicit object
parameters in the library, all of which forward the parameter via
std::forward, to instead cast the parameter to the current class via
our __like_t alias template.  Note that unlike the paper's like_t,
ours always returns a reference so we can just write

  __like_t<Self, B>(self)

instead of

  (_like_t<Self, B>&&)self

as the paper does.

[1]: https://wg21.link/P0847#name-lookup-within-member-functions (and the
section after that)

	PR libstdc++/116038

libstdc++-v3/ChangeLog:

	* include/std/functional (_Bind_front::operator()): Use __like_t
	instead of std::forward when forwarding __self.
	(_Bind_back::operator()): Likewise.
	* include/std/ranges (_Partial::operator()): Likewise.
	(_Pipe::operator()): Likewise.
	* testsuite/20_util/function_objects/bind_back/116038.cc: New test.
	* testsuite/20_util/function_objects/bind_front/116038.cc: New test.
	* testsuite/std/ranges/adaptors/116038.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-07-25 09:02:13 -04:00
Alexandre Oliva 67be156f95 [libstdc++] add _GLIBCXX_CLANG to workaround predefined __clang__
A proprietary embedded operating system that uses clang as its primary
compiler ships headers that require __clang__ to be defined.  Defining
that macro causes libstdc++ to adopt workarounds that work for clang
but that break for GCC.

So, introduce a _GLIBCXX_CLANG macro, and a convention to test for it
rather than for __clang__, so that a GCC variant that adds -D__clang__
to satisfy system headers can also -D_GLIBCXX_CLANG=0 to avoid
workarounds that are not meant for GCC.

I've left fast_float and ryu files alone, their tests for __clang__
don't seem to be harmful for GCC, they don't include bits/c++config,
and patching such third-party files would just make trouble for
updating them without visible benefit.  pstl_config.h, though also
imported, required adjustment.


for  libstdc++-v3/ChangeLog

	* include/bits/c++config (_GLIBCXX_CLANG): Define or undefine.
	* include/bits/locale_facets_nonio.tcc: Test for it.
	* include/bits/stl_bvector.h: Likewise.
	* include/c_compatibility/stdatomic.h: Likewise.
	* include/experimental/bits/simd.h: Likewise.
	* include/experimental/bits/simd_builtin.h: Likewise.
	* include/experimental/bits/simd_detail.h: Likewise.
	* include/experimental/bits/simd_x86.h: Likewise.
	* include/experimental/simd: Likewise.
	* include/std/complex: Likewise.
	* include/std/ranges: Likewise.
	* include/std/variant: Likewise.
	* include/pstl/pstl_config.h: Likewise.
2024-06-05 22:43:54 -03:00
Patrick Palka 66d2a76dcf libstdc++: Implement ranges::concat_view from P2542R7
libstdc++-v3/ChangeLog:

	* include/bits/version.def (ranges_concat): Define.
	* include/bits/version.h: Regenerate.
	* include/std/ranges (__detail::__concat_reference_t): Define
	for C++26.
	(__detail::__concat_value_t): Likewise.
	(__detail::__concat_rvalue_reference_t): Likewise.
	(__detail::__concat_indirectly_readable_impl): Likewise.
	(__detail::__concat_indirectly_readable): Likewise.
	(__detail::__concatable): Likewise.
	(__detail::__all_but_last_common): Likewise.
	(__detail::__concat_is_random_access): Likewise.
	(__detail::__concat_is_bidirectional): Likewise.
	(__detail::__last_is_common): Likewise.
	(concat_view): Likewise.
	(__detail::__concat_view_iter_cat): Likewise.
	(concat_view::iterator): Likewise.
	(views::__detail::__can_concat_view): Likewise.
	(views::_Concat, views::concat): Likewise.
	* testsuite/std/ranges/concat/1.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-05-23 18:03:56 -04:00
Patrick Palka 0e64bbb882 libstdc++: Allow adjacent __maybe_present_t<false, ...> fields to overlap
Currently __maybe_present_t<false, T> maps to the same empty class
type independent of T.  This is suboptimal because it means adjacent
__maybe_present_t<false, ...> members with the [[no_unique_address]]
attribute can't overlap even if the conditionally present types are
different.

This patch turns this empty class type into a template parameterized by
the conditionally present type, so that

  [[no_unique_address]] __maybe_present_t<false, T> _M_a;
  [[no_unique_address]] __maybe_present_t<false, U> _M_b;

now overlap if T and U are different.

This patch goes a step further and also adds an optional integer
discriminator parameter to allow for overlapping when T and U are
the same.

libstdc++-v3/ChangeLog:

	* include/std/ranges (ranges::__detail::_Empty): Rename to ...
	(ranges::__detail::_Absent): ... this.  Turn into a template
	parameterized by the absent type _Tp and discriminator _Disc.
	(ranges::__detail::__maybe_present_t): Add an optional
	discriminator parameter.
	(slide_view::_M_cached_begin): Pass a discriminator argument to
	__maybe_present_t.
	(slide_view::_M_cached_end): Likewise.
	* testsuite/std/ranges/adaptors/sizeof.cc: Verify the size of
	slide_view<V> is 3 instead 4 pointers.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-04-02 13:07:07 -04:00
Patrick Palka 65b4cba9d6 libstdc++: Implement P2165R4 changes to std::pair/tuple/etc [PR113309]
This implements the C++23 paper P2165R4 Compatibility between tuple,
pair and tuple-like objects, which builds upon many changes from the
earlier C++23 paper P2321R2 zip.

Some declarations had to be moved around so that they're visible from
<bits/stl_pair.h> without introducing new includes and bloating the
header.  In the end, the only new include is for <bits/utility.h> from
<bits/stl_iterator.h>, for tuple_element_t.

	PR libstdc++/113309
	PR libstdc++/109203

libstdc++-v3/ChangeLog:

	* include/bits/ranges_util.h (__detail::__pair_like): Don't
	define in C++23 mode.
	(__detail::__pair_like_convertible_from): Adjust as per P2165R4.
	(__detail::__is_subrange<subrange>): Moved from <ranges>.
	(__detail::__is_tuple_like_v<subrange>): Likewise.
	* include/bits/stl_iterator.h: Include <bits/utility.h> for
	C++23.
	(__different_from): Move to <concepts>.
	(__iter_key_t): Adjust for C++23 as per P2165R4.
	(__iter_val_t): Likewise.
	* include/bits/stl_pair.h (pair, array): Forward declare.
	(get): Forward declare all overloads relevant to P2165R4
	tuple-like constructors.
	(__is_tuple_v): Define for C++23.
	(__is_tuple_like_v): Define for C++23.
	(__tuple_like): Define for C++23 as per P2165R4.
	(__pair_like): Define for C++23 as per P2165R4.
	(__eligibile_tuple_like): Define for C++23.
	(__eligibile_pair_like): Define for C++23.
	(pair::_S_constructible_from_pair_like): Define for C++23.
	(pair::_S_convertible_from_pair_like): Define for C++23.
	(pair::_S_dangles_from_pair_like): Define for C++23.
	(pair::pair): Define overloads taking a tuple-like type for
	C++23 as per P2165R4.
	(pair::_S_assignable_from_tuple_like): Define for C++23.
	(pair::_S_const_assignable_from_tuple_like): Define for C++23.
	(pair::operator=): Define overloads taking a tuple-like type for
	C++23 as per P2165R4.
	* include/bits/utility.h (ranges::__detail::__is_subrange):
	Moved from <ranges>.
	* include/bits/version.def (tuple_like): Define for C++23.
	* include/bits/version.h: Regenerate.
	* include/std/concepts (__different_from): Moved from
	<bits/stl_iterator.h>.
	(ranges::__swap::__adl_swap): Clarify which __detail namespace.
	* include/std/map (__cpp_lib_tuple_like): Define C++23.
	* include/std/ranges (__detail::__is_subrange): Moved to
	<bits/utility.h>.
	(__detail::__is_subrange<subrange>): Moved to <bits/ranges_util.h>
	(__detail::__has_tuple_element): Adjust for C++23 as per P2165R4.
	(__detail::__tuple_or_pair): Remove as per P2165R4.  Replace all
	uses with plain tuple as per P2165R4.
	* include/std/tuple (__cpp_lib_tuple_like): Define for C++23.
	(__tuple_like_tag_t): Define for C++23.
	(__tuple_cmp): Forward declare for C++23.
	(_Tuple_impl::_Tuple_impl): Define overloads taking
	__tuple_like_tag_t and a tuple-like type for C++23.
	(_Tuple_impl::_M_assign): Likewise.
	(tuple::__constructible_from_tuple_like): Define for C++23.
	(tuple::__convertible_from_tuple_like): Define for C++23.
	(tuple::__dangles_from_tuple_like): Define for C++23.
	(tuple::tuple): Define overloads taking a tuple-like type for
	C++23 as per P2165R4.
	(tuple::__assignable_from_tuple_like): Define for C++23.
	(tuple::__const_assignable_from_tuple_like): Define for C++23.
	(tuple::operator=): Define overloads taking a tuple-like type
	for C++23 as per P2165R4.
	(tuple::__tuple_like_common_comparison_category): Define for C++23.
	(tuple::operator<=>): Define overload taking a tuple-like type
	for C++23 as per P2165R4.
	(array, get): Forward declarations moved to <bits/stl_pair.h>.
	(tuple_cat): Constrain with __tuple_like for C++23 as per P2165R4.
	(apply): Likewise.
	(make_from_tuple): Likewise.
	(__tuple_like_common_reference): Define for C++23.
	(basic_common_reference): Adjust as per P2165R4.
	(__tuple_like_common_type): Define for C++23.
	(common_type): Adjust as per P2165R4.
	* include/std/unordered_map (__cpp_lib_tuple_like): Define for
	C++23.
	* include/std/utility (__cpp_lib_tuple_like): Define for C++23.
	* testsuite/std/ranges/zip/1.cc (test01): Adjust to handle pair
	and 2-tuple interchangeably.
	(test05): New test.
	* testsuite/20_util/pair/p2165r4.cc: New test.
	* testsuite/20_util/tuple/p2165r4.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-02-01 14:59:46 -05:00
Marek Polacek 7db802d972 libstdc++: suppress -Wdangling-reference with operator| [PR111410]
It seems to me that we should exclude std::ranges::views::__adaptor::operator|
from the -Wdangling-reference warning.  It's commonly used when handling
ranges.

	PR c++/111410

libstdc++-v3/ChangeLog:

	* include/std/ranges: Add #pragma to disable -Wdangling-reference with
	std::ranges::views::__adaptor::operator|.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wdangling-reference17.C: New test.
2024-01-20 15:41:52 -05:00
Patrick Palka 98966f32f9 libstdc++: Implement P2540R1 change to views::cartesian_product()
This paper changes the identity element of views::cartesian_product to a
singleton range instead of an empty range.  It was approved alongside
the main cartesian_product paper P2374R4, but unfortunately was overlooked
when implementing the main paper.

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::_CartesianProduct::operator()):
	Adjust identity case as per P2540R1.
	* testsuite/std/ranges/cartesian_product/1.cc (test01):
	Adjust expected result of the identity case.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2024-01-16 21:20:12 -05:00