0

I have the following structure:

Two possible algorithms, one inherits the other, whose template parameter is a self defined container class.

template <class C>
class AlgBase {
    protected:
        C c;
};

template <class C>
class AlgDerived : public AlgBase<C> {};

The container class is templated as well, and the parameters are also related by inheritance:

class TParamBase {};
class TParamDerived : public TParamBase {};

template <class T> // T is TParamBase or TParamDerived
class Container {
    protected:
        T t_;
};

Now, in my main, I want to do the following:

typedef Container<TParamBase> ContainerBase;
typedef Container<TParamDerived> ContainerDerived;

int main (int argc, const char ** argv)
{
    vector<AlgBase<ContainerBase>* > algs;
    algs.push_back(new AlgBase<ContainerBase>);
    algs.push_back(new AlgDerived<ContainerBase>);
    algs.push_back(new AlgDerived<ContainerDerived>);
}

The first two push_back work since they use ContainerBase. The third fails because the use of ContainerDerived. The error is:

error: no matching function for call to ‘std::vector<AlgBase<Container<TParamBase> >*>::push_back(AlgDerived<Container<TParamDerived> >*)’
     algs.push_back(new AlgDerived<ContainerDerived>);
                                                    ^
main.cpp:34:52: note: candidates are:
In file included from /usr/include/c++/4.9/vector:64:0,
                 from main.cpp:3:
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = AlgBase<Container<TParamBase> >*; _Alloc = std::allocator<AlgBase<Container<TParamBase> >*>; std::vector<_Tp, _Alloc>::value_type = AlgBase<Container<TParamBase> >*]
       push_back(const value_type& __x)
       ^
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note:   no known conversion for argument 1 from ‘AlgDerived<Container<TParamDerived> >*’ to ‘AlgBase<Container<TParamBase> >* const&’
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = AlgBase<Container<TParamBase> >*; _Alloc = std::allocator<AlgBase<Container<TParamBase> >*>; std::vector<_Tp, _Alloc>::value_type = AlgBase<Container<TParamBase> >*]
       push_back(value_type&& __x)
       ^
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note:   no known conversion for argument 1 from ‘AlgDerived<Container<TParamDerived> >*’ to ‘AlgBase<Container<TParamBase> >*&&’

The only solution I have in mind is to forget about TParamBase and TParamDerived and only use one class, but this is a bit inefficient since I will be creating and allocating elements not used.

Is there any other solution to make the third push_back work?

Thank you!

Javi
  • 3,440
  • 5
  • 29
  • 43
  • @gha.st thank you, I saw that one but for some reason I thought it was not the same. I will check it ouy. – Javi Jan 29 '15 at 12:44
  • If you find any difficulties applying a workaround, please let us know. If you find that the duplicate doesn't help you solve the problem, we can discuss to reopen your question. – leemes Jan 29 '15 at 12:45
  • Your `ContainerBase` and `ContainerDerived` are fundamentally unrelated. Consider replacing both with a `Container` (or smart pointer equivalent). – Tony Delroy Jan 29 '15 at 12:48

0 Answers0