3

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!

SergeyA
  • 61,605
  • 5
  • 78
  • 137
meetaig
  • 913
  • 10
  • 26
  • 3
    `using [new alias' name] = [what it aliases];`. You're using it like `typedef`. – François Andrieux Jan 22 '19 at 16:29
  • 2
    Greetings, Master Yoda! I took the liberty of rephrasing your question such that it would be better understandable by us, mere mortal - by putting Node definition first, and questions about it later. – SergeyA Jan 22 '19 at 16:30
  • It is unclear what you are doing here. Where is this `DataType` expected to come from? – SergeyA Jan 22 '19 at 16:33

2 Answers2

4

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;
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I am still unsure where DataType should be coming from. Does OP need a template alias? – SergeyA Jan 22 '19 at 16:34
  • @SergeyA Just added that as I'm not sure as well. – NathanOliver Jan 22 '19 at 16:36
  • Thank you very much. Indeed it seems I got the order wrong.. stupid mistake. The template alias I definitely need, as I want the kdtree to work with a templated point class that has templated datatype and number of dimensions. This solution above seems to work! Again, thanks! :) – meetaig Jan 22 '19 at 16:41
1

as commented:

template<typename DataType, unsigned Dim>
using QueryResult = std::pair<float, Node<DataType, Dim>*>;
Walter
  • 44,150
  • 20
  • 113
  • 196