Function with template and Priority Queue - c++

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?

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>;

Related

what is this template doing?

from https://github.com/wjakob/tbb/blob/master/include/tbb/tbb_allocator.h#L150
template <typename T, template<typename X> class Allocator = tbb_allocator>
class zero_allocator : public Allocator<T>
{...}
what I understand is that this is a definition for a new class, that inherits from the Allocator type visible in that translation unit .
the part that I don't get is template<typename X> class Allocator = tbb_allocator .
according to the tbb docs the zero_allocator takes 2 inputs, the type T and how many objects of type T you need to allocate . the zero_allocator also inheriths from the tbb_allocator which in turns defaults to a "standard" malloc/free behaviour if TBB is not present when linking .
I still don't think I get that syntax, especially the template<typename X> class Allocator part .
Can you explain this syntax and what is achieving ?
template <typename T, template<typename X> class Allocator = tbb_allocator>
class zero_allocator : public Allocator<T>
{...}
What we have:
template starts declaration of a template
it is followed by the template parameter list:
<typename T, template<typename X> class Allocator = tbb_allocator>
The first template parameter is "some type" T
the next one is not a type, it is a template itself.
template<typename X> class Allocator
So the template class zero_allocator needs to get instantiated with first
parameter is any type T and with second parameter a template which itself takes on template parameter X must be given.
In addition, the second template parameter for zero_allocator can be left, in this case for Allocator parameter the template tbb_allocator is used.
Here a full compileable example:
template <typename Y>
class ExampleTemplate {};
// and the one which is used as the default
template <typename Y>
class tbb_allocator {};
template <typename T, template<typename X> class Allocator = tbb_allocator>
class zero_allocator : public Allocator<T>
{
// Lets use the type T:
T some_var; // in our example, this will be "int some_var"
// and here we use the Template Template thing...
Allocator<float> some_allocator;
};
int main()
{
zero_allocator< int, ExampleTemplate > za;
}

Template dependent typedefs

I am modifying a templated A* search and now have the following class (part of):
template <typename TNode, typename THeuristic>
class AStar
{
public:
// Typedefs.
typedef d_ary_heap<TNode*, boost::heap::compare<NodeCompare<TNode>>, boost::heap::arity<4>, boost::heap::mutable_<true>> PriorityQueueType;
//...
}
Until now, I hadn't thought of templatizing the heuristic parameter, so the Node class was defined as follows:
template <typename T = float>
class Node
{
public:
// Typedefs:
typedef typename AStar<Node>::PriorityQueueType::handle_type HeapHandle;
//...
}
But now since AStar takes a second template paremeter for the heuristic the typedef gives a compile error here: typedef typename AStar<Node ??>.... Is it possible to make this work somehow while maintaining freedom to specify the heuristic in the AStar class?
You could factor your code differently and keep the heuristic-independent part separate:
namespace detail
{
template <typename T>
struct AStarHeap
{
using QueueType = /* ... */;
using HandleType = QueueType::handle_type;
// ...
};
}
template <typename Node, typename Heur>
struct AStar : detail::AStarHeap<Node>
{
// ...
};
template <typename T>
struct Node
{
using HeapHandle = typename detail::AStarHeap<T>::HandleType;
// ...
};
Reworking a bit from my comment on the question, here's how I would probably do it:
template <typename TNode, typename THeuristic = void>
class AStar;
template <typename TNode>
class AStar<TNode>
{
// put everything that does not depend on THeuristic here
};
template <typename TNode, typename THeuristic>
class AStar : public AStar<TNode>
{
// put everything that does depend on THeuristic here
};
Looking at it now, this takes much the same approach as Kerrek SB's answer, but has the advantage that your existing code, which uses AStar<TNode>, continues to compile so long as it does not attempt to do anything that requires THeuristic.

How to specialize a class template for a tuple when variadic template arguments are not supported?

I have a class template
template<class T>
class A
{...};
and I want to specialize it when T is a tuple. I think I can do this
template<class Args...>
class A<std::tuple<Args...>>
{...};
However, my compiler doesn't support variadic template arguments, how to do it?
You can specialize it for tuples of every different arity:
// explicit specialization for 0-element tuples
template<>
class A<std::tuple<>>
{...};
// partial specialization for 1-element tuples
template<class Arg>
class A<std::tuple<Arg>>
{...};
// partial specialization for 2-element tuples
template<class Arg0, class Arg1>
class A<std::tuple<Arg0, Arg1>>
{...};
... and so on, up to whatever maximum number of tuple elements you need to support.

Template class with template container

