Search code examples
c++templatesheader-filestemplate-templates

Create a template template function in header and implementation file


So I have the class PolyLine which I am trying to genericize to allow stl containers, like list or vector, to act as the class container. I am trying to use a template template function to do this:

template<typename T, template<typename, typename> class Container, typename alloc = std::allocator<T>>
class PolyLine : public CAD::Shape {
private:
  size_t _n_points; //Number of points
  Container<T, alloc> _pline;
public:

  //Constructors
  PolyLine(size_t, double);
  PolyLine(const PolyLine&); //Copy constructor

  //Print
  void print();

  //Operator overload functions
  PolyLine& operator = (const PolyLine&);


};

And I am trying to implement the functions in the implementation file like this:

#include "PolyLine.hpp"

template<typename T, template<typename,typename> class Container, typename alloc = std::allocator<T>>
PolyLine<Container<T, alloc>>::PolyLine(size_t size, double distance) :  Shape(), _n_points(size) {
};

This does not work and apparently I need to fix something in the declaration of PolyLine<Container<T,alloc>>, but I am not sure what. *Edit: the error is get is "PolyLine: too few template arguments".


Solution

  • You haven't specialised PolyLine for Container<T, alloc> so the only constructor definitions we can provide are those for the default specialisation (PolyLine<T, Container, alloc>):

    template<typename T, template<typename,typename> class Container, typename alloc>
    PolyLine<T, Container, alloc>::PolyLine(size_t size, double distance) 
    : Shape(), _n_points(size) 
    {
    }