キキジキ
キキジキ

Reputation: 1453

Mixing variadic templates and initializer list in a constructor

I have a class for holding a list of parameters of generic types (ParameterList). A number of classes usually accepts a ParameterList in the constructor, but some need only a few parameters so I also want to supply a simplified version.

The problem is with this second version. I wrote an example to show a typical usage:

#include <string>
#include <map>
#include <memory>
#include <vector>
#include <iostream>

struct Parameter
{
};

template<typename T>
struct TypedParameter : public Parameter
{
    TypedParameter (const T& data): data(data){};
    T data;
};

struct ParameterList
{
    std::map<std::wstring, std::shared_ptr<Parameter>> list;

    template<class T> void addParameter(const std::wstring& name, const T& param)
    {
        list[name] = std::shared_ptr<Parameter>(new TypedParameter<T>(param));
    }

    template<class T> T& getParameter(const std::wstring& name) const
    {
        return static_cast<TypedParameter<T>*>(list.at(name).get())->data;
    }
};

class Test
{
private:
    /*const*/ ParameterList _param;
protected:
public:
    Test(ParameterList p):
        _param(p)
    {
    }

    Test(const std::wstring& name, int age)
    {
        _param.addParameter<std::wstring>(L"name", name);
        _param.addParameter<int>(L"age", age);
    }

    void Present()
    {
        std::wcout << L"My name is " << _param.getParameter<std::wstring>(L"name");
        std::wcout << L" and I'm " << _param.getParameter<int>(L"age") << L" years old." << std::endl;
    }
};

int main()
{
    ParameterList l;

    l.addParameter<int>(L"age", 16521);
    l.addParameter<std::wstring>(L"name", L"Bahamut");

    std::wcout << L"name: " << l.getParameter<std::wstring>(L"name") << std::endl;
    std::wcout << L"age: " << l.getParameter<int>(L"age") << std::endl;

    Test t1(l);
    Test t2(L"Tiamat", 15525);

    t1.Present();
    t2.Present();

    getchar();
}

t2 works, but there are mainly two problems. First I have to create a new constructor, and second I have to give up making "_param" a const.

I was thinking of adding a new constructor to ParameterList, something like this:

template<typename... Values>
ParameterList(std::initializer_list<std::wstring> keys, const Values&... values)
{
    //?
}

//Later
Test t3({L"name", L"age"}, L"Vorel", 19870);

But how can I unpack the values correctly?

Upvotes: 2

Views: 3736

Answers (2)

PiotrNycz
PiotrNycz

Reputation: 24382

You can do what you wanted originally without extra std::pair stuff like in other answer. Just extract arguments in one step - one for name, one for value.

struct ParameterList
{
    template <class... NamedParams>
    ParameterList(NamedParams... namedParams)
    {
        buildList(namedParams...);
    }
//...
private:
    template <class... NamedParams>
    void buildList() {}
    template <class Name, class Value, class... NamedParams>
    void buildList(Name name, Value value, NamedParams... restParams)
    {
        addParameter(name, value);
        buildList(restParams...);
    }
 };

Then in your Test class, just use this:

class Test
{
private:
    const ParameterList _param;
protected:
public:
    Test(ParameterList p):
        _param(p)
    {
    }

    Test(const std::wstring& name, int age) : _param(L"name", name, L"age", age)
    {
    }

//...
};

Full working example here at ideone.

Upvotes: 3

Potatoswatter
Potatoswatter

Reputation: 137780

std::initializer_list is for making non-template functions that accept a compile-time variable number of arguments. There's no point in mirroring a variadic pack with an initializer_list.

Try making a pack of tuples or pairs:

template<typename... Values>
ParameterList(std::pair< std::wstring, Values > const &... keyvalues)

Test t3(std::make_pair(L"name", L"Vorel"), std::make_pair(L"age", 19870));

The dissimilar arguments must be generated with make_pair, they cannot be deduced from a braced-init-list, because a std::initializer_list must have elements all of the same type, and the template arguments to std::pair cannot be deduced from a braced-init-list, nor can the compiler even figure out that the given overload is appropriate in the first place.

Upvotes: 3

Related Questions