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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:20:05+00:00 2026-05-12T15:20:05+00:00

I have the following function that will convert a string into a numeric data

  • 0

I have the following function that will convert a string into a numeric data type:

template <typename T>
bool ConvertString(const std::string& theString, T& theResult)
{
    std::istringstream iss(theString);
    return !(iss >> theResult).fail();
}

This does not work for enumerated types, however, so I have done something like this:

template <typename T>
bool ConvertStringToEnum(const std::string& theString, T& theResult)
{
    std::istringstream iss(theString);
    unsigned int temp;
    const bool isValid = !(iss >> temp).fail();
    theResult = static_cast<T>(temp);
    return isValid;
}

(I’m making the assumption that theString has a valid value for the enumerated type; I’m using this mainly for simple serialization)

Is there a way to create a single function that combines both of these?

I’ve played a bit with the template arguments but haven’t come up with anything; it’d just be nice not to have to call one function for enumerated types and another for everything else.

Thank you

  • 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-12T15:20:05+00:00Added an answer on May 12, 2026 at 3:20 pm

    You have to do two steps. Finding an integral type large enough to store the values. You could use unsigned long, but the values could be negative. Then you could use long but the values could extend into the range of unsigned long. So there is not really a fit-it-all type.

    There is a trick though, by using overload resolution. Here is it

    template<typename T>
    struct id { typedef T type; };
    
    id<char[1]>::type &find_etype(int);
    id<char[2]>::type &find_etype(unsigned int);
    id<char[3]>::type &find_etype(long);
    id<char[4]>::type &find_etype(unsigned long);
    

    You can change it appropriately to cover also long long or unsigned long long if your implementation has support for that. Now, passing an enum type will prefer one of these over all the other ones – that’s a type that can store all values of it. You just need to pass sizeof of the return type to some template.

    template<int> struct get_etype;
    template<> struct get_etype<1> { typedef int type; };
    template<> struct get_etype<2> { typedef unsigned int type; };
    template<> struct get_etype<3> { typedef long type; };
    template<> struct get_etype<4> { typedef unsigned long type; };
    

    Now, you can get a right type. All you need now is to see whether some type is an enumeration. How to do this is described in the book “C++ Templates – The complete Guide”, and unfortunately is a whole lot of code. So i would use boost’s is_enum. Putting it together, it could look like

    template <typename T>
    typename boost::disable_if< boost::is_enum<T>, bool>::type 
    ConvertString(const std::string& theString, T& theResult)
    {
        std::istringstream iss(theString);
        return !(iss >> theResult).fail();
    }
    
    template <typename T>
    typename boost::enable_if< boost::is_enum<T>, bool>::type 
    ConvertString(const std::string& theString, T& theResult)
    {
        typedef typename get_etype<sizeof find_etype(theResult)>::type 
          safe_type;
    
        std::istringstream iss(theString);
        safe_type temp;
        const bool isValid = !(iss >> temp).fail();
        theResult = static_cast<T>(temp);
        return isValid;
    }
    

    Hope this helps.

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • 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 Are you using a custom-rolled player? I know the FLVPlayback… May 13, 2026 at 1:23 am
  • Editorial Team
    Editorial Team added an answer I think the general paradigm used with table views and… May 13, 2026 at 1:23 am
  • Editorial Team
    Editorial Team added an answer We dogfood our products, and VS is no exception. /me… May 13, 2026 at 1:23 am

Related Questions

I've written a very simple GUI in MATLAB that will convert temperatures. It is
I have created a function which will convert any string into tab delimited. What's
I have the following code in VB.net Dim bytIV() As Byte = {121, 241,
I have a char in c#: char foo = '2'; Now I want to

Trending Tags

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

Top Members

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.