W3cubDocs

/C++

deduction guides for std::map

Defined in header <map>
template< class InputIt,
          class Comp = std::less<iter_key_t<InputIt>>,
          class Alloc = std::allocator<iter_to_alloc_t<InputIt>> >
map( InputIt, InputIt, Comp = Comp(), Alloc = Alloc() )
  -> map<iter_key_t<InputIt>, iter_val_t<InputIt>, Comp, Alloc>;
(1) (since C++17)
template< class Key,
          class T,
          class Comp = std::less<Key>,
          class Alloc = std::allocator<std::pair<const Key, T>> >
map( std::initializer_list<std::pair<Key, T>>, Comp = Comp(), Alloc = Alloc() )
  -> map<Key, T, Comp, Alloc>;
(2) (since C++17)
template< class InputIt, class Alloc >
map( InputIt, InputIt, Alloc )
  -> map<iter_key_t<InputIt>, iter_val_t<InputIt>,
         std::less<iter_key_t<InputIt>>, Alloc>;
(3) (since C++17)
template< class Key, class T, class Alloc >
map( std::initializer_list<std::pair<Key, T>>, Alloc )
  -> map<Key, T, std::less<Key>, Alloc>;
(4) (since C++17)
template< ranges::input_range R, class Compare = std::less<range_key_t<R>,
          class Alloc = std::allocator<range_to_alloc_t<R>> >
map( std::from_range_t, R&&, Compare = Compare(), Alloc = Alloc() )
  -> map<range_key_t<R>, range_mapped_t<R>, Compare, Alloc>;
(5) (since C++23)
template< ranges::input_range R, class Alloc >
map( std::from_range_t, R&&, Alloc )
  -> map<range_key_t<R>, range_mapped_t<R>, std::less<range_key_t<R>>, Alloc>;
(6) (since C++23)
Exposition-only helper type aliases
template< class InputIter >
using iter_val_t =
  typename std::iterator_traits<InputIter>::value_type;
(exposition only)
(since C++17)
template< class InputIter >
using iter_key_t =
  std::remove_const_t< std::tuple_element_t<0, iter_val_t<InputIter>>>;
(exposition only)
(since C++17)
template< class InputIter >
using iter_mapped_t =
  std::tuple_element_t<1, iter_val_t<InputIter>>;
(exposition only)
(since C++17)
template< class InputIter >
using iter_to_alloc_t =
  std::pair<std::add_const_t<tuple_element_t<0, iter_val_t<InputIter>>>,
            std::tuple_element_t<1, iter_val_t<InputIter>>>;
(exposition only)
(since C++17)
template< ranges::input_range Range >
using range_key_t =
  std::remove_const_t<typename ranges::range_value_t<Range>::first_type>;
(exposition only)
(since C++23)
template< ranges::input_range Range >
using range_mapped_t =
  typename ranges::range_value_t<Range>::second_type;
(exposition only)
(since C++23)
template< ranges::input_range Range >
using range_to_alloc_t =
  std::pair<std::add_const_t<typename ranges::range_value_t<Range>::first_type>,
            typename ranges::range_value_t<Range>::second_type>;
(exposition only)
(since C++23)
1-4) These deduction guides are provided for map to allow deduction from an iterator range (overloads (1,3)) and std::initializer_list (overloads (2,4)).
5,6) These deduction guides are provided for map to allow deduction from a std::from_range_t tag and an input_range.

These overloads participate in overload resolution only if InputIt satisfies LegacyInputIterator, Alloc satisfies Allocator, and Comp does not satisfy Allocator.

Note: 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 Value Std Comment
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware construction and insertion; overload (5,6)

Example

#include <map>
 
int main()
{
    // std::map m1 = {{"foo", 1}, {"bar", 2}};
        // Error: braced-init-list has no type; cannot deduce
        // pair<Key, T> from {"foo", 1} or {"bar", 2}
 
    std::map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2
    std::map m2(m1.begin(), m1.end()); // guide #1
}

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 3025 C++17 initializer-list guides take std::pair<const Key, T> use std::pair<Key, T>

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/map/deduction_guides