From cppreference.com
Defined in header
template<classInputIt,classAlloc=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<classCharT,classTraits,classAlloc=std::allocator<CharT>>explicitbasic_string(std::basic_string_view<CharT,Traits>,constAlloc&=Alloc())->basic_string<CharT,Traits,Alloc>; (2)(since C++17)template<classCharT,classTraits,classAlloc=std::allocator<CharT>>>basic_string(std::basic_string_view<CharT,Traits>,typename/* see below */::size_type,typename/* see below */::size_type,constAlloc&=Alloc())->basic_string<CharT,Traits,Alloc>; (3) (since C++17)template<ranges::input_rangeR,classAlloc=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
to allow deduction from a
. 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
.
Note: the extent to which the library determines that a type does not satisfy
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
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 (
) are needed because the
constructors for
s are made templates to avoid causing ambiguities in existing code, and those templates do not support class template argument deduction.
Notes
macro ValueStdFeature
(C++23)
construction and insertion; overload (
)Example
Run this code
#include<cassert>#include<string>#include<vector>intmain(){std::vector<char>v={'a','b','c'};std::basic_strings1(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_strings4(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 Applied to Behavior as published Correct behavior
C++17 deduction from basic_string_view was unsupported (exacerbated by
) deduction guides added