Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:55:31+00:00 2026-06-14T11:55:31+00:00

I have a class for holding a list of parameters of generic types (ParameterList).

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:55:32+00:00Added an answer on June 14, 2026 at 11:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Generic list holding root objects means I have: public class Component {
I have a class holding the list of contacts of my user in an
I have a class holding entries in a list, which can be cleared and
I have a List of Lists, with each list holding a different class of
I have a class holding a table (list of lists). This class should return
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
I have a Foo object, and a std::list holding instances of it. My problem
Suppose I have two classes class A { b bInstance; public A ( b
Hi I have made checked List Box in WPF but the binding is not
I have a templated class holding a value. Is it possible to add a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.