deduction guides for std::basic_string - cppreference.com (original) (raw)

Defined in header
template< class InputIt, class Alloc = std::allocator<typename std::iterator_traits <InputIt>::value_type> >basic_string( InputIt, InputIt, Alloc = Alloc() ) -> basic_string<typename std::iterator_traits<InputIt>::value_type, std::char_traits <typename std::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)

2,3) These deduction guides are provided for std::basic_string to allow deduction from a std::basic_string_view. These overloads participate in overload resolution only if Alloc satisfies Allocator.

  1. The size_type parameter type refers to the nested type size_type of the type deduced by the deduction guide.

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.

[edit] 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.

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware construction and insertion; overload (4)

[edit] Example

[edit] Defect reports

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

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