Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      deduction guides forstd::basic_string

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      Defined in header<string>
      template<class InputIt,

               class Alloc=std::allocator<typenamestd::iterator_traits
                                                         <InputIt>::value_type>>
      basic_string( InputIt, InputIt, Alloc= Alloc())
         -> basic_string<typenamestd::iterator_traits<InputIt>::value_type,
                         std::char_traits
                             <typenamestd::iterator_traits<InputIt>::value_type>,

                               Alloc>;
      (1)(since C++17)
      template<class CharT,

               class Traits,
               class Alloc=std::allocator<CharT>>
      explicit basic_string(std::basic_string_view<CharT, Traits>,
                             const Alloc&= Alloc())

         -> basic_string<CharT, Traits, Alloc>;
      (2)(since C++17)
      template<class CharT,

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

         -> basic_string<CharT, Traits, Alloc>;
      (3)(since C++17)
      template<ranges::input_range R,

               class Alloc=std::allocator<ranges::range_value_t<R>>>
      basic_string(std::from_range_t, R&&, Alloc= Alloc())
         -> basic_string<ranges::range_value_t<R>,

                         std::char_traits<ranges::range_value_t<R>>, Alloc>;
      (4)(since C++23)
      1) Thisdeduction guide is provided forstd::basic_string to allow deduction from an iterator range. This overload participates in overload resolution only ifInputIt satisfiesLegacyInputIterator andAlloc satisfiesAllocator.
      2,3) These deduction guides are provided forstd::basic_string to allow deduction from astd::basic_string_view. These overloads participate in overload resolution only ifAlloc satisfiesAllocator.
      3) Thesize_type parameter type refers to the nested typesize_type of the type deduced by the deduction guide.
      4) This deduction guide is provided forstd::basic_string to allow deduction from astd::from_range_t tag and aninput_range.

      Note: the extent to which the library determines that a type does not satisfyLegacyInputIterator 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 satisfyAllocator is unspecified, except that as a minimum the member typeAlloc::value_type must exist and the expressionstd::declval<Alloc&>().allocate(std::size_t{}) must be well-formed when treated as an unevaluated operand.

      Contents

      [edit]Notes

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

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_containers_ranges202202L(C++23)Ranges-aware construction and insertion; overload(4)

      [edit]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 >= 202202Lstd::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}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3075C++17deduction frombasic_string_view was unsupported
      (exacerbated byLWG issue 2946)
      deduction guides added
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/deduction_guides&oldid=182821"

      [8]ページ先頭

      ©2009-2025 Movatter.jp