简体   繁体   English

C ++模板递归-如何解决?

[英]C++ template recursion - how to solve?

Im stuck again with templates. 我再次陷入模板。

say, i want to implement a guicell - system. 例如,我要实现guicell-系统。 each guicell can contain a number of child-guicells. 每个guicell可以包含多个子guicell。 so far, so tree-structure. 到目前为止,树状结构。 in std-c++ i would go for sthg. 在std-c ++中,我会考虑。 like: 喜欢:

template <typename T>
class tree
{
public:

    void add (T *o)     { _m_children.push_back (o); }
    void remove (T *o)  { ... };

    list<T*> _m_children;
};

class _cell : public tree<_cell>
{
public:
    _cell ()            { x = 0; y =0; }
    long                x,y;
};

But now i want to go further, and make the cells referencable if the coder wishes so. 但是现在我想走得更远,如果编码人员愿意的话,使这些单元可引用。 so i basically implement a refTree - class for that purpose that also takes just pointers (_cell*) as input. 因此,我基本上为此实现了refTree-类,该类也仅将指针(_cell *)作为输入。

template <typename T>
class refTree
{
public:
   void add (T *o)      { _ref<T> r = o;  _m_children.push_back (r);  }
   void remove (T *o)       { ... }

   list<_ref<T> > _m_children;
};

Also this is still working fine. 此外,这仍然可以正常工作。 using 使用

class _cell : public refTree<_cell>
{
   :
};

no changes to the user-code, but all added _cell* are now referenced before they are added to the tree. 无需更改用户代码,但现在将引用所有添加的_cell *,然后再将其添加到树中。

Good, but now i want to be able to chose on _cell - level which tree-template implementation to use. 很好,但是现在我希望能够在_cell级别上选择要使用的树模板实现。 So this means i have to make the _cell - class a template class that takes a template class as parameter (the chosen tree template). 因此,这意味着我必须使_cell-类成为一个模板类,该模板类将模板类作为参数(所选的树模板)。

template <template <typename> class __TyTree = tree>
class Cell : public __TyTree <Cell>     // cannot work - no question, Cell expects input
{
};

And here we got the recursive problem - ofcourse the compiler can't resolve that, because Cell is awaiting a tree - parameter which is expecting a simple-type parameter (which should be a Cell ofc. is awaiting a tree - parameter which is expecting a simple.... ). 这就是递归问题-当然,编译器无法解决该问题,因为Cell正在等待一棵树-参数期望一个简单类型的参数(应该是Cell ofc。正在等待一棵树-参数正在期望一个简单的.... )。

You get the picture - what is a proper solution to that kind of problem ? 您明白了吗-解决此类问题的正确解决方案是什么?

There is no recursion. 没有递归。 Template argument for Cell is __TyTree , not __TyTree<Cell> . Cell模板参数是__TyTree ,而不是__TyTree<Cell>

template <template <typename> class __TyTree = tree>
class Cell : public __TyTree <Cell<__TyTree> >
{
};

int main()
{
   Cell          mycell0; // error
   Cell<>        mycell1; // ok. tree is used
   Cell<tree>    mycell2;
   Cell<refTree> mycell3;
}

PS You should not use two leading underscores in __TyTree because it is reserved for implementation purposes by C++ Standard. PS不应在__TyTree使用两个前导下划线,因为它是C ++ Standard保留用于实现的目的。

You have two separate problems: what's in a cell and how cells are connected to each other. 您有两个独立的问题:单元中的内容以及单元之间的连接方式。 You need to use two separate data structures for that. 为此,您需要使用两个单独的数据结构。 In other words, the right link to break is the inheritance of cell from tree. 换句话说,断开连接的正确链接是从树继承单元格。 The tiny amount of memory you save is not worth it - drop the "intrusive" approach. 您节省的微小内存是不值得的-放弃“侵入式”方法。 The cell should not be aware of the way the tree holds it. 单元不应该知道树的固定方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM