deductiongu...basic_string(3)
π 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_stringcan deduce from an iterator range. It only participates in overload resolution ifInputItsatisfiesLegacyInputIteratorandAllocsatisfiesAllocator. - (2,3) β these guides exist so
std::basic_stringcan deduce from astd::basic_string_view. Thesize_typeparameter type in(3)refers to thesize_typemember type of the type deduced by the deduction guide. These overloads participate in overload resolution only ifAllocsatisfiesAllocator. - (4) β this guide exists so
std::basic_stringcan deduce from astd::from_range_ttag and aninput_range.
Note: the extent to which the library determines that a type does not satisfyLegacyInputIteratoris 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 satisfyAllocatoris unspecified, except that as a minimum the member typeAlloc::value_typemust exist and the expressionstd::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β value202202L, 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 frombasic_string_viewwas unsupported (exacerbated by LWG issue2946) β 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms