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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:03:44+00:00 2026-06-08T04:03:44+00:00

I have this problem in C++: can I typedef a bitfield whose values come

  • 0

I have this problem in C++: can I typedef a bitfield whose values come from an enum?

Code will be more explainatory:

typedef {
 AUDIO    = 0x01,
 VIDEO    = 0x02,
 SUBTITLE = 0x04,
 DATA     = 0x08,
 GUARD,
 ALL      = 0xFF
} my_enum_e;

// I'd like to replace 'unsigned int' by 'my_enum_e' or similar
int myFunction( unsigned int mask ) 
{
  // code
}

// called like this:
myFunction( AUDIO|VIDEO|DATA );

In the prototype of the function, I’d like to use my_enum_e as an input value type, so that when exploring the code, you can immediately know which values you’re supposed to put in there.

Now, changing the prototype to

int myFunction( my_enum_e mask );

makes the compiler whine about a cast error. I cant fix it by casting the function calls like this:

int myFunction( my_enum_e mask )
{
    // code
}

myFunction( (my_enum_e)(VIDEO|AUDIO|DATA) );

But I find this quite horrible, and I’m not even sure it is legal (could it truncate the value??).
Do you have a solution?

  • 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-08T04:03:46+00:00Added an answer on June 8, 2026 at 4:03 am

    Add an explicit overload for | and possibly other operators.

    my_enum_e operator|(my_enum_e a, my_enum_e b) 
        { return my_enum_e(unsigned(a)|unsigned(b)); }
    

    One can write a macro that defines all needed operators for a given bitmask type.

    #define BITMASK_OPERATORS(T)   T operator|(T a, T b) { return T(unsigned(a)|unsigned(b)); } \
                                     T operator^(T a, T b) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code and can't figure out what seems to be the problem:
I have the following problem with this segment of code. Can some one please
I have this problem that I can ref a button for control it. I
I have had this problem crop up a few times and I can't figure
I have worked on this problem for my entire day and can't solve it.
I have a problem that I can't resolve. Let's say I have this map
I'm quite new to hibernate and have stumbled on this problem, which I can't
I can't fix this problem on my listview template: i have the error as
I hope some of you guys can help me with this problem.... I have
Dear everybody who can help, I have this PHP > MongoDB problem, I want

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.