2

I got this error:

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;
                              ^

while trying to instantiate C< int> against this code:

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> > {};

It's a model of my real code (http://ideone.com/lCw5OT), and I cannot understand why I'm getting the covariant return error since C< int> is actually a child of Tree< int, C< int> > which is a child of ATree< int>!

I'm using C++98 and, sadly, can't use C++11 extensions (if it could help).

What I'm missing?

Valentin T.
  • 519
  • 3
  • 12

0 Answers0