0

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 ?????

2
  • 1
    Stand<Fruits>... Commented Apr 5, 2018 at 11:11
  • So simple, guess I didn't know what to google for, thanks! Commented Apr 5, 2018 at 11:16

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

5 Comments

Out of curiosity, what if Stand<X> and Stand<Y> have different constructor? I tried with a forwarding vararg constructor for TownFairStand godbolt.org/g/u5uq33
That constructor doesn't expand the Fruits pack, so it tries to use all the args... to construct a single base class of type Stand<Fruits> which is invalid (there is no such single base class).
I guess I need a way to "peel off" args as I pass them in to the constructor of the derived class because this one tries to match the args to each Stand<X> godbolt.org/g/w6WVEc
Yes. Now you're passing every argument to every base class.
Is there a clean way to pass them off to each Stand<X> or do I have to implement a bunch of SFINAE helper structs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.