aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include
AgeCommit message (Collapse)AuthorFilesLines
2025-06-25libstdc++: Report compilation error on formatting "%d" from month_last ↵Tomasz Kamiński1-2/+1
[PR120650] For month_day we incorrectly reported day information to be available, which lead to format_error being thrown from the call to formatter::format at runtime, instead of making call to format ill-formed. The included test cover most of the combinations of _ChronoParts and format specifiers. PR libstdc++/120650 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (formatter<chrono::month_day_last,_CharT>::parse): Call _M_parse with only Month being available. * testsuite/std/time/format/data_not_present_neg.cc: New test.
2025-06-24libstdc++: Unnecessary type completion in __is_complete_or_unbounded [PR120717]Patrick Palka1-17/+22
When checking __is_complete_or_unbounded on a reference to incomplete type, we overeagerly try to instantiate/complete the referenced type which besides being unnecessary may also produce an unexpected -Wsfinae-incomplete warning (added in r16-1527) if the referenced type is later defined. This patch fixes this by effectively restricting the sizeof check to object (except unknown-bound array) types. In passing simplify the implementation by using is_object instead of is_function/reference/void and introducing a __maybe_complete_object_type helper. PR libstdc++/120717 libstdc++-v3/ChangeLog: * include/std/type_traits (__maybe_complete_object_type): New helper trait, factored out from ... (__is_complete_or_unbounded): ... here. Only check sizeof on a __maybe_complete_object_type type. Fix formatting. * testsuite/20_util/is_complete_or_unbounded/120717.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Co-authored-by: Jonathan Wakely <jwakely@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-13libstdc++: Fix std::uninitialized_value_construct for arrays [PR120397]Jonathan Wakely1-0/+20
The std::uninitialized_{value,default}_construct{,_n} algorithms should be able to create arrays, but that currently fails because when an exception happens they clean up using std::_Destroy and in C++17 that doesn't support destroying arrays. (For C++20 and later, std::destroy does handle destroying arrays.) This commit adjusts the _UninitDestroyGuard RAII type used by those algos so that in C++17 mode it recursively destroys each rank of an array type, only using std::_Destroy for the last rank when it's destroying non-array objects. libstdc++-v3/ChangeLog: PR libstdc++/120397 * include/bits/stl_uninitialized.h (_UninitDestroyGuard<I,void>): Add new member function _S_destroy and call it from the destructor (for C++17 only). * testsuite/20_util/specialized_algorithms/uninitialized_default_construct/120397.cc: New test. * testsuite/20_util/specialized_algorithms/uninitialized_value_construct/120397.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-13libstdc++: Format %r, %x and %X using locale's time_put facet [PR120648]Tomasz Kamiński1-94/+34
Similarly to issue reported for %c in PR117214, the format string for locale specific time (%r, %X) and date (%x) representations may contain specifiers not accepted by chrono-spec, leading to exception being thrown. This happened for following conversion specifier and locale combinations: * %r, %X for aa_DJ.UTF-8, ar_SA.UTF-8 * %x for ca_AD.UTF-8, my_MM.UTF-8 This fix follows approach from r15-8490-gc24a1d5, and uses time_put to emit localized date format. The existing _M_c is reworked to handle all locale dependent conversion specifies, by accepting them as argument. It is also renamed to _M_c_r_x_X. PR libstdc++/120648 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format_to): Handle %c, %r, %x and %X by passing them to _M_c_r_x_X. (__formatter_chrono::_M_c_r_x_X): Reworked from _M_c. (__formatter_chrono::_M_c): Renamed into above. (__formatter_chrono::_M_r, __formatter_chrono::_M_x) (__formatter_chrono::_M_X): Removed. * testsuite/std/time/format/pr117214.cc: New tests for %r, %x, %X with date, time and durations.
2025-06-13libstdc++: Optimize __make_comp/pred_proj for empty/scalar typesPatrick Palka1-16/+48
When creating a composite comparator/predicate that invokes a given projection function, we don't need to capture a scalar (such as a function pointer or member pointer) or empty object by reference, instead capture it by value and use [[no_unique_address]] to elide its storage (in the empty case). This makes using __make_comp_proj zero-cost in the common case where both functions are empty/scalars. libstdc++-v3/ChangeLog: * include/bits/ranges_algo.h (__detail::__by_ref_or_value_fn): New. (__detail::_Comp_proj): New. (__detail::__make_comp_proj): Use it instead. (__detail::_Pred_proj): New. (__detail::__make_pred_proj): Use it instead. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-13libstdc++: add a workaround for format_kind<optional<T>> [PR120644]Giuseppe D'Angelo1-1/+1
The specialization of format_kind for optional is causing a problem when optional is imported and included. The comments on the PR strongly suggest that this is a frontend bug; this commit just works around the issue by specifying the type of format_kind<optional<T>> to be `range_format`, rather than leaving the compiler deduce it via `auto`. PR c++/120644 libstdc++-v3/ChangeLog: * include/std/optional (format_kind): Do not use `auto`.
2025-06-13libstdc++: Rework formatting of empty chrono-spec for duration.Tomasz Kamiński1-43/+45
In contrast to other calendar types if empty chrono-spec is used for duration we are required to format it (and its representation type) via ostream. Handling this case was now moved to be part of the format function for duration. To facilitate that __formatter_chrono::_M_format_to_ostream function was made public. However, for standard integral types, we know the result of inserting them into ostream, and in consequence we can format them directly. This is handled by configuring default format spec to "%Q%q" for such types. As we no longer use __formatter_chrono::_M_format with empty chrono-spec, this function now requires that _M_chrono_specs are not empty, and conditional call to _M_format_to_ostream is removed. This allows _M_format_to_ostream to be reduced to accept only duration. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format): Remove handling of empty _M_chrono_specs. (__formatter_chrono::_M_format_to_ostream): Changed to accept only chrono::duration and made public. (std::formatter<chrono::duration<_Rep, _Period>, _CharT>): Configure __defSpec and handle empty chrono-spec locally. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-13libstdc++: Format empty chrono-spec for the sys_info and local_info directly.Tomasz Kamiński1-15/+85
This patch change implementation of the formatters for sys_info and local_info, so they no longer delegate to operator<< for ostream in case of empty spec. As this types may be only formatted with chrono-spec containing only %%, %t, %n specifiers and fill characters, we use a separate __formatter_chrono_info formatter. For empty chron-spec __formatter_chrono_info formats sys_info using format_to call with format specifier extracted from corresponding operator<<, that now delegates to format with empty spec. For local_info we replicate functionality of the operator<<. The alignment and padding is handled using an _Padding_sink. For non-empty spec, we delegate to __formatter_chrono::_M_format. As non-of the format specifiers depends on the formatted object, we pass chrono::day to avoid triggering additional specializations. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__format::__formatter_chrono_info) [_GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI]: Define. (std::formatter<chrono::sys_info, _CharT>) (std::formatter<chrono::local_inf, _CharT>): Delegate to __format::__formatter_chrono_info. (std::operator<<(basic_ostream<_CharT, _Traits>& const sys_info&)): Use format on sys_info with empty format spec. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: do not use an unreserved name in _Temporary_buffer [PR119496]Giuseppe D'Angelo2-3/+3
As the PR observes, _Temporary_buffer was using an unreserved name for a member function that can therefore clash with macros defined by the user. Avoid that by renaming the member function. PR libstdc++/119496 libstdc++-v3/ChangeLog: * include/bits/stl_algo.h: Adjust calls to requested_size. * include/bits/stl_tempbuf.h (requested_size): Rename with an _M_ prefix. * testsuite/17_intro/names.cc: Add a #define for requested_size. Signed-off-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2025-06-12libstdc++: add range support to std::optional (P3168)Giuseppe D'Angelo5-26/+91
This commit implements P3168 ("Give std::optional Range Support"), added for C++26. Both begin() and end() are straightforward, implemented using normal_iterator over a raw pointer. std::optional is also a view, so specialize enable_view for it. We also need to disable automatic formatting a std::optional as a range by specializing format_kind. In order to avoid dragging <format> when including <optional>, I've isolated format_kind and some supporting code into <bits/formatfwd.h> so that I can use that (comparatively) lighter header. libstdc++-v3/ChangeLog: * include/bits/formatfwd.h (format_kind): Move the definition (and some supporting code) from <format>. * include/std/format (format_kind): Likewise. * include/bits/version.def (optional_range_support): Add the feature-testing macro. * include/bits/version.h: Regenerate. * include/std/optional (iterator, const_iterator, begin, end): Add range support. (enable_view): Specialize for std::optional. (format_kind): Specialize for std::optional. * testsuite/20_util/optional/range.cc: New test. * testsuite/20_util/optional/version.cc: Test the new feature-testing macro.
2025-06-12libstdc++: Format empty chrono-spec for the time points and hh_mm_ss directly.Tomasz Kamiński1-85/+135
This patch change implementation of the formatters for time points and hh_mm_ss, so they no longer delegate to operator<< for ostream in case of empty chrono-spec. As in case of calendar types, the formatters for specific type now provide __formatter_chrono with default _ChronoSpec that are used in case if empty chrono-spec. The configuration of __defSpec is straight forward, except for the sys_time, and local_time that print time, if the duration is convertible to days, which is equivalent to setting _M_chrono_specs "%F" instead of "%F %T". Furthermore, certain sys_time<Dur> do not support ostream operator, and should not be formattable with empty spec - in such case default _M_chrono_spec, allowing the issue to still be detected in _M_parse. Finally, _ChronoFormats are extended to cover required format strings. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (_ChronoFormats::_S_ftz) (_ChronoFormats::_S_ft, _ChronoFormats::_S_t): Define. (__formatter_chrono::_M_format_to_ostream): Remove handling for time_points. (std::formatter<chrono::hh_mm_ss<_Dur>, _CharT>) (std::formatter<chrono::sys_time<_Dur>, _CharT>) (std::formatter<chrono::utc_time<_Dur>, _CharT>) (std::formatter<chrono::tai_time<_Dur>, _CharT>) (std::formatter<chrono::gps_time<_Dur>, _CharT>) (std::formatter<chrono::file_time<_Dur>, _CharT>) (std::formatter<chrono::local_time<_Dur>, _CharT>) (std::formatter<chrono::__detail::__local_time_fmt<_Dur>, _CharT>) (std::formatter<chrono::zoned_time<_Dur>, _CharT>): Define __defSpec, and pass it as argument to _M_prase and constructor of __formatter_chrono.
2025-06-12libstdc++: Format empty chrono-spec for the calendar types directly.Tomasz Kamiński1-37/+361
This patch change implementation of the formatters for the calendar types, so they no longer delegate to operator<< for ostream in case of empty chrono-spec. Instead of that, we define the behavior in terms of format specifiers supplied by each formatter as an argument to _M_parse. Similarly each formatter constructs its __formatter_chrono from a relevant default spec, preserving the functionality of calling format on default constructed formatters. Expressing the existing functionality of the operator ostream, requires providing two additional features: * printing "is not a valid sth" for !ok objects, * printing a weekday index in the month. The formatter functionality is enabled by setting spec _M_debug (corresponding to '?') that is currently unused. This is currently supported only for subset of format specifiers used by the ostream operators. In future, we could make this user configurable (by adding '?' after 'L') and cover all flags. For the handling of the weekday index (for weekday_indexed, month_weekday, year_month_weekday), we need to introduce a new format specifier. To not conflict with future extension we use '%\0' (embedded null) as this character cannot be placed in valid format spec. Finally, the format strings for calendar types subsets each other, e.g. year_month_weekday_last ("%Y/%b/%a[last])" contains month_weekday_last, weekday_last, weekday, e.t.c.. We introduce a _ChronoFormats class that provide consteval accessors to format specs, internally sharing they representations. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__format::_ChronoFormats): Define. (__formatter_chrono::__formatter_chrono()) (__formatter_chrono::__formatter_chrono(_ChronoSpec<_CharT>)): Define. (__formatter_chrono::_M_parse): Add parameter with default spec, and merge it with new values. Handle '%\0' as weekday index specifier. (__formatter_chrono::_M_a_A, __formatter_chrono::_M_b_B) (__formatter_chrono::_M_C_y_Y, __formatter_chrono::_M_d_e) (__formatter_chrono::_M_F): Support _M_debug flag. (__formatter_chrono::_M_wi, __formatter_chrono::_S_weekday_index): Define. (std::formatter<chrono::day, _CharT>) (std::formatter<chrono::month, _CharT>) (std::formatter<chrono::year, _CharT>) (std::formatter<chrono::weekday, _CharT>) (std::formatter<chrono::weekday_indexed, _CharT>) (std::formatter<chrono::weekday_last, _CharT>) (std::formatter<chrono::month_day, _CharT>) (std::formatter<chrono::month_day_last, _CharT>) (std::formatter<chrono::month_weekday, _CharT>) (std::formatter<chrono::month_weekday_last, _CharT>) (std::formatter<chrono::year_month, _CharT>) (std::formatter<chrono::year_month_day, _CharT>) (std::formatter<chrono::year_month_day_last, _CharT>) (std::formatter<chrono::year_month_weekday, _CharT>) (std::formatter<chrono::year_month_weekday_last, _CharT>): Define __defSpec, and pass it as argument to _M_parse and constructor of __formatter_chrono. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Uglify __mapping_alike template parameter and fix test and typo ↵Tomasz Kamiński1-11/+11
in comment. When the static assert was generated from instantiations of default member initializer of class B, the error was not generated for B<1, std::layout_left, std::layout_left> case, only when -D_GLIBCXX_DEBUG was set. Changing B calls to functions fixes that. We also replace class with typename in template head of layout_right::mapping constructors. libstdc++-v3/ChangeLog: * include/std/mdspan (__mdspan::__mapping_alike): Rename template parameter from M to _M_p. (layout_right::mapping): Replace class with typename in template head. (layout_stride::mapping): Fix typo in comment. * testsuite/23_containers/mdspan/layouts/class_mandate_neg.cc: Changed B to function. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Make layout_left(layout_stride) noexcept.Luc Grosheintz1-1/+2
[mdspan.layout.left.cons] of N4950 states that this ctor is not noexcept. Since, all other ctors of layout_left, layout_right or layout_stride are noexcept, the choice was made, based on [res.on.exception.handling], to make this ctor noexcept. Two other major standard library implementations make the same choice. libstdc++-v3/ChangeLog: * include/std/mdspan (layout_left): Strengthen the exception guarantees of layout_left::mapping(layout_stride::mapping). * testsuite/23_containers/mdspan/layouts/ctors.cc: Simplify tests to reflect the change. Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Implement layout_stride from mdspan.Luc Grosheintz1-1/+248
Implements the remaining parts of layout_left and layout_right; and all of layout_stride. The implementation of layout_stride::mapping::is_exhaustive applies the following change to the standard: 4266. layout_stride::mapping should treat empty mappings as exhaustive https://cplusplus.github.io/LWG/issue4266 The preconditions for layout_stride(extents, strides) are not checked. libstdc++-v3/ChangeLog: * include/std/mdspan (layout_stride): New class. * src/c++23/std.cc.in: Add layout_stride. Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Implement layout_right from mdspan.Luc Grosheintz1-1/+152
Implement the parts of layout_left that depend on layout_right; and the parts of layout_right that don't depend on layout_stride. libstdc++-v3/ChangeLog: * include/std/mdspan (layout_right): New class. * src/c++23/std.cc.in: Add layout_right. Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Implement layout_left from mdspan.Luc Grosheintz1-1/+302
Implements the parts of layout_left that don't depend on any of the other layouts. libstdc++-v3/ChangeLog: * include/std/mdspan (layout_left): New class. * src/c++23/std.cc.in: Add layout_left. Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Improve naming, whitespace and silence warnings for extents.Luc Grosheintz1-12/+12
libstdc++-v3/ChangeLog: * include/std/mdspan(__mdspan::_ExtentsStorage): Change name of private member _M_dynamic_extens to _M_dyn_exts. (extents): Change name of private member from _M_dynamic_extents to _M_exts. Fix two instances of whitespace errors. * testsuite/23_containers/mdspan/extents/ctor_default.cc: Fix integer comparison with cmp_equal. Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Do not specialize std::formatter for incomplete type [PR120625]Jonathan Wakely1-7/+11
Using an incomplete type as the template argument for std::formatter specializations causes problems for program-defined specializations of std::formatter which have constraints. When the compiler has to find which specialization of std::formatter to use for the incomplete type it considers the program-defined specializations and checks to see if their constraints are satisfied, which can give errors if the constraints cannot be checked for incomplete types. This replaces the base class of the disabled specializations with a concrete class __formatter_disabled, so there is no need to match a specialization and no more incomplete type. libstdc++-v3/ChangeLog: PR libstdc++/120625 * include/std/format (__format::__disabled): Remove. (__formatter_disabled): New type. (formatter<char*, wchar_t>, formatter<const char*, wchar_t>) (formatter<char[N], wchar_t>, formatter<string, wchar_t>) (formatter<string_view, wchar_t>): Use __formatter_disabled as base class instead of formatter<__disabled, wchar_t>. * testsuite/std/format/formatter/120625.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-11libstdc++: Fix mismatched @cond and @endcond in <type_traits>Jonathan Wakely1-0/+3
I messed up the Doxygen conditionals in r16-1077-gb32bf304793047. libstdc++-v3/ChangeLog: * include/std/type_traits: Restore @cond and @endcond balance.
2025-06-11libstdc++: Replace some uses of std::__addressof with std::addressofJonathan Wakely2-22/+22
Since r16-154-gc91eb5a5c13f14 std::addressof is no less efficient than std::__addressof, so change some uses of the latter to the former. We can't change them all, because some uses need to compile as C++98 which only has std::__addressof. Similarly, since r16-848-gb2aeeb2803f97b std::is_constant_evaluated is no less efficient than std::__is_constant_evaluated. libstdc++-v3/ChangeLog: * include/bits/stl_construct.h: Replace std::__addressof with std::addressof in code that doesn't need to compile as C++98. Replace std::__is_constant_evaluated with std::is_constant_evaluated in code that doesn't need to compile as C++17 or earlier. * include/bits/stl_uninitialized.h: Likewise for __addressof.
2025-06-11libstdc++: Fix whitespace before comments in <sstream>Jonathan Wakely1-6/+6
libstdc++-v3/ChangeLog: * include/std/sstream: Adjust whitespace.
2025-06-11libstdc++: Improve diagnostics for ill-formed std::_Destroy and ↵Jonathan Wakely1-36/+20
std::_Destroy_n [PR120390] By using std::is_trivially_destructible instead of the old __has_trivial_destructor built-in we no longer need the static_assert to deal with types with deleted destructors. All non-destructible types, including those with deleted destructors, will now give user-friendly diagnostics that clearly explain the problem. Also combine the _Destroy_aux and _Destroy_n_aux class templates used for C++98 into one, so that we perform fewer expensive class template instantiations. libstdc++-v3/ChangeLog: PR libstdc++/120390 * include/bits/stl_construct.h (_Destroy_aux::__destroy_n): New static member function. (_Destroy_aux<true>::__destroy_n): Likewise. (_Destroy_n_aux): Remove. (_Destroy(ForwardIterator, ForwardIterator)): Remove static_assert. Use is_trivially_destructible instead of __has_trivial_destructor. (_Destroy_n): Likewise. Use _Destroy_aux::__destroy_n instead of _Destroy_n_aux::__destroy_n. * testsuite/20_util/specialized_algorithms/memory_management_tools/destroy_neg.cc: Adjust dg-error strings. Move destroy_n tests to ... * testsuite/20_util/specialized_algorithms/memory_management_tools/destroy_n_neg.cc: New test. * testsuite/23_containers/vector/cons/destructible_debug_neg.cc: Adjust dg-error strings. * testsuite/23_containers/vector/cons/destructible_neg.cc: Likewise. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-10libstdc++: Make __max_size_type and __max_diff_type structuralPatrick Palka1-2/+2
This patch makes these integer-class types structural types by public-izing their data members so that they could be used as NTTP types. I don't think this is required by the standard, but it seems like a useful extension. libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (__max_size_type::_M_val): Make public instead of private. (__max_size_type::_M_msb): Likewise. (__max_diff_type::_M_rep): Likewise. * testsuite/std/ranges/iota/max_size_type.cc: Verify __max_diff_type and __max_size_type are structural. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-10libstdc++: Implement LWG3528 make_from_tuple can perform (the equivalent of) ↵Yihan Wang1-2/+22
a C-style cast Implement LWG3528 to make std::make_from_tuple SFINAE friendly. libstdc++-v3/ChangeLog: * include/std/tuple (__can_make_from_tuple): New variable template. (__make_from_tuple_impl): Add static_assert. (make_from_tuple): Constrain using __can_make_from_tuple. * testsuite/20_util/tuple/dr3528.cc: New test. Signed-off-by: Yihan Wang <yronglin777@gmail.com> Co-authored-by: Jonathan Wakely <jwakely@redhat.com> Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-09libstdc++: sstream from string_view (P2495R3) [PR119741]Nathan Myers3-19/+204
Add constructors to stringbuf, stringstream, istringstream, and ostringstream, and a matching overload of str(sv) in each, that take anything convertible to a string_view in places where the existing ctors and function take a string. Note this change omits the constraint applied to the istringstream constructor from string cited as a "drive-by" in P2495R3, as we have determined it is redundant. libstdc++-v3/ChangeLog: PR libstdc++/119741 * include/std/sstream: full implementation, really just decls, requires clause and plumbing. * include/bits/version.def, include/bits/version.h: new preprocessor symbol __cpp_lib_sstream_from_string_view. * testsuite/27_io/basic_stringbuf/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_istringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_ostringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_stringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_stringbuf/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_istringstream/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_ostringstream/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_stringstream/cons/wchar_t/string_view.cc: New tests. Reviewed-by: Jonathan Wakely
2025-06-06libstdc++: Use std::conditional_t instead of lambda to select semaphore ↵Jonathan Wakely2-18/+8
implementation The lambda expression causes testsuite failures such as: FAIL g++.dg/modules/xtreme-header-2_b.C -std=c++26 (test for excess errors) libstdc++-v3/ChangeLog: * include/bits/semaphore_base.h (_Select_semaphore_impl): Rename to _Semaphore_impl and use std::conditional_t instead of an immediately invoked lambda expression. * include/std/semaphore (counting_semaphore): Adjust to use new name.
2025-06-06libstdc++: Fix flat_map::operator[] for const lvalue keys [PR120432]Patrick Palka1-3/+3
The const lvalue operator[] overload wasn't properly forwarding the key type to the generic overload, causing a hard error for const keys. Rather than correcting the forwarded type this patch just makes the non-template overloads call try_emplace directly instead. That way we can remove the non-standard same_as constraint on the generic overload and match the spec more closely. PR libstdc++/120432 libstdc++-v3/ChangeLog: * include/std/flat_map (flat_map::operator[]): Make the non-template overloads call try_emplace directly. Remove non-standard same_as constraint on the template overload. * testsuite/23_containers/flat_map/1.cc (test08): New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-06libstdc++: Add assertions to atomic waiting functions that need platform waitJonathan Wakely2-0/+9
These overloads should never be used for proxy waits, so add assertions to ensure that they aren't used accidentally. The reason they can't be used is that they don't call __args._M_setup_wait to obtain a __wait_state pointer. Even if that was changed, they would wait on a proxy wait which is potentially used by many other threads waiting on other addresses, meaning spurious wake ups are likely. In order to make the functions correct they would need to perform additional loads and comparisons of the atomic variable before calling __wait_impl or __wait_until_impl, which would make these functions no faster than the general purpose overloads that take an accessor function and predicate. That would be possible, and I think they would then work for proxy waits, but doesn't seem necessary at this time. In order to preseve the property that these functions are more lightweight and efficient than the general ones, they should not be used for proxy waits. libstdc++-v3/ChangeLog: * include/bits/atomic_timed_wait.h (__atomic_wait_address_until_v): Add assertion to prevent use with proxy waits. (__atomic_wait_address_for_v): Likewise. * include/bits/atomic_wait.h (__atomic_wait_address_v): Likewise.
2025-06-06libstdc++: Optimize std::counting_semaphore for futex pathJonathan Wakely3-57/+183
Rename __semaphore_base to __semaphore_impl, because it's not used as a base class. Replace the three identical lambda expressions with a named class, __semaphore_impl::_Available, which stores the most recent value of the counter as a data member, and provides call operators that test whether the value is decrementable (i.e. whether the semaphore can be acquired). Add a new __platform_semaphore_impl class template to be used when __platform_wait is available, which uses __platform_wait_t for the counter and uses more efficient atomic waits for the acquire functions. For a binary semaphore some members are further optimized because we know the counter can only be zero or one. Also add a bare wait flag to __atomic_wait_address_v, for consistency with __atomic_wait_address_until_v and __atomic_wait_address_for_v and to allow semaphores to use it without the redundant overhead of tracking waiters. libstdc++-v3/ChangeLog: * include/bits/atomic_wait.h (__atomic_wait_address_v): Add bare wait flag. * include/bits/semaphore_base.h (__semaphore_base): Rename to __semaphore_impl. Replace local variable and predicate lambdas with _Available struct. (__platform_semaphore_impl): New class template. (__semaphore_impl): Remove alias template. (_Select_semaphore_impl): New alias template. * include/std/semaphore (counting_semaphore): Use _Select_semaphore_impl.
2025-06-06libstdc++: Support wide characters output for sys_info and local_info [PR120565]Tomasz Kamiński1-9/+14
Formatting sys_info as wchar_t require widening of the abbrev (zone) member. To support that we reuse the existing code in support for '%Z' specifier, for local_time_format, and produce output using singe format call with "[{0:%F %T},{1:%F %T},{2:%T},{3:%Q%q},{0:%Z}]" format string. As noted in the comment, produced output is locale independed, as it does not contain decimal separtors. For sys_info, the outputed literals are widended using _GLIBCXX_WIDEN, except opening and closing brackets, that are fetched from __format::_Separators. PR libstdc++/120565 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (operator<<(basic_ostream<_CharT, _Traits>&, const sys_info&)) (operator<<(basic_ostream<_CharT, _Traits>&, const local_info&)): Support wchar_t as _CharT. * testsuite/std/time/format/empty_spec.cc: Instantiated test_infos for wchar_t and increase timeout. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-05libstdc++: Fix std::format thousands separators when sign present [PR120548]Jonathan Wakely1-2/+9
The leading sign character should be skipped when deciding whether to insert thousands separators into a floating-point format. libstdc++-v3/ChangeLog: PR libstdc++/120548 * include/std/format (__formatter_fp::_M_localize): Do not include a leading sign character in the string to be grouped. * testsuite/std/format/functions/format.cc: Check grouping when sign is present in the output. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-05libstdc++: Fix formatting of 3-digits months,day,weekday and hour [PR120481]Tomasz Kamiński1-65/+152
This patch fixes the handle multiple digits values for the month, day, weekday and hour, when used with the %m, %d, %e, %m, %u, %w, %H, and %D, %F specifiers. The values are now printed unmodified. This patch also fixes printing negative year with %F, where the values was not padded to four digits. Furthemore, the %I,%p are adjusted to handle input with hours values set to over 24 hours. In the case the values is interpretd modulo 24. This was already the case for %r (locale's 12-hour clock), as we convert the input into seconds. In case of %u, %w we print values unchanged, this makes the behavior of this specifiers equivalent to printing the iso_encoding and c_encoding respectively. As constructing weekday from value 7, initializes it with 0, the !ok() weekdays values are always greater of equal eight, so they are clearly distinguishable. The months, weekday, day values that can have 3 decimal digit as maximum (range [0, 255]), we are using new _S_str_d1, _S_str_d2 that return string_view containing textual representation, without padding or padded to two digits. This function accepts are 3 character buffer, that are used for 3 digits number. In other cases, we return _S_digit and _S_two_digits result directly. The former is changed to return string_view to facilitate this. For %F and %D when at least one component have more digits that expected (2 for month and day, 4 for year), we produce output using format_to with appropriate format string. Otherwise the representation is produced in local char buffer. Two simply fill this buffer, _S_fill_two_digits function was added. We also make sure that minus is not included in year width for %F. The handling of %C, %Y, %y was adjusted to use similar pattern, for years with more than two digits. To support that the order of characters in _S_chars was adjusted so it contain "-{}" string. For handling of %H, we print 3 or more digits values using format_to. The handling for large hours values in %T and %R was changed, so they printed using format_to, and otherwise we use same stack buffer as for minutes to print them. PR libstdc++/120481 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__format::_S_chars): Reorder so it contains "-{}". (__format::_S_colon, __format::_S_slash, __format::_S_space) (__format::_S_plus_minus): Updated starting indicies. (__format::_S_minus_empty_spec): Define. (__formatter_chrono::_M_C_y_Y, __formatter_chrono::_M_R_T): Rework implementation. (__formatter_chrono::_M_d_e, __formatter_chrono::_M_F) (__formatter_chrono::_M_m, __formatter_chrono::_M_u_w) (__formatter_chrono::_M_H_I, __formatter_chrono::_M_p): Handle multi digits values. (__formatter_chrono::_S_digit): Return string view. (__formatter_chrono::_S_str_d1, __formatter_chrono::_S_str_d2) (__formatter_chrono::_S_fill_two_digits): Define. * testsuite/std/time/format/empty_spec.cc: Update test for year_month_day, that uses '%F'. * testsuite/std/time/format/pr120481.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-04Revert "libstdc++: sstream from string_view (P2495R3) [PR119741]"Nathan Myers3-200/+19
This reverts commit 8537e4851072ea1f1982c4c6ab0d24c9383e9edd.
2025-06-04libstdc++: Make system_clock::to_time_t always_inline [PR99832]Jonathan Wakely1-0/+2
For some 32-bit targets Glibc supports changing the size of time_t to be 64 bits by defining _TIME_BITS=64. That causes an ABI change which would affect std::chrono::system_clock::to_time_t. Because to_time_t is not a function template, its mangled name does not depend on the return type, so it has the same mangled name whether it returns a 32-bit time_t or a 64-bit time_t. On targets where the size of time_t can be selected at preprocessing time, that can cause ODR violations, e.g. the linker selects a definition of to_time_t that returns a 32-bit value but a caller expects 64-bit and so reads 32 bits of garbage from the stack. This commit adds always_inline to to_time_t so that all callers inline the conversion to time_t, and will do so using whatever type time_t happens to be in that translation unit. Existing objects compiled before this change will either have inlined the function anyway (which is likely if compiled with any optimization enabled) or will contain a COMDAT definition of the inline function and so still be able to find it at link-time. The attribute is also added to system_clock::from_time_t, because that's an equally simple function and it seems reasonable for them to both be always inlined. libstdc++-v3/ChangeLog: PR libstdc++/99832 * include/bits/chrono.h (system_clock::to_time_t): Add always_inline attribute to be agnostic to the underlying type of time_t. (system_clock::from_time_t): Add always_inline for consistency with to_time_t. * testsuite/20_util/system_clock/99832.cc: New test.
2025-06-04libstdc++: sstream from string_view (P2495R3) [PR119741]Nathan Myers3-19/+200
Add constructors to stringbuf, stringstream, istringstream, and ostringstream, and a matching overload of str(sv) in each, that take anything convertible to a string_view in places where the existing ctors and function take a string. Note this change omits the constraint applied to the istringstream constructor from string cited as a "drive-by" in P2495R3, as we have determined it is redundant. libstdc++-v3/ChangeLog: PR libstdc++/119741 * include/std/sstream: full implementation, really just decls, requires clause and plumbing. * include/bits/version.def, include/bits/version.h: new preprocessor symbol __cpp_lib_sstream_from_string_view. * testsuite/27_io/basic_stringbuf/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_istringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_ostringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_stringstream/cons/char/string_view.cc: New tests. * testsuite/27_io/basic_stringbuf/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_istringstream/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_ostringstream/cons/wchar_t/string_view.cc: New tests. * testsuite/27_io/basic_stringstream/cons/wchar_t/string_view.cc: New tests.
2025-06-04libstdc++: Implement P0849R8 auto(x) library changesPatrick Palka3-31/+28
This implements the library changes in P0849R8 "auto(x): decay-copy in the language" which consist of replacing most uses of the exposition-only function decay-copy with auto(x) throughout the library wording. We implement this as a DR against C++20 since there should be no behavior change in practice (especially in light of LWG 3724 which makes decay-copy SFINAE-friendly). The main difference between decay-copy and auto(x) is that decay-copy materializes its argument unlike auto(x), and so the latter is a no-op when its argument is a prvalue. Effectively the former could introduce an unnecessary move constructor call in some contexts. In C++20 and earlier we could emulate auto(x) with decay_t<decltype((x))>(x). After this paper the only remaining uses of decay-copy in the standard are in the specification of some range adaptors. In our implementation of those range adaptors I believe decay-copy is already implied which is why we don't use __decay_copy explicitly there. So since it's apparently no longer needed this patch goes ahead and removes __decay_copy. libstdc++-v3/ChangeLog: * include/bits/c++config (_GLIBCXX_AUTO_CAST): Define. * include/bits/iterator_concepts.h (_Decay_copy, __decay_copy): Remove. (__member_begin, __adl_begin): Use _GLIBCXX_AUTO_CAST instead of __decay_copy as per P0849R8. * include/bits/ranges_base.h (_Begin): Likewise. (__member_end, __adl_end, _End): Likewise. (__member_rbegin, __adl_rbegin, _RBegin): Likewise. (__member_rend, __adl_rend, _Rend): Likewise. (__member_size, __adl_size, _Size): Likewise. (_Data): Likewise. Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-04libstdc++: Implement C++23 P1659R3 starts_with and ends_withPatrick Palka4-0/+267
This implements ranges::starts_with and ranges::ends_with from the C++23 paper P1659R3. The corresponding_S_impl member functions take optional optional size parameters __n1 and __n2 of the two ranges, where -1 means the corresponding size is not known. libstdc++-v3/ChangeLog: * include/bits/ranges_algo.h (__starts_with_fn, starts_with): Define. (__ends_with_fn, ends_with): Define. * include/bits/version.def (ranges_starts_ends_with): Define. * include/bits/version.h: Regenerate. * include/std/algorithm: Provide __cpp_lib_ranges_starts_ends_with. * src/c++23/std.cc.in (ranges::starts_with): Export. (ranges::ends_with): Export. * testsuite/25_algorithms/ends_with/1.cc: New test. * testsuite/25_algorithms/starts_with/1.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-04libstdc++: Refactor __semaphore_base member functionsJonathan Wakely1-19/+22
Replace the _S_get_current and _S_do_try_acquire static member functions with non-static member functions _M_get_current and _M_do_try_acquire. This means they don't need the address of _M_counter passed in. libstdc++-v3/ChangeLog: * include/bits/semaphore_base.h (_S_get_current): Replace with non-static _M_get_current. (_S_do_try_acquire): Replace with non-static _M_do_try_acquire. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-04libstdc++: Fix std::counting_semaphore::acquire deadlock [PR104928]Jonathan Wakely1-23/+51
There's a deadlock in std::counting_semaphore that occurs when the semaphore is under contention. The bug happens when one thread tries to acquire the mutex, calling __semaphore_base::_S_do_try_acquire to atomically decrement the counter using compare_exchange_strong. If the counter is non-zero (and so should be possible to decrement) but another thread changes it (either incrementing or decrementing it) then the compare_exchange fails and _S_do_try_acquire returns false. Because that function is used by the predicate passed to __atomic_wait_address, when it returns false the thread does a futex wait until the value changes. However, when the predicate is false because the compare_exchange failed due to not matching the expected value, waiting for the value to change is incorrect. The correct behaviour would be to retry the compare_exchange using the new value (as long as it's still non-zero). Waiting for the value to change again means we can block forever, because it might never change again. The predicate should only test the value, not also attempt to alter it, and its return value should mean only one thing, not conflate a busy semaphore that cannot be acquired with a contended one that can be acquired by retrying. The correct behaviour of __semaphore_base::_M_acquire would be to attempt the decrement, and to retry immediately if it failed due to contention on the variable (i.e. due to the variable not having the expected value). It should only wait for the value to change when the value is zero, because that's the only time we can't decrement it. This commit moves the _S_do_try_acquire call out of the predicate and loops while it is false, only doing an atomic wait when the counter's value is zero. The predicate used for the atomic wait now only checks whether the value is decrementable (non-zero), without also trying to perform that decrement. In order for the caller to tell whether it should retry a failed _S_do_try_acquire or should wait for the value to be non-zero, the value obtained by a failed compare_exchange needs to be passed back to the caller. _S_do_try_acquire is changed to take its parameter by reference, so that the caller gets the new value and can check whether it's zero. In order to avoid doing another atomic load after returning from an atomic wait, the predicate is also changed to capture the local __val by reference, and then assign to __val when it sees a non-zero value. That makes the new value available to _M_acquire, so it can be passed to _S_do_try_acquire as the expected value of the compare_exchange. Although this means that the predicate is modifying data again, not just checking a value, this modification is safe. It's not changing the semaphore's counter, only changing a local variable in the caller to avoid a redundant atomic load. Equivalent changes are made to _M_try_acquire_until and _M_try_acquire_for. They have the same bug, although they can escape the deadlock if the wait is interrupted by timing out. For _M_acquire there's no time out so it potentially waits forever. _M_try_acquire also has the same bug, but can be simplified to just calling _M_try_acquire_for(0ns). A timeout of zero results in calling __wait_impl with the __spin_only flag set, so that the value is loaded and checked in a spin loop but there is no futex wait. This means that _M_try_acquire can still succeed under light contention if the counter is being changed concurrently, at the cost of a little extra overhead. It would be possible to implement _M_try_acquire as nothing more than an atomic load and a compare_exchange, but it would fail when there is any contention. libstdc++-v3/ChangeLog: PR libstdc++/104928 * include/bits/semaphore_base.h (_S_do_try_acquire): Take old value by reference. (_M_acquire): Move _S_do_try_acquire call out of the predicate and loop on its result. Make the predicate capture and update the local copy of the value. (_M_try_acquire_until, _M_try_acquire_for): Likewise. (_M_try_acquire): Just call _M_try_acquire_for. * testsuite/30_threads/semaphore/104928-2.cc: New test. * testsuite/30_threads/semaphore/104928.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-04libstdc++: Fix format call and test formatting with empty specs for durations.Tomasz Kamiński1-1/+2
This patches fixes an obvious error, where the output iterator argument was missing for call to format_to, when duration with custom representation types are used. It's also adding the test for behavior of ostream operator and the formatting with empty chron-spec for the chrono types. Current coverage is: * duration and hh_mm_ss in this commit, * calendar types in r16-1016-g28a17985dd34b7. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono:_M_s): Add missing __out argument to format_to call. * testsuite/std/time/format/empty_spec.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-03libstdc++: Check feature test macro for jthread in <stop_token>Jonathan Wakely1-4/+3
Check the appropriate feature test macro instead of checking that __cplusplus >= 201703L. libstdc++-v3/ChangeLog: * include/std/stop_token: Check __glibcxx_jthread instead of __cplusplus.
2025-06-03libstdc++: Use new __is_destructible built-in in <type_traits>Jonathan Wakely1-2/+40
libstdc++-v3/ChangeLog: * include/std/type_traits (is_destructible, is_destructible_v): Define using new built-in. (is_nothrow_destructible, is_nothrow_destructible_v): Likewise. (is_trivially_destructible, is_trivially_destructible_v): Likewise. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-03libstdc++: Fix errors and incorrect returns in atomic timed waitsJonathan Wakely1-4/+3
The __detail::__wait_until function has a comment that should have been removed when r16-1000-g225622398a9631 changed the return type from a std::pair to a struct with three members. The __atomic_wait_address_until_v and __atomic_wait_address_for_v function templates are apparently never used or instantiated, because they don't compile. This fixes them, but they're still unused. I plan to make use of them in a later commit. In __atomic_wait_address_until_v, __res.first in the return statement should have also been changed when r16-1000-g225622398a9631 changed __wait_result_type, and &__args should have been changed to just __args by r16-988-g219bb905a60d95. In __atomic_wait_address_for_v, the parameter is a copy & paste error and should use chrono::duration not chrono::time_point Fix _M_spin_until_impl so that the _M_has_val member of the result is accurate. If the deadline has passed then it never enters the loop and so never loads a fresh value, so _M_has_val should be false. There's also a redundant clock::now() call in __spin_until_impl which can be removed, we can reuse the call immediately before it. libstdc++-v3/ChangeLog: * include/bits/atomic_timed_wait.h (__detail::__wait_until): Remove incorrect comment. (__atomic_wait_address_until_v): Do not take address of __args in call to __detail::__wait_until. Fix return statement to refer to member of __wait_result_type. (__atomic_wait_address_for_v): Change parameter type from time_point to duration. * src/c++20/atomic.cc (__spin_until_impl): Fix incorrect return value. Reuse result of first call to clock.
2025-06-03libstdc++: Replace some implicit conversions in std::vectorJonathan Wakely2-3/+5
This replaces two implicit conversions from ptrdiff_t to size_t with explicit conversions that include unreachable hints for the ptrdiff_t value not being negative. libstdc++-v3/ChangeLog: * include/bits/stl_vector.h (~_Vector_base): Add unreachable hint for negative capacity and cast to size_t explicitly. * include/bits/vector.tcc (vector::_M_realloc_append): Use size() instead of end() - begin(). Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-03libstdc++: Use explicit cast to unsigned in std::rotr and std::rotlJonathan Wakely1-2/+2
This suppresses some -Wsign-conversion warnings from Clang when compiling with -Wsystem-headers. libstdc++-v3/ChangeLog: * include/std/bit (__rotl, __rotr): Use static_cast for conversion from int to unsigned. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-02libstdc++: Remove spaces before function parameter listJonathan Wakely4-7/+7
We don't use this GCC coding convention in libstdc++. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string::size): Remove space before parameter list. (basic_string::capacity): Likewise. * include/bits/stl_deque.h (deque::size): Likewise. * include/bits/stl_vector.h (vector::size, vector::capacity): Likewise. * include/bits/vector.tcc (vector::_M_realloc_insert): Likewise. (vector::_M_realloc_append): Likewise.
2025-06-02libstdc++: Implement LWG 2439 for std::unique_copy [PR120386]Jonathan Wakely2-41/+55
The current overload set for __unique_copy handles three cases: - The input range uses forward iterators, the output range does not. This is the simplest case, and can just compare adjacent elements of the input range. - Neither the input range nor output range use forward iterators. This requires a local variable copied from the input range and updated by assigning each element to the local variable. - The output range uses forward iterators. For this case we compare the current element from the input range with the element just written to the output range. There are two problems with this implementation. Firstly, the third case assumes that the value type of the output range can be compared to the value type of the input range, which might not be possible at all, or might be possible but give different results to comparing elements of the input range. This is the problem identified in LWG 2439. Secondly, the third case is used when both ranges use forward iterators, even though the first case could (and should) be used. This means that we compare elements from the output range instead of the input range, with the problems described above (either not well-formed, or might give the wrong results). The cause of the second problem is that the overload for the first case looks like: OutputIterator __unique_copy(ForwardIter, ForwardIter, OutputIterator, BinaryPred, forward_iterator_tag, output_iterator_tag); When the output range uses forward iterators this overload cannot be used, because forward_iterator_tag does not inherit from output_iterator_tag, so is not convertible to it. To fix these problems we need to implement the resolution of LWG 2439 so that the third case is only used when the value types of the two ranges are the same. This ensures that the comparisons are well behaved. We also need to ensure that the first case is used when both ranges use forward iterators. This change replaces a single step of tag dispatching to choose between three overloads with two step of tag dispatching, choosing between two overloads at each step. The first step dispatches based on the iterator category of the input range, ignoring the category of the output range. The second step only happens when the input range uses non-forward iterators, and dispatches based on the category of the output range and whether the value type of the two ranges is the same. So now the cases that are handled are: - The input range uses forward iterators. - The output range uses non-forward iterators or a different value type. - The output range uses forward iterators and has the same value type. For the second case, the old code used __gnu_cxx::__ops::__iter_comp_val to wrap the predicate in another level of indirection. That seems unnecessary, as we can just use a pointer to the local variable instead of an iterator referring to it. During review of this patch, it was discovered that all known implementations of std::unique_copy and ranges::unique_copy (except cmcstl2) disagree with the specification. The standard (and the SGI STL documentation) say that it uses pred(*i, *(i-1)) but everybody uses pred(*(i-1), *i) instead, and apparently always has done. This patch adjusts ranges::unique_copy to be consistent. In the first __unique_copy overload, the local copy of the iterator is changed to be the previous position not the next one, so that we use ++first as the "next" iterator, consistent with the logic used in the other overloads. This makes it easier to compare them, because we aren't using pred(*first, *next) in one and pred(something, *first) in the others. Instead it's always pred(something, *first). libstdc++-v3/ChangeLog: PR libstdc++/120386 * include/bits/ranges_algo.h (__unique_copy_fn): Reorder arguments for third case to match the first two cases. * include/bits/stl_algo.h (__unique_copy): Replace three overloads with two, depending only on the iterator category of the input range. Dispatch to __unique_copy_1 for the non-forward case. (__unique_copy_1): New overloads for the case where the input range uses non-forward iterators. (unique_copy): Only pass the input range category to __unique_copy. * testsuite/25_algorithms/unique_copy/lwg2439.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-02libstdc++: Pass small trivial types by value in polymorphic wrappersTomasz Kamiński4-1/+31
This patch adjust the passing of parameters for the move_only_function, copyable_function and function_ref. For types that are declared as being passed by value in signature template argument, they are passed by value to the invoker, when they are small (at most two pointers), trivially move constructible and trivially destructible. The latter guarantees that passing them by value has not user visible side effects. In particular, this extends the set of types forwarded by value, that was previously limited to scalars, to also include specializations of std::span and std::string_view, and similar standard and program defined-types. Checking the suitability of the parameter types requires the types to be complete. As a consequence, the implementation imposes requirements on instantiation of move_only_function and copyable_function. To avoid producing the errors from the implementation details, a static assertion was added to partial specializations of copyable_function, move_only_function and function_ref. The static assertion uses existing __is_complete_or_unbounded, as arrays type parameters are automatically decayed in function type. Standard already specifies in [res.on.functions] p2.5 that instantiating these partial specialization with incomplete types leads to undefined behavior. libstdc++-v3/ChangeLog: * include/bits/funcwrap.h (__polyfunc::__pass_by_rref): Define. (__polyfunc::__param_t): Update to use __pass_by_rref. * include/bits/cpyfunc_impl.h:: Assert that are parameters type are complete. * include/bits/funcref_impl.h: Likewise. * include/bits/mofunc_impl.h: Likewise. * testsuite/20_util/copyable_function/call.cc: New test. * testsuite/20_util/function_ref/call.cc: New test. * testsuite/20_util/move_only_function/call.cc: New test. * testsuite/20_util/copyable_function/conv.cc: New test. * testsuite/20_util/function_ref/conv.cc: New test. * testsuite/20_util/move_only_function/conv.cc: New test. * testsuite/20_util/copyable_function/incomplete_neg.cc: New test. * testsuite/20_util/function_ref/incomplete_neg.cc: New test. * testsuite/20_util/move_only_function/incomplete_neg.cc: New test. Reviewed-by: Patrick Palka <ppalka@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-02libstdc++: Implement C++26 std::polymorphic [PR119152]Jonathan Wakely4-1/+395
This patch implements C++26 std::polymorphic as specified in P3019 with amendment to move assignment from LWG 4251. The implementation always allocate stored object on the heap. The manager function (_M_manager) is similary keep with the object (polymorphic::_Obj), which reduces the size of the polymorphic to size of the single pointer plus allocator (that is declared with [[no_unique_address]]). The implementation does not not use small-object optimization (SSO). We may consider adding this in the future, as SSO is allowed by the standard. However, storing any polimorphic object will require providing space for two pointers (manager function and vtable pointer) and user-declared data members. PR libstdc++/119152 libstdc++-v3/ChangeLog: * include/bits/indirect.h (std::polymorphic, pmr::polymorphic) [__glibcxx_polymorphic]: Define. * include/bits/version.def (polymorphic): Define. * include/bits/version.h: Regenerate. * include/std/memory: Define __cpp_lib_polymorphic. * testsuite/std/memory/polymorphic/copy.cc: New test. * testsuite/std/memory/polymorphic/copy_alloc.cc: New test. * testsuite/std/memory/polymorphic/ctor.cc: New test. * testsuite/std/memory/polymorphic/ctor_poly.cc: New test. * testsuite/std/memory/polymorphic/incomplete.cc: New test. * testsuite/std/memory/polymorphic/invalid_neg.cc: New test. * testsuite/std/memory/polymorphic/move.cc: New test. * testsuite/std/memory/polymorphic/move_alloc.cc: New test. Co-authored-by: Tomasz Kamiński <tkaminsk@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>