deduction guides for std::basic_string
Defined in header <string>
|
||
template<class InputIt, class Alloc = std::allocator< typename std::iterator_traits<InputIt>::value_type>> |
(1) | (since C++17) |
template<class CharT, class Traits, |
(2) | (since C++17) |
template<class CharT, class Traits, |
(3) | (since C++17) |
InputIt
satisfies InputIterator and Alloc
satisfies Allocator.Alloc
satisfies Allocator.Note: the extent to which the library determines that a type does not satisfy InputIterator 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.
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 |
Example
#include <string> #include <vector> int main() { std::vector<char> v = {'a', 'b', 'c'}; std::basic_string s(v.begin(), v.end()); // uses explicit deduction guide }