如何使用结构作为模板编写联合,同时将该联合作为元素

How to code a union using a struct as template, while having that union as an element

  1. 我正在尝试构建一个联合来替换这对指针(左和右) 通过一个可以作为数组访问的联合。 最初这是二叉搜索树 (BST) 的工作代码

我希望能够做到这一点:

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

除了老支

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

1b。这不完全是为了避免代价高昂的分支错误预测, 但只是为了能够 学习 来实现这样的 [东西]。

  1. 主要是BinaryNode模板化在union中的问题,union是BinaryNode的一个元素!

这是我正在进行的非编译代码:

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

重构自:

template    <class dataType>
struct BinaryNode {
    dataType    value;
    BinaryNode  *left;
    BinaryNode  *right;
};
  1. 到目前为止我尝试过的:

3a。根据 'X is not a template' error 模板应该是

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。所以我把它改成

template    <class dataType, class BinaryNode<dataType> >

但是这个给出了错误,加上原文。

[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。所以我再次将其更改为

template    <class dataType, class BinaryNode<dataType> >

但这会产生更多错误

[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

3天。现在,这非常接近 CRTP (不用告诉我这不是真正的 CRTP,但很奇怪)

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

只有1个错误!哇!这一定是赢家!

3e。现在我真的真的将它强行模式化为 CRTP...它更糟。

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

不一定是CRTP。 有办法吗?

如何编写带有指向具有该联合元素的结构的指针的联合?

我不太明白这个问题,但你是不是忘记了 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;
};