deductionguidesforstd::set(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 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 πŸ¦‹

Feature-test macro moment, bestie β€” here’s the whole row:

  • __cpp_lib_containers_ranges β€” value 202202L, 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