deductiongu...basic_string(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… deductiongu...basic_string(3) πŸ’…

okay so this page is from the C++ Standard Libary manual, section (3), and it's literally all about deductionguidesforstd::basic_string ✨ no cap, we're doing deduction guides today bestie.


πŸŽ€ NAME πŸŽ€

deductionguidesforstd::basic_string β€” deductionguidesforstd::basic_string

yes bestie, it's the same string twice, that's the whole name πŸ’€


✨ SYNOPSIS ✨

          Defined in header <string>
          template< class InputIt, class Alloc = std::allocator<

                                       typename
          std::iterator_traits<InputIt>::value_type> >
          basic_string( InputIt, InputIt, Alloc = Alloc() )
              -> basic_string<typename
       (1) (since C++17)
          std::iterator_traits<InputIt>::value_type,
                              std::char_traits<

                                  typename
          std::iterator_traits<InputIt>::value_type>, Alloc>;
          template< class CharT,

                    class Traits,
                    class Alloc = std::allocator<CharT> >
       (2) (since C++17)
          explicit basic_string( std::basic_string_view<CharT, Traits>,
          const Alloc& = Alloc() )

              -> basic_string<CharT, Traits, Alloc>;
          template< class CharT,

                    class Traits,
                    class Alloc = std::allocator<CharT>> >
          basic_string( std::basic_string_view<CharT, Traits>,
       (3) (since C++17)
                        typename /* see below */::size_type,
                        typename /* see below */::size_type,
                        const Alloc& = Alloc() )

              -> basic_string<CharT, Traits, Alloc>;
          template< ranges::input_range R,

                    class Alloc = std::allocator<ranges::range_value_t<R>> >
          basic_string( std::from_range_t, R&&, Alloc = Alloc() )
       (4) (since C++23)
              -> basic_string<ranges::range_value_t<R>,

                                 std::char_traits<ranges::range_value_t<R>>,
          Alloc>;
  • (1) β€” this guide exists so std::basic_string can deduce from an iterator range. It only participates in overload resolution if InputIt satisfies LegacyInputIterator and Alloc satisfies Allocator.
  • (2,3) β€” these guides exist so std::basic_string can deduce from a std::basic_string_view. The size_type parameter type in (3) refers to the size_type member type of the type deduced by the deduction guide. These overloads participate in overload resolution only if Alloc satisfies Allocator.
  • (4) β€” this guide exists so std::basic_string can deduce from a std::from_range_t tag and an input_range.
Note: the extent to which the library determines that a type does not satisfy LegacyInputIterator is unspecified, except that as a minimum integral types do not qualify as input iterators. Likewise, the extent to which it determines that a type does not satisfy Allocator is unspecified, except that as a minimum the member type Alloc::value_type must exist and the expression std::declval<Alloc&>().allocate(std::size_t{}) must be well-formed when treated as an unevaluated operand. 🫢

πŸ“ NOTES πŸ“

Guides (2,3) are needed because the std::basic_string constructors for std::basic_string_views are made templates to avoid causing ambiguities in existing code, and those templates do not support class template argument deduction.


πŸ“ NOTES πŸ“ (feature-test macro edition)

  • __cpp_lib_containers_ranges β€” value 202202L, Std (C++23), feature: Ranges-aware construction and insertion; overload (4).

πŸ“ EXAMPLE πŸ“

       // Run this code

        #include <cassert>
        #include <string>
        #include <vector>

        int main()
        {
            std::vector<char> v = {'a', 'b', 'c'};
            std::basic_string s1(v.begin(), v.end()); // uses deduction guide
       (1)
            assert(s1 == "abc");

        #if __cpp_lib_containers_ranges >= 202202L
            std::vector<wchar_t> v4{0x43, 43, 053, 0x32, 0x33};
            std::basic_string s4(std::from_range, v4); // uses deduction guide
       (4)
            assert(s4 == L"C++23");
        #endif
        }

πŸ•΅οΈ DEFECT REPORTS πŸ•΅οΈ

The following behavior-changing defect reports were applied retroactively to previously published C++ standards:

  • DR: LWG 3075 β€” Applied to: C++17 β€” Behavior as published: deduction from basic_string_view was unsupported (exacerbated by LWG issue 2946) β€” Correct behavior: deduction guides added.

okay that's the whole page bestie πŸ’– this came from http://cppreference.com, dated 2024.06.10, and the footer title is deductiongu...basic_string(3) β€” slay πŸ¦‹

πŸ“¦ where this page comes from