Search code examples
c++templatesstltemplate-templates

template template parameter in std::pair


For a kd-tree I have a Node class which looks like following:

template<typename DataType, unsigned int Dim>
struct Node
{
    DataType* pos;
    DataType payload;
    struct Node<DataType, Dim>* left;
    struct Node<DataType, Dim>* right;
};

I am trying to set up a tuple type that combines the distance from the query node to the found neighbor and the neighbor node itself like so:

using std::pair<float, Node<typename DataType, Dim>*> = QueryResult;

unfortunately the above code gives me the error:

[...] error: wrong number of template arguments (1, should be 2)

So I tried a couple of different things, following some of the threads I found on here. First I thought maybe the second parameter should be declared without anything, but inside the Node class.

using std::pair<float, Node<DataType, Dim>*> = QueryResult;

[...] error: a template-id may not appear in a using-declaration

so then I tried telling the compiler that it is a nested template;

using std::pair<float, template Node<typename DataType, Dim>*> = QueryResult;

[...] error: template argument 2 is invalid

There was also a solution using an adaptor class, but that also did not work. I am unsure how I can set up what I am trying to do here. Any help or pointers would be greatly appreciated. I am not super versed in template programming, but am using this project to increase my knowledge.

Thanks!


Solution

  • You almost have it. Unlike a typedef a using declaration puts the alias as the fist symbol. That means

    using std::pair<float, Node<DataType, Dim>*> = QueryResult;
    

    needs to be

    using QueryResult = std::pair<float, Node<DataType, Dim>*>;
    

    If you don't know what DataType and Dim need to be at that point then you need to make it a template alias like

    template<typename DataType, unsigned int Dim>
    using QueryResult = std::pair<float, Node<DataType, Dim>*>;
    

    and then you would use it like

    QueryResult<type_you_want, dim_you_want> some_name;