Namespaces
Variants
Views
Actions

deduction guides for std::priority_queue

From cppreference.com
< cpp‎ | container‎ | priority queue
Revision as of 01:29, 30 August 2021 by Fruderica (Talk | contribs)

 
 
 
 
Defined in header <queue>
template <class Comp, class Container>

priority_queue(Comp, Container)

 -> priority_queue<typename Container::value_type, Container, Comp>;
(1) (since C++17)
template<class InputIt,

         class Comp = std::less</*iter-value-type*/<InputIt>>,
         class Container = std::vector</*iter-value-type*/<InputIt>>
priority_queue(InputIt, InputIt, Comp = Comp(), Container = Container())

  -> priority_queue</*iter-value-type*/<InputIt>, Container, Comp>;
(2) (since C++17)
template<class Comp, class Container, class Alloc>

priority_queue(Comp, Container, Alloc)

  -> priority_queue<typename Container::value_type, Container, Comp>;
(3) (since C++17)
template<class InputIt, class Alloc>

priority_queue(InputIt, InputIt, Alloc)
  -> priority_queue</*iter-value-type*/<InputIt>,
                    std::vector</*iter-value-type*/<InputIt>, Alloc>,

                    std::less</*iter-value-type*/<InputIt>>>;
(4) (since C++17)
template<class InputIt, class Comp, class Alloc>

priority_queue(InputIt, InputIt, Comp, Alloc)
  -> priority_queue</*iter-value-type*/<InputIt>,

                    std::vector</*iter-value-type*/<InputIt>, Alloc>, Comp>;
(5) (since C++17)
template<class InputIt, class Comp, class Container, class Alloc>

priority_queue(InputIt, InputIt, Comp, Container, Alloc)

  -> priority_queue<typename Container::value_type, Container, Comp>;
(6) (since C++17)

These deduction guides are provided for std::priority_queue to allow deduction from underlying container type and from an iterator range. /*iter-value-type*/<It> denotes typename std::iterator_traits<It>::value_type for any type It.

These overloads participate in overload resolution only if InputIt satisfies LegacyInputIterator, Comp does not satisfy Allocator, Container does not satisfy Allocator, for overloads (4) and (5),(since C++23) Alloc satisfies Allocator, and for overloads (3) and (6), std::uses_allocator_v<Container, Alloc> is true.

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.

Example

#include <functional>
#include <vector>
#include <queue>
int main() {
   std::vector<int> v = {1,2,3,4};
   std::priority_queue pq1{std::greater<int>{}, v}; // deduces std::priority_queue<
                                                    //   int, std::vector<int>,
                                                    //   std::greater<int>>
   std::priority_queue pq2{v.begin(), v.end()}; // deduces std::priority_queue<int>
}

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 3506 C++17 deduction guides from iterator and allocator were missing added