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

  • Home
  • SEARCH
  • 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 9290971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:32:30+00:00 2026-06-18T20:32:30+00:00

I have the following class using 3 different maps: keys are always strings, while

  • 0

I have the following class using 3 different maps: keys are always strings, while values may be strings, integers or floats.

class MyMaps
{

public:

    template<typename T> void addKey(const std::string& key);
    void addValue(const std::string& key, const std::string& value);
    void addValue(const std::string& key, int value);
    void addValue(const std::string& key, float value);

private:

    std::map<std::string, std::string> stringFields;            
    std::map<std::string, int> intFields;                       
    std::map<std::string, float> floatFields;                   
};

The addValue() functions simply add a new pair to the related map. What I’m working on is the addKey() template function:

/** Add only a key, the related value is a default one and is specified by template parameter T. */

template<typename T>
void MyMaps::addKey(const string& key)
{       
    if (typeid(T) == typeid(string))
    {
        stringFields.insert(pair<string, string>(key, string()));
    }

    else if (typeid(T) == typeid(int))
    {
        intFields.insert(pair<string, int>(key, int()));;
    }

    else if (typeid(T) == typeid(float))
    {
        floatFields.insert(pair<string, float>(key, float()));
    }
}

Basically, I’m using template and typeid() because I don’t like this alternative that relies on type-within-function-name:

void MyMaps::addStringKey(const string& key) 
{
    stringFields.insert(pair<string, string>(key, string()));
}

void MyMaps::addIntKey(const string& key) 
{
    intFields.insert(pair<string, int>(key, int()));
}

void MyMaps::addFloatKey(const string& key) 
{
    floatFields.insert(pair<string, float>(key, float()));
}

The first addKey() version seems working, but I’m wondering if there is a more elegant solution. Maybe I’m missing some Object-Oriented design concept that could be helpful in this case?

Thanks in advance.

  • 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-18T20:32:32+00:00Added an answer on June 18, 2026 at 8:32 pm

    This is a perfect fit for template specialization:

    template<>
    void MyMaps::addKey<string>(const string& key)
    {       
        stringFields.insert(pair<string, string>(key, string()));
    }
    
    template<>
    void MyMaps::addKey<int>(const int& key)
    {   
        intFields.insert(pair<string, int>(key, int()));;
    }
    
    template<>
    void MyMaps::addKey<float>(const float& key)
    {   
        floatFields.insert(pair<string, float>(key, float()));
    }
    

    EDIT: For syntax/more info about template specialization read: Template Specialization and Partial Template Specialization

    Or better yet, if boost is an option and if the keys are unique for all 3 maps and you have 3 different maps just to be able to store them, then consider using boost::variant:

    typedef boost::variant<string, int, float> ValueType;
    
    class MyMap
    {
    
    public:
        typedef std::map<std::string, ValueType> MapType;
        template<typename T> void addKey(const std::string& key, T &val)
        {
            ValueType varVal= val;
            allFields.insert(MapType::value_type(key, varVal));
        }
    
    private:
    
        MapType allFields;                              
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
I am using Tornado and I have the following code: class UserHandler(RequestHandler): def get(self):
I am using appengine, webapp2 framework. I have the following model: class Match(db.Model): date_time
I'm using the Code First approach and have the following Model: public class Person
I'm using flask-sqlalchemy to build a webapp and I have the following model(s): class
I am using Rails 3.2. I have following models: Blog Comment User class Blog
I am using EntityFramework 4.1. I have the following model: public class User {
I am using the XmlSerializer and have the following property in a class public
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer

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.