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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:02:14+00:00 2026-05-17T15:02:14+00:00

I have inherited a template to convert a string to a numerical value, and

  • 0

I have inherited a template to convert a string to a numerical value, and want to apply it to convert to boolean. I am not very experienced with the stringstream and locale classes. I do seem to be getting some odd behaviour, and I am wondering if someone could please explain it to me?

template<typename T> T convertFromString( const string& str ) const {

std::stringstream SStream( str );  
T num = 0; 
SStream >> num;

return num;  
}  

This works fine until I try the boolean conversion

string str1("1");
int val1 = convertFromString<int>(str1); // ok
string str2("true");
bool val2 = convertFromString<bool>(str2); // val2 is _false_

I spent some time tracking down the problem. I have confirmed that the locale’s truename() returns “true”.

The problem seems to be with the initialisation of the variable num. I can change the template to this and it works:

template<typename T> T convertFromString( const string& str ) const {

std::stringstream SStream( str );  
T num;  // <----------------------- Changed here
SStream >> num;

return num;  
}  
string str2("true");
bool val2 = convertFromString<bool>(str2); // val2 is _true_

Why does it work? I accept that initialising a bool with ‘0’ is wrong, but why would this cause the SStream>>numconversion to fail?

  • 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-17T15:02:14+00:00Added an answer on May 17, 2026 at 3:02 pm

    Initialising a bool with 0 will reliably set it to false, and this has no effect on the stream extraction.

    What is causing your problem is that streams by default only recognize the values 0 and 1 when dealing with booleans. To have them recognize the names true and false, you need to tell that explicitly to the stream with the boolalpha manipulator.

    The best way to solve your problems is to specialize the template for bool:

    template<> bool convertFromString<bool>( const string& str ) const {
      std::stringstream SStream( str );  
      bool val = false;
      SStream >> val;
      if( SStream.fail() )
      {
        SStream.clear(); 
        SStream >> boolalpha >> val; 
      }    
      return val;  
    }  
    

    Note that your change did not make the code work. It just appeared to do so for the single testcase you used.
    With your change, the function failed to read from the stream and returned an uninitialised value. As any non-zero value will be interpreted as true, the function appears to work, but as soon as you try to extract "false", you will see it fail (the function still returns true).

    Edit: Adapted the code to handle both numeric and alpha bools.

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

Sidebar

Related Questions

Possible Duplicate: C++ Inherited template classes don't have access to the base class I'm
Is there any way to have a template inherit another template? I'm not using
I have inherited couple of .Net (C#) application which does not have any tracing
I have inherited search results template in an EE 1.6.8 install and would like
I have the inherited the following string (I can do nothing about the format):
I have inherited the following URL template from our old RESTful (supposedly) service: http://{host}:{port}/{handle}?{extraQualifier}
I have inserted the following line into my Joomla template: <jdoc:include type=component /> This
We have inherited an application (Java-based, running on WebLogic 10.3.5) that makes extensive use
I have inherited a Drupal 6 site to maintain. I am new to Drupal
I have inherited work from a previous employee. The issue I'm having is 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.