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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:37:25+00:00 2026-06-03T06:37:25+00:00

I have following code: typedef enum { FOO, BAR, BAZ } foo_t; static void

  • 0

I have following code:

typedef enum
{
    FOO,
    BAR,
    BAZ
} foo_t;

static void afunc(bool is_it_on)
{
    /* do the job */
}

int main(void)
{
    afunc(BAZ);
    return 0;
}

Compiling this code does not generate any warning message, even with -Wall -Wextra options given to the compiler. I have even tried with -Wconversion option, which took no effect because bool and enum seemed to be of same size for g++. (the size of enum type is not defined in specification as far as I know)

I have combed through gcc manual and found nothing about it.

Questions:

  • Is there a way to force the compiler to generate a warning in cases like this?
  • Or is it that this implicit casting is legal by c++ specification?

Compiler that I am using: gcc 4.1.2


Editted

Conclusion:

The only viable solution to this seems to define a new type to represent 0 or 1, and use it instead of bool.

The code would be like following, and g++ complains about type conversion:

typedef enum
{
    FOO1,
    FOO2
} foo_t;

typedef enum
{
    MY_FALSE,
    MY_TRUE
} my_bool_t;

void foo(my_bool_t a)
{
}

int main(void)
{
     /* 
      * gcc generates an error.
      * error: cannot convert ‘foo_t’ to ‘my_bool_t’ 
      * for argument ‘1’ to ‘void foo(my_bool_t)’
      */
    foo(FOO1);
    return 0;
}
  • 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-03T06:37:26+00:00Added an answer on June 3, 2026 at 6:37 am

    Yes, those implicit conversions are perfectly legal.

    C++11 draft n3290, §4.12 Boolean conversions:

    A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
    prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
    any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of
    type bool; the resulting value is false.

    Warnings on these conversions (for arithmetic types) would probably lead to huge numbers of warnings all over the place, I don’t think it would be manageable.

    In C++11, you can use scoped enums to prevent that implicit conversion:

    This fails to compile for lack of a conversion from Foo to bool:

    enum class Foo { ONE };
    
    void tryit(bool b) { }
    
    int main()
    {
        tryit(Foo::ONE);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C code: typedef void (*mycallback) (char buf[128]); void take_callback(mycallback cb)
I have the following block code typedef BOOL(^FieldValidationBlock)(NSString *); FieldValidationBlock aBlock = ^(NSString *input){
Let's say I have the following code: typedef std::function<void ()> func_type; void some_func() {
I have the following code in my .h/.cpp files: .h: class Foo; typedef Foo
I have in essence the following code: typedef std::function<void ()> fnGlobalChangeEvent; typedef std::vector<fnGlobalChangeEvent> GlobalTriggers;
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
I have the following code: typedef boost::variant<LandSearchParameter, WaterSearchParameter> SearchParameter; enum Visibility{ CLEAR, CLOUDY, FOG,
I have the following code: typedef void * (__stdcall * call_generic)(...); typedef void *
have the following C code: typedef void*(m3_func)(void); #define NULL ((void*)0) char* lolinfo() { return
I have following piece of code: typedef uint8_t array_t[8]; static array_t _my_array; static const

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.