dimanche 4 juin 2017

Shorter way of calling templated base function

I know the answer to this question might be simply "No" but I'm hoping that if it is "no" then I'll get a suggestion of doing thing differently. Consider the following:

class IFoo
{
public:
    virtual void Foo() = 0;
};

class IBar : public virtual IFoo
{
public:
    virtual void Bar1() = 0;
    virtual void Bar2() = 0;
};

template <typename T>
class BaseFoo : public T
{
public:
    virtual void Foo() override
    {
        std::cout << "Common behavior Foo" << std::endl;
    }
};

template <typename T>
class BaseBar1 : public T
{
public:
    virtual void Bar1() override
    {
        std::cout << "Common behavior Bar1" << std::endl;
    }
};

Now in my class I want to use the base implementation for Foo and only in some conditions use the base implementation of Bar1. So naturally I inherit from BaseFoo1<BaseBar1<IBar>> and in my class's Bar1 I will call the base sometimes (or even all the times if it made sense).

class MyClass : public BaseFoo<BaseBar1<IBar>>
{
public:
    void Bar1() override
    {
        if(SomeCondition())
        {
            std::cout << "MyClass Bar1" << std::endl;
        }
        else
        {
            BaseFoo<BaseBar1<IBar>>::Bar1(); 
        }
    }
    void Bar2() override {}
    bool SomeCondition() { return false;}
};

What I'm interested in, is knowing if there is some way to shorten the call to the base function. I considered using decltype(this) (or std::decay<decltype(this)>) and then I search for a way to use template metaprogramming to get the base type, but I couldn't find anything useful.

1 - Is there a way to shorten the call to the templated base?

2 - Any comments on the design?

I'm using C++11 in my project, but any suggestion up to C++17 would be nice.

Aucun commentaire:

Enregistrer un commentaire