deductionguidesforstd::set(3)
π NAME π
Bestie, this is deductionguidesforstd::set from the C++ Standard Libary manual, page 3 β and the name is literally deductionguidesforstd::set again. We love a self-referential moment. β¨
πΈ Synopsis πΈ
Defined in header <set>. Okay so here are the deduction guides, and yes, every template parameter and arrow stays exactly as written. No cap. π
Guide (1) β (since C++17)
template<
class InputIt,
class Comp = std::less<typename std::iterator_traits<InputIt>::value_type>,
class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
set( InputIt, InputIt, Comp = Comp(), Alloc = Alloc() )
-> set<typename std::iterator_traits<InputIt>::value_type, Comp, Alloc>;
Guide (2) β (since C++17)
template<
class Key, class Comp = std::less<Key>,
class Alloc = std::allocator<Key> >
set( std::initializer_list<Key>, Comp = Comp(), Alloc = Alloc() )
-> set<Key, Comp, Alloc>;
Guide (3) β (since C++17)
template< class InputIt, class Alloc >
set( InputIt, InputIt, Alloc )
-> set<typename std::iterator_traits<InputIt>::value_type,
std::less<typename std::iterator_traits<InputIt>::value_type>, Alloc>;
Guide (4) β (since C++17)
template< class Key, class Alloc >
set( std::initializer_list<Key>, Alloc )
-> set<Key, std::less<Key>, Alloc>;
Guide (5) β (since C++23)
template< ranges::input_range R, class Compare = less<ranges::range_value_t<R>>,
class Alloc = std::allocator<ranges::range_value_t<R>> >
set( std::from_range_t, R&&, Compare = Compare(), Alloc = Alloc() )
-> set<ranges::range_value_t<R>, Compare, Alloc>;
Guide (6) β (since C++23)
template< ranges::input_range R, class Alloc >
set( std::from_range_t, R&&, Alloc )
-> set<ranges::range_value_t<R>,
std::less<ranges::range_value_t<R>>, Alloc>;
So, tea time: 1-4) are the deduction guides provided for set so you can deduce from an iterator range β those are overloads (1,3) β and from std::initializer_list β those are overloads (2,4). π«Ά
And 5,6) are the deduction guides provided for set so you can deduce from a std::from_range_t tag and an input_range. Ranges girlies rise. π
These overloads participate in overload resolution only if InputIt satisfies LegacyInputIterator, Alloc satisfies Allocator, and Comp does not satisfy Allocator. Yes, that last one is a vibe check: Comp must not be an allocator. π
Note, and this is important: 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 typeAlloc::value_typemust exist and the expressionstd::declval<Alloc&>().allocate(std::size_t{})must be well-formed when treated as an unevaluated operand. π
π¦ Notes π¦
Feature-test macro moment, bestie β hereβs the whole row:
__cpp_lib_containers_rangesβ value202202L, standard(C++23), feature: Ranges-aware construction and insertion; overload(5,6).
π Example π
// Run this code
#include <set>
int main()
{
// guide #2 deduces std::set<int>
std::set s = {1, 2, 3, 4};
// guide #1 deduces std::set<int>
std::set s2(s.begin(), s.end());
}
Literally such a clean little example. Guide #2 does its thing, guide #1 does its thing, and both end up at std::set<int>. Slay. β
Source/version/date check, folded into the cutest sign-off ever: this page came from http://cppreference.com, dated 2024.06.10, and it is <rainbow>deductionguidesforstd::set(3)</rainbow> π
π¦ 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