Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does gcc not warn when an enum or int value is passed on as a function's argument which is bool?
    primarykey
    data
    text
    <p>I have following code:</p> <pre class="lang-c prettyprint-override"><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; } </code></pre> <p>Compiling this code does not generate any warning message, even with <code>-Wall -Wextra</code> options given to the compiler. I have even tried with <code>-Wconversion</code> option, which took no effect because <code>bool</code> and <code>enum</code> seemed to be of same size for g++. (the size of <code>enum</code> type is not defined in specification as far as I know)</p> <p>I have combed through gcc manual and found nothing about it.</p> <p>Questions:</p> <ul> <li>Is there a way to force the compiler to generate a warning in cases like this?</li> <li>Or is it that this implicit casting is legal by c++ specification?</li> </ul> <p>Compiler that I am using: gcc 4.1.2</p> <hr> <p><strong>Editted</strong></p> <p>Conclusion:</p> <p>The only viable solution to this seems to define a new type to represent 0 or 1, and use it instead of <code>bool</code>.</p> <p>The code would be like following, and g++ complains about type conversion:</p> <pre class="lang-c prettyprint-override"><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload