arynaq
arynaq

Reputation: 6870

Variadic class template, multiple inheritance and template unpacking

Consider the following simple example

struct Banana{

};
struct Apple{

};
struct Watermelon{

};


template<typename Fruit>
struct Stand {
protected:
    Fruit& get();
private:
    Fruit fruit_;
};


template<typename... Stands>
struct TownFairStands : private Stands... {
    template<typename Fruit>
    Fruit& get() {
        return Stand<Fruit>::get();
    }
};

int main(){
    TownFairStand<Banana, Apple, Watermelon> stand;
    TownFairStands<Stand<Banana>, Stand<Apple>, Stand<Watermelon>> stand2;
    return 0;
}

The ugly way of defining a TownFairStand is the one defined with stands2. But I would like the option of the cleaner interface defined with stand.

However I am stuck on trying to figure out how I can create this interface

template<typename... Fruits>
struct TownFairStand : private ??????{

    template<typename Fruit>
    Fruit& get(){
        return Stand<Fruit>::get();
    }


};

What goes in place of ?????

Upvotes: 0

Views: 27

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

Stand<Fruits>...

You want a base class of type Stand<X> for every X in Fruits, so the pattern to expand is Stand<Fruits> and that gets expanded by ... for every element of the pack.

Upvotes: 2

Related Questions