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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:04:49+00:00 2026-06-06T19:04:49+00:00

I want to replace the bitshift bool overload for i/ostream. The current implementation only

  • 0

I want to replace the bitshift bool overload for i/ostream. The current implementation only takes in input strings of “0” or “1” and outputs only “0” or “1”. I want to make a bool overload that considers other sequences such as “t”,”true”,”f”,”false”, etc… Is there anyway to do this, even if its confined to a limited scope? This is the code I want to use:

inline std::ostream& operator << (std::ostream& os, bool b)
{
    return os << ( (b) ? "true" : "false" );
}

inline std::istream& operator >> (std::istream& is, bool& b)
{
    string s;
    is >> s;
    s = Trim(s);

    const char*  true_table[5] = { "t", "T", "true" , "True ", "1" };
    const char* false_table[5] = { "f", "F", "false", "False", "0" };

    for (uint i = 0; i < 5; ++i)
    {
        if (s == true_table[i])
        {
            b = true;
            return is;
        }
    }

    for (uint i = 0; i < 5; ++i)
    {
        if (s == false_table[i])
        {
            b = false;
            return is;
        }
    }

    is.setstate(std::ios::failbit);
    return is;
}
  • 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-06T19:04:51+00:00Added an answer on June 6, 2026 at 7:04 pm

    If you’re willing to drop t and f as possibilities you can just use std::boolalpha and std::noboolalpha

    from cppreference:

    // boolalpha output
    std::cout << std::boolalpha 
              << "boolalpha true: " << true << '\n'
              << "boolalpha false: " << false << '\n';
    std::cout << std::noboolalpha 
              << "noboolalpha true: " << true << '\n'
              << "noboolalpha false: " << false << '\n';
    // booalpha parse
    bool b1, b2;
    std::istringstream is("true false");
    is >> std::boolalpha >> b1 >> b2;
    std::cout << '\"' << is.str() << "\" parsed as " << b1 << ' ' << b2 << '\n';
    

    Output:

    boolalpha true: true
    boolalpha false: false
    noboolalpha true: 1
    noboolalpha false: 0
    "true false" parsed as 1 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to replace a regex with '*', but only if the regex is
I want to replace all symbols that aren't letters by - , but my
I have this code, and I want to know, if I can replace only
i want to replace the username in the class of current selector with the
I want to replace a word that is in html content (not attributes) and
I want to replace some HTML tags that I have in a CDATA element,
I want to replace a lines that ends with 'YES' with 'YES' and replace
I want to replace comma when its inside parentheses only. For Example Progamming languages
I want replace a(or each) element(number) instead another element(number) in a class while running
I want to replace some headers by images, so I would have nice font.For

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.