Function with template and Priority Queue

I'm trying to write Dijkstra's algorithm with Fibonacci Heap and Priority Queue. So I have a class (struct) for Fibonacci Heap

template<class T>
struct Fib {
    ...
};

and a function

template <template <class> class T>
void dijkstra(...) {
    T<std::pair<double, int> > heap;
    ...
}

The problem is:

dijkstra<Fib>(...);                 // OK
dijkstra<std::priority_queue>(...); // doesn't compile

Why doesn't this compile, and how can I write it properly?

Upvotes: 0

Views: 783

Answers (1)

Barry
Barry

Reputation: 302748

When you write a parameter like:

template<class> class T

that expects a class template with a single template argument. Fib is such a class template, it just takes T. However, std::priority_queue doesn't take a single template parameter. It takes three, even though two are defaulted:

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

If you're using C++11, you could simply enhance dijkstra to take an arbitrary class template:

template<template<class...> class T>
//                     ^^^
void dijkstra(...) {

Or write a single-template-parameter alias for std::priority_queue:

template <class T>
using vector_less_pq = std::priority_queue<T>;

Upvotes: 1

Related Questions