C ++: calling the template method of a base class from a child of the template, although multi-level inheritance

I want to call a base class A method from a child class D, which inherits it through C :: A and B :: A.

template <class PType>
class A
{
public:
    template <class ChildClass>
    void Func( void )
    {
        std::cout << "Called A<PType>::Func<ChildClass>" << std::endl;
    }
};

template <class PType>
class B : public A<PType>
{
};

template <class PType>
class C : public A<PType>
{
};

class D : public B<int>, public C<int>
{
public:
    D()
    {
        static_cast<B<int>*>( this )->A<int>::Func<D>();
        static_cast<C<int>*>( this )->A<int>::Func<D>();
    }
};

This works as expected, D calls both B :: A :: Func and C :: A :: Func with the template argument of the child class during initialization. However, this does not work when D is a template class.

template <class PType>
class D2 : public B<PType>, public C<PType>
{
public:
    D2()
    {
        //expected primary-expression before ‘>’ token
        //expected primary-expression before ‘)’ token
        static_cast<B<PType>*>( this )->A<PType>::Func< D2<PType> >();
        static_cast<C<PType>*>( this )->A<PType>::Func< D2<PType> >();
    }
};

The problem, apparently, is the argument of the D2 template for Func, but cannot figure out what's next.

+4
source share
1 answer

, /type/ template template, , .

, , template.

, , , template, < > , - .

. , , typename. , template .

- :

   static_cast<B<PType>*>( this )->template A<PType>::template Func< D2<PType> >();

, template, . :

   static_cast<B<int>*>( this )->A<int>::Func< D2<PType> >();

template B<int>, A ( ) template. , ->A<int> template, ::Func ( ) template.

+1

Source: https://habr.com/ru/post/1539077/


All Articles