Specializing template class for graphs

Question

Since there are a lot of graph algorithms that use residual graphs I thought it would make sense to implement a template class resGraph parametrized over the Objects Node and Edge which implements some basic functionality like reading a graph from a file, printing a flow and storing all the relevant information about the graph.

Now I want to write the class PushRelabel which I want to be a specialization of the resGraph class template. But I don't just want to plug in some Node and Edge type, I also want to extend the functionality of the class, i.e. I want to add methods to the class. How can this be done?


Show source
| class   | c++   | templates   | graph   2016-12-25 13:12 1 Answers

Answers to Specializing template class for graphs ( 1 )

  1. 2016-12-25 14:12

    There are a lot of graph libraries, for example BGL from Boost. Maybe you should look at them.

    Adding methods to template specialization is not an issue. Note that specialization may have nothing to do with the original template. For example:

    template<typename T>
    struct A
    {
        void B(int);
    };
    
    template<>
    struct A<int>
    {
        float C(char*);
    };
    
    template<>
    struct A<double>
    {
        void D(int, int, int);
    };
    

    This is perfectly fine. For different types you will have instantiations that do not have anything in common to each other. You can also write:

    template<typename T>
    struct B : public A<T>           // Class template can have base class.
    {                                // It can be either a class or instantiation
        void B(int);                 // of some class template.
        void B2(int, int, int);
    };
    
    template<>
    struct B<int> : public A<int>    // Specialization for int.
    {
        void B(int);
        void B2(int, int, int);
    };
    

Leave a reply to - Specializing template class for graphs

◀ Go back