簡體   English   中英

如何使用結構作為模板編碼聯合,同時將該聯合作為元素

[英]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在聯合中被模板化,並且聯合是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不是模板”錯誤 ,模板應為

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

3d。 現在,這非常接近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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM