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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:02:41+00:00 2026-05-22T21:02:41+00:00

I’m having trouble with some features I want to implement in my config class,

  • 0

I’m having trouble with some features I want to implement in my config class, I have some values with diffrent types and I want to store them in an map. formerly i used ExtendedString class to store all data and convert them using a template function to the wanted type whenever I needed. and then had a map<ExtendedString,vector<ExtendedString> > in my config class to store all the keys in config file. here is what my ExtendedString class already look like:

class ExtendedString : public std::string
{
public:
    ExtenedString() : public std::string
    {
    }
    template <class T> ExtenededString(T)
    {
        std::stringstream s;
        *this = s.str();
    }
    template <class T>T as(){
        istringstream s(*this);
        T temp;
        d >> temp;
        return temp;
    }
}

now i want to parse all the values while loading my config file and also have the feature of converting to any possible type in case of need. for example if i parsed some value into an int and in my runtime i needed that value as string I want to be able to cast it right away, and besides i prefer not using boost in this case, it’s one of my base classes and I don’t want to link boost to every project i’m developing.

  • 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-22T21:02:42+00:00Added an answer on May 22, 2026 at 9:02 pm

    I’d recommend using boost::any, as it sounds like it does what you are looking for. However, if you really don’t want to use it, you might be able to handle it something like this. It may not be the best way to do it (its the first thing that came into my head) – basically its storing the value in the type you had it as originally, and as a string. If you try and get it as the original type, it returns that value, otherwise, it will use a std::stringstream to try and convert it.

    Note: This doesn’t handle copying/assignment, change d_data to your shared pointer of choice to handle that.

    #include <string>
    #include <sstream>
    #include <iostream>
    
    class ConfigValue
    {
        class HolderBase
        {
          protected:
            virtual void writeValueToStream(std::ostream& os) const = 0;
    
          public: 
            virtual ~HolderBase() { }
    
            template <class Type>
            Type getAs() const
            {
                std::stringstream ss;
                writeValueToStream(ss);
    
                Type temp;
                ss >> temp;
                return temp;
            }
        };
    
        template <class Type>
        class Holder : public HolderBase
        {
            Type d_value;
    
          protected:
            void writeValueToStream(std::ostream& os) const
            {
                os << d_value;
            }
    
          public:
            Holder(const Type& value)
            : d_value(value)
            {
                std::ostringstream oss;
                oss << value;
            }
    
            Type get() const
            {
                return d_value;
            }
        };
    
        HolderBase *d_data;
    
      public:
        template <class Type>
        ConfigValue(const Type& value)
        : d_data(new Holder<Type>(value))
        {
        }
    
        ~ConfigValue()
        {
            delete d_data;
        }
    
        template <class Type>
        Type get() const
        {
            if (Holder<Type> *holder = dynamic_cast<Holder<Type>*>(d_data))
            {
                return holder->get();
            }
            else
            {
                return d_data->getAs<Type>();
            }
        }
    };
    
    int main()
    {
        ConfigValue cv = 10;
    
        std::cout << cv.get<std::string>() << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

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.