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.
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;
};
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?
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;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.