-1
  1. I am trying to construct a union that will replace the pair of pointers (left & right) by a union which is accessible as an array. Originally this is working code for a Binary Search Tree (BST)

I want to be able to do this:

p = p->pLR.array[value>insertValue];

in addition to the old branched

if(value>insertValue) p = p->right;
else  p = p->left;

1b. This is not exactly to avoid costly branching misprediction, but simply to be able to learn to implement [something] like this.

  1. Mostly the problem that BinaryNode is templated in the union, and the union is an element of the BinaryNode!

Here is my non-compiling code-in-progress:

template    <class dataType, class BinaryNode<dataType> >
union BinaryNodePointerPair {
struct {
        BinaryNode<dataType> *left;
        BinaryNode<dataType> *right;
    };
    BinaryNode<dataType> *array[2]; 
};  
template    <class dataType>
struct BinaryNode {
    dataType    value;
    BinaryNodePointerPair<dataType,BinaryNode<dataType> > pLR;
};
[Error] 'BinaryNode' is not a template

refactored from:

template    <class dataType>
struct BinaryNode {
    dataType    value;
    BinaryNode  *left;
    BinaryNode  *right;
};
  1. What I have tried so far:

3a. According to 'X is not a template' error the template should be

template    <class dataType, class <dataType>BinaryNode >
[Error] expected identifier before '<' token
[Error] expected '>' before '<' token
[Error] type/value mismatch at argument 2 in template parameter list for 'template<class dataType, int <anonymous> > union BinaryNodePointerPair'

3b. So i change it to

template    <class dataType, class BinaryNode<dataType> >

But this gives the errors, plus the original.

[Error] 'BinaryNode' is not a class template
[Error] 'class BinaryNode' is not a valid type for a template non-type parameter
[Error] 'BinaryNode' is not a template

3c. So i again change it to

template    <class dataType, class BinaryNode<dataType> >

but this gives even more errors

[Error] 'BinaryNode' is not a class template
[Error] 'class BinaryNode' is not a valid type for a template non-type parameter
[Error] 'BinaryNode' is not a template
[Error] 'BinaryNode' is not a template type

3d. Now, this is eerily close to CRTP (no need to tell me this isnt real CRTP, but it is curiously)

template    <class dataType>
union BinaryNodePointerPair {
struct {
    BinaryNode<dataType> *left;
    BinaryNode<dataType> *right;
};
BinaryNode<dataType> *array[2]; 
};  
template    <class dataType, BinaryNodePointerPair<dataType> >
struct BinaryNode {
    dataType    value;
    BinaryNodePointerPair<dataType> pLR;
};
[Error] 'BinaryNode' is not a template

Only 1 error! WOW! This has got to be a winner!

3e. Now I really really force-pattern it into CRTP... it's even worse.

template    <class dataType, class BinaryNode<dataType> >
union BinaryNodePointerPair {
    struct {
        BinaryNode<dataType> *left;
        BinaryNode<dataType> *right;
    };
    BinaryNode<dataType> *array[2]; 
};  
template    <class dataType>
struct BinaryNode {
    dataType    value;
    BinaryNodePointerPair<dataType, BinaryNode<dataType>> pLR;
};
[Error] 'BinaryNode' is not a class template
[Error] 'class BinaryNode' is not a valid type for a template non-type parameter
[Error] 'BinaryNode' is not a template

It doesn't have to be CRTP. Is there a way to do it?

How do I write a union w/ pointers to a struct having an element of that union?

Community
  • 1
  • 1
Karl
  • 36
  • 5
  • to the rush downvoter: might I know what is wrong with the question? Which principle of "how to ask a good question" did I violate? How should I modify the question to make it better? – Karl Apr 02 '17 at 21:45
  • I downvoted, since I made some edits that are considered good here (reducing chat and meta commentary) and you overwrote them in a subsequent edit. (In general I do not recommend asking why people downvoted, since you will rarely get an answer - most people will vote and move on). – halfer Apr 02 '17 at 21:54
  • 1
    Have you actually observed a performance problem with just the struct? http://softwareengineering.stackexchange.com/questions/75390/why-should-i-care-about-micro-performance-and-efficency – Caleth Apr 02 '17 at 22:04
  • Good point with the premature optimization. The presumed performance issue is with the constant branching, not the struct. My goal is to create an alternative and then compare. But yes, I've not yet observed any real issues or bottlenecks. – Karl Apr 02 '17 at 22:14

1 Answers1

2

I don't really get the question, but did you perhaps forget the forward declaration of BinaryNode?

template <class dataType>
struct BinaryNode;

template <class dataType>
union BinaryNodePointerPair
{
  struct
  {
    BinaryNode<dataType> *left;
    BinaryNode<dataType> *right;
  };
  BinaryNode<dataType> *array[2]; 
};

template <class dataType>
struct BinaryNode
{
  dataType value;
  BinaryNodePointerPair<dataType> pLR;
};
Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • This actually works! I'd be pretty much ashamed of myself if I was not so ecstatic! How do I mark this as the answer? – Karl Apr 02 '17 at 21:50