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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:28:07+00:00 2026-06-01T02:28:07+00:00

I am trying to write bool-conversion operator for std::bitset I tried: template<size_t size> operator

  • 0

I am trying to write bool-conversion operator for std::bitset

I tried:

template<size_t size>
operator bool(std::bitset<size> & b)
{
    return b.any();
}

but I got

error C2801: 'mynamespace::operator bool' must be a non-static member

from my visual-studio.

But when I look up C2801 explanation it says nothing about conversion operators (only about =, ->, [],())

So, is it possible to somehow write “Conversion std::bitset to bool operator?”

(I can not call b.any() in my if-statements, because the same code must run when std::bitset is replaced with unsigned or something

typedef std::bitset<x> Bitset;
//typedef unsigned Bitset;

so the ideal syntax will be like:

Bitset b = whatewer;
if(b)
    doStuff();

)

If this overloading is not possible, what is the recommended workaround?

so far I use it like:

if(b == Bitset(0))
    doStuff(); 

but I dont like it.

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-06-01T02:28:09+00:00Added an answer on June 1, 2026 at 2:28 am

    As the error message says, the conversion operator must be a non-static member of a class. That is true.

    I can not call b.any() in my if-statements, because the same code must run when std::bitset is replaced with unsigned or something.

    If that is your problem, then you can use function overload, and call it passing the argument which will return a boolean value:

    template<typename T>
    bool to_bool(T const & b)
    {
        return b; //implicit conversion (if allowed) for all other types
    }
    
    template<size_t N>
    bool to_bool(std::bitset<N> const & b)
    {
        return b.any();
    }
    

    then use it as:

    if (to_bool(whatever)) 
    {
    }
    

    It will call the correct overload. If the type of whatever is std::bitset<N> then the second overloaded function will be called, or else the first one will be called.

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

Sidebar

Related Questions

I am trying to write a class template that provides a comparison operator between
I am trying write a function that generates simulated data but if the simulated
Trying to write out syslog entries containing strings but they don't register. // person.name
I'm trying to write my own Custom Validation attribute but I'm having some problems.
I'm trying to write a simple recursive function that look over list and return
Is there any problem with this code? bool Spellcheck::smart_comp(string value, string key){ return true;
I am trying to write an operator != function that compares if two complex
I am trying to write Boolean value to the plist file, But I really
I am trying to write operator overload for custom class and don't know how
im trying to write a uint128_t library (http://stackoverflow.com/questions/6119306/operator-overloading-c-placement) and ive hit a bump: i

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.