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 1016425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:32:51+00:00 2026-05-16T10:32:51+00:00

What I would like to do (in C++) is create a ‘Parameter’ data type

  • 0

What I would like to do (in C++) is create a ‘Parameter’ data type which has a value, min, and max. I would then like to create a container for these types.

E.g. I have the following code:

    template <typename T>
    class ParamT {
    public:
        ParamT() {
        }

        ParamT(T _value):value(_value) {
        }

        ParamT(T _value, T _vmin, T _vmax):value(_value), vmin(_vmin), vmax(_vmax) {
        }


        void setup(T vmin, T vmax) {
            this->vmin      = vmin;
            this->vmax      = vmax;
        }

        void setup(T value, T vmin, T vmax) {
            setup(vmin, vmax);
            setValue(value);
        }

        T operator=(const T & value) {
            setValue(value);
        }



        void setValue(T v) {
            value = v;
        }

        T getValue() {
            return value;
        }

        operator T() {
            return getValue();
        }

    protected:

        T       value;
        T       vmin;
        T       vmax;
    };

    typedef ParamT<int>     Int;
    typedef ParamT<float>   Float;
    typedef ParamT<bool>    Bool;

In an ideal world my Api would be something like:

std::map<string, Param> params;
params["speed"] = PFloat(3.0f, 2.1f, 5.0f);
params["id"] = PInt(0, 1, 5);

or

params["speed"].setup(3.0f, 2.1f, 5.0f);
params["id"].setup(0, 1, 5);

and writing to them:

params["speed"] = 4.2f;
params["id"] = 1;

or

params["speed"].setValue(4.2f);
params["id].setValue(1);

and reading:

float speed = params["speed"];
int id = params["id"];

or

float speed = params["speed"].getValue();
int id = params["id"].getValue();

Of course in the code above, ParamT has no base class so I cannot create a map. But even if I create a base class for it which ParamT extends, I obviously cannot have different getValues() which return different types. I thought about many solutions, including setValueI(int i), setValuef(float f), int getValueI(), float getValueF(), or a map for ints, a map for floats etc. But all seem very unclean. Is it possible in C++ to implement the above API?

At the moment I am only concerned with simple types like int, float, bool etc. But I would like to extend this to vectors (my own) and potentially more.

  • 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-05-16T10:32:52+00:00Added an answer on May 16, 2026 at 10:32 am

    It’s a tough concept to implement in C++, as you’re seeing. I’m always a proponent of using the Boost library, which has already solved it for you. You can typedef the complex boost variant template class to something more usable in your specific domain, so

    typedef boost::variant< int, float, bool > ParamT; 
    class Param
    {
       public:
          // initialize the variants
          Param(ParamT min, ParamT max, ParamT value)
           : m_Min(min), m_Max(max), m_Value(value) {}
    
          // example accessor
          template<typename OutT>
          const ParamT& value()
          {
              return boost::get<OutT>(m_Value);
          }
          // other accessors for min, max ...
       private:
          ParamT m_Min, m_Value, m_Max;
    };
    
    
    
    Param speed(-10.0f, 10.0f, 0.0f);
    float speedValue = speed.value<float>();
    

    Now, to add another type to your variant (eg, long, std::string, whatever) you can just modify the typedef of ParamT; The catch, here, is that the burden of checking the types is on you – it’ll throw an exception if you store a float and try to receive an int, but there’s no compile-time safety.

    If you want to get really crazy, you can implement an overloaded cast operator on a proxy object….

    class ProxyValue
    {
    public:
      ProxyValue(ParamT& value) : m_Value(value) {}
      template<typename ValueT>
      operator ValueT()
      {
         return boost::get<ValueT>(m_Value);
      }
    private:
      ParamT& m_Value;
    };
    

    You’d return this from a non-templated value() function in Param, instead of the variant itself. Now you can assign a value without the template call..

    Param speed(-10.0f, 0, 10);
    float speedValue = speed.value();
    

    Though fair warning, you’re stepping into meta-programming hell here. Here thar be dragons. And as always, this is not a complete solution, just a pointer. YMMV.

    Heres a roughly working version showing how to use it, and the failures that are easy to hit.

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

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could figure out what subdomain you're on right now… May 16, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Not in JavaScript no...this could have some pretty malicious uses.… May 16, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer By trying to hide the REST layer behind a repository… May 16, 2026 at 11:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like create my own collection that has all the attributes of python
I would like create a database using value from a variable. Here my script
I would like to create a link that pass a parameter, so I don't
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like create a service to do something when some hot situation occurs,
I would like create an application similar to the safari browser, What is the
I would like create a very simple Paint application using MAF on WPF. The
I would like create URL rewrite rule that will set default document for my
I would like create a custom DataRow that will have -let's say- a propery
I would like create a GLOBAL VARIABLE in a Sql script. For my understanding

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.