simulate
simulate

Reputation: 1264

Create a different Parameter Pack from a Parameter Pack

I am not sure what it really is that I am trying to do, so please excuse me if the title is not fitting.

I have a class template taking a template parameter pack. I would like the constructor to expect types depending on the types in the parameter pack, more specifically, I would like to pass each parameter pack element to another class template (taking one argument) to construct the constructor parameters.

So this is the structure I am starting out with:

template<class ElementType>
struct ElementPreset{
//... stores a preset for an element of ElementType
};

template<class... Elements>
class WidgetPreset{
    WidgetPreset(ElementPreset<Elements...> pPresets)
        :presets(pPresets){}

    std::tuple<ElementPreset<Elements...>> presets;
}

The above code is not working, but what I am trying to do is to create a new parameter pack from Elements where each element is a ElementPreset<Element> instead of Element.

Is there any language support for this in C++?

Upvotes: 1

Views: 906

Answers (1)

max66
max66

Reputation: 66200

Maybe

template <typename ... Elements>
struct WidgetPreset{
    WidgetPreset(ElementPreset<Elements> ... pPresets)
        :presets(pPresets...){}

    std::tuple<ElementPreset<Elements>...> presets;
};

I mean...

(1) when you create a class, all is private if you don't define it public or protected; so your class construct was private

(2) the a ElementPreset<Elements...> pPresets is wrong: ElementPreset receive only a type; you have to unpack after the type: ElementPreset<Elements> ... pPresets

(3) same problem with the tuple: std::tuple<ElementPreset<Elements>...>, not std::tuple<ElementPreset<Elements...>>

(4) when you initialize preset you have to expand the pack: presets(pPresets...) (or presets{pPresets...}), not presets(pPresets)

(5) remember the ; at the end of the struct (or class)

Upvotes: 3

Related Questions