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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:31:00+00:00 2026-05-13T17:31:00+00:00

How can operator bool() cause an error when declaring operator std::string in a class

  • 0

How can operator bool() cause an error when declaring operator std::string in a class and also serving as an implicit conversion to string by itself?

#include <iostream>
#include <string>
using namespace std;

class Test {
public:
    operator std::string() { cout << "op string" << endl; return "whatever";}
    operator bool() { cout << "op bool" << endl; return true;}
};

int main(int argc, char *argv[]) {
    string s;
    Test t;
    s = t;
}
  • 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-13T17:31:01+00:00Added an answer on May 13, 2026 at 5:31 pm

    The problem you are facing (besides operator std::string() returning a bool) is that implicit conversions trigger when you want and when you don’t.

    When the compiler sees s = t it identifies the following potential std::operator= matches:

    // using std::string for compactness instead of the full template
    std::string::operator=( std::string const & ); 
    std::string::operator=( char );
    

    Now, t is neither of them, so it tries to convert it to something that can fit and finds two paths: convert to bool that can be promoted to char or convert to std::string directly. The compiler cannot really decide and gives up.

    This is one of the reasons that you want to avoid providing many different conversion operators. Anything that can be implicitly called by the compiler will eventually be called when you don’t think it should.

    This article specifically deals with this problem. The suggestion is instead of providing a conversion to bool, provide a conversion to a member function

    class testable {
       typedef void (testable::*bool_type)();
       void auxiliar_function_for_true_value() {}
    public:
       operator bool_type() const {
          return condition() ? &testable::auxiliar_function_for_true_value : 0;
       }
       bool condition() const;
    };
    

    If an instance of this class is used inside a condition (if (testable())) the compiler will try and convert to bool_type that can be used in a condition.

    EDIT:

    After the comment on how the code is more complex with this solution, you can always provide it as a generic small utility. Once you provide the first part of the code, the complexity is encapsulated in the header.

    // utility header safe_bool.hpp
    class safe_bool_t;
    typedef void (safe_bool_t::*bool_type)();
    inline bool_type safe_bool(bool);
    
    class safe_bool_t {
       void auxiliar_function_for_true_value() {}
       friend bool_type safe_bool(bool);
    };
    inline bool_type safe_bool(bool)
    {
       return condition ? &safe_bool_t::auxiliar_function_for_true_value : 0;
    }
    

    Your class now becomes much more simple, and it is readable in itself (by choosing appropriate names for the functions and types):

    // each class with conversion
    class testable {
    public:
       operator bool_type() {
          return safe_bool(true);
       }
    };
    

    Only if the reader is interested in knowing how the safe_bool idiom is implemented and reads the header they fill be faced with the complexity (which can be explained in comments)

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

Sidebar

Related Questions

bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
I tried creating a class with one operator bool and one operator void* ,
In this answer I talk about using a std::ifstream object's conversion to bool to
Can the new operator throw an exception in real life? And if so, do
I know that we can't overload operator with other meaning, we can't create new
I'm wondering if you can overload an operator and use it without changing the
How can this code compile? The code below in the operator int CAN access
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
I want to derive a stringstream so that I can use the operator<< to
For associative containers, can the ++ operator send an iterator past the end of

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.