How can I declare template class (adaptor) with different containers as template arguments?
For example, I need to declare class:
template<typename T, typename Container>
class MyMultibyteString
{
Container buffer;
...
};
And I want it to my based on vector. How to make it hard-defined? (to prevent someone from writing such declaration MyMultibyteString<int, vector<char>>).
Moreover, how to implement such construction:
MyMultibyteString<int, std::vector> mbs;
without passing template argument to container.
You should use template template parameters:
template<typename T, template <typename, typename> class Container>
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
class MyMultibyteString
{
Container<T, std::allocator<T>> buffer;
// ...
};
This would allow you to write:
MyMultibyteString<int, std::vector> mbs;
Here is a compiling live example. An alternative way of writing the above could be:
template<typename T,
template <typename, typename = std::allocator<T>> class Container>
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
class MyMultibyteString
{
Container<T> buffer; // <== No more need to specify the second argument here
// ...
};
And here is the corresponding live example.
The only thing you have to pay attention to is that the number and type of arguments in the template template parameter declaration must match exactly the number and type of arguments in the definition of the corresponding class template you want to pass as a template argument, regardless of the fact that some of those parameters may have default values.
For instance, the class template std::vector accepts two template parameters (the element type and the allocator type), although the second one has the default value std::allocator<T>. Because of this, you could not write:
template<typename T, template <typename> class Container>
// ^^^^^^^^
// Notice: just one template parameter declared!
class MyMultibyteString
{
Container<T> buffer;
// ...
};
// ...
MyMultibyteString<int, std::vector> mbs; // ERROR!
// ^^^^^^^^^^^
// The std::vector class template accepts *two*
// template parameters (even though the second
// one has a default argument)
This means that you won't be able to write one single class template that can accept both std::set and std::vector as a template template parameter, because unlike std::vector, the std::set class template accepts three template parameters.
Another approach to solve this is by using variadic templates and with that you can use any container as suggested in comments above and here is the implemenation :
template<template <typename... Args> class Container,typename... Types>
class Test
{
public:
Container<Types...> test;
};
int main()
{
Test<std::vector,int> t;
Test<std::set,std::string> p;
return 0;
}
If you look at the definitions of list and vector from cplusplus.com, for example they are:
template < class T, class Alloc = allocator<T> > class list;
and
template < class T, class Alloc = allocator<T> > class vector;
So this should go as the type of the container, the other template parameter is the type of the elements. As an example this program will output 3:
#include <iostream>
#include <list>
using namespace std;
template <template <typename...> typename C, typename T>
struct Cont {
C<T> info;
};
int main(void)
{
Cont<list, int> cont;
cont.info.push_back(3);
cout << cont.info.front() << endl;
return 0;
}

Partial Template Specialization of Template

I can't for the life of me get this to work.
I have an existing template:
template <class T>
class MyTemplate;
Now I want to specialize it, but for a class T that's a template--without further specializing the second template, for example:
template <>
template <class T>
class MyTemplate<vector> { /*...*/ };
But this, and various other syntaxes I've tried don't seem to compile. What's the syntax for this? Or is it even possible? If not, are there possible alternatives for MyTemplate so that I could handle, say, a generalized specialization for both vector and map?
I think you're looking for this:
template<typename T>
class MyTemplate {...}
template<typename T>
class MyTemplate<vector<T> > {...}
Above, the partial specialization is used when you make a MyTemplate<vector<int> > x; and T is int.
The proper syntax is :
template < typename T>
class MyTemplate<vector<T> > { /*...*/ };
more generally
template<typename A, typename B> class MyTemplate;
template<typename C, typename D> class SomeTemplate;
template<typename A, typename C>
class MyTemplate<A, SomeTemplate<C,A> > { /* ... */ };
A template can be defined either in terms of:
non-type parameters (constants)
type parameters
template parameters
once it has been so defined, any specialization must respect the kind of the parameter.
Therefore, if you want to specialize for a vector (which is a template) where a type parameter is expected, you need to spell out the parameters of the vector to create a (templated) type parameter:
template <typename T, typename Alloc>
class MyTemplate < std::vector<T, Alloc> > {
};
Similarly for map, though there are more parameters:
template <typename K, typename V, typename C, typename A>
class MyTemplate < std::map<K, V, C, A> > {
};
and here you go :)
I'm not sure about the syntax for defining a template for a class. You need to look that up. However defining the template is not the catch all. You need to write the class definition for each template. For example if you have an class define to do talking 1 argument of type x, and taking 2 arguments of type y, or etc... then you need a class to handle it. Same as function overloading . You have the same function name but each takes different arguments. You write a function for each. And that call picks the right function based on the argument list.
So a class that ... will sort different objects by defining for each type.