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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:25:26+00:00 2026-06-13T02:25:26+00:00

I have a function that aims to convert a string from a config file

  • 0

I have a function that aims to convert a string from a config file in to various different types. A special case has needed to be inserted to handle bool’s as “false” equates to true when using a string stream.

Separate functions are not really an option as it would involve type checking for every type we are using.

The function worked correctly when part of a class previously, however to make it more usable it was moved in to its own start function. The function throws 2 errors on the return true’s. The return false’s are as expected.

Below is the code example and the errors thrown by visual studio.

template <typename T>
static T StringCast(string inValue)
{
    if(typeid(T) == typeid(bool))
    {
        if(inValue == "true")
            return true;
        if(inValue == "false")
            return false;
        if(inValue == "1")
            return true;
        if(inValue == "0")
            return false;
        return false;
    }

    std::istringstream stream(inValue);
    T t;
    stream >> t;
    return t;
}

Error 1 error C2664: ‘std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)’ : cannot convert parameter 1 from ‘bool’ to ‘const std::basic_string<_Elem,_Traits,_Ax> &’

Error 2 error C2664: ‘std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)’ : cannot convert parameter 1 from ‘bool’ to ‘const std::basic_string<_Elem,_Traits,_Ax> &’

  • 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-13T02:25:28+00:00Added an answer on June 13, 2026 at 2:25 am

    If you want to have specialization for bool – then just define specialization for bool. Your way is not possible. Use the proper way below:

    template <typename T>
    T StringCast(string inValue)
    {
        std::istringstream stream(inValue);
        T t;
        stream >> t;
        return t;
    }
    
    template <>
    bool StringCast<bool>(string inValue)
    {
            if(inValue == "true")
                return true;
            if(inValue == "false")
                return false;
            if(inValue == "1")
                return true;
            if(inValue == "0")
                return false;
            return false;
    }
    
    int main() {
       int a = StringCast<int>("112");
       bool b = StringCast<bool>("true"); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
I have a function that loads a SVG Dom from a file. Currently, it
I have a function that is supposed to read from a file into a
I have a function that takes an input string and then runs the string
I have a function that counts the amount of visits a page has, and
I have a function that I want to execute on keyup for 2 different
I have a function that returns a comparison matrix from a given list: def
I have function that has : ob_start(); //Include of some files $content = ob_get_contents();
I have a function that uses comma as separator in breaking name/email pairs from
I have a function that accepts a file path. Users can pass in either

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.