将self-class name作为模板参数传递时,C ++ covariant-return错误

时间:2015-03-06 15:37:44

标签: c++ templates c++98 covariant

我收到了这个错误:

prog.cpp: In instantiation of 'class Tree<int, C<int> >':
prog.cpp:13:16: error: invalid covariant return type for
    'Self* Tree<LeafT, Self>::branch() [with LeafT = int; Self = C<int>]'
     Self * branch() {}
            ^
prog.cpp:7:34: error: overriding
    'ATree<LeafT>* ATree<LeafT>::branch() [with LeafT = int]'
     virtual  ATree< LeafT> * branch() = 0;
                              ^

尝试实例化 C&lt;对此代码进行int&gt;

template< typename LeafT>
  class ATree {
  public:
      virtual  ATree< LeafT> * branch() = 0;
  };

template< typename LeafT, typename Self>
  class Tree : public ATree< LeafT> {
  public:
      Self * branch() {}
  };

template< typename LeafT>
  class C : public Tree< LeafT, C< LeafT> > {};

它是我的真实代码(http://ideone.com/lCw5OT)的模型,我无法理解为什么我得到协变返回错误,因为 C&lt; int&gt; 实际上是 Tree&lt; int,C&lt; INT&GT; &gt; ATree的孩子&lt; INT&GT; <!/ em>的

我使用的是C ++ 98,遗憾的是,它不能使用C ++ 11扩展(如果可能有帮助的话)。

我失踪了什么?

0 个答案:

没有答案