Re: enums and switch vs function calls

Liste des GroupesRevenir à cl c  
Sujet : Re: enums and switch vs function calls
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 05. Apr 2025, 16:43:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vsrj37$2jlgk$3@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 05/04/2025 13:26, Stefan Ram wrote:
Richard Harnden <richard.nospam@gmail.invalid> wrote or quoted:
int hex = colour_to_hex(10);
... then the compiler doesn't complain that 10 is not in the enum.
Why? Surely the compiler can tell.
    I get
 |
|error: enum conversion when passing argument 1 of 'colour_to_hex' is invalid in C++ [-Wc++-compat]
|      |int hex = colour_to_hex(10);
|      |                        ^~
|note: expected 'enum colour' but argument is of type 'int'
|      | int colour_to_hex(enum colour colour)
|      |     ^~~~~~~~~~~~~
|
    with
 gcc -Wc++-compat
 
Yes.  In C++, the enumeration constants are of the enumeration type, while in C they are "int".
It would be nice if gcc had a warning for this kind of thing (for those that choose to enable it).

Date Sujet#  Auteur
5 Apr 25 * enums and switch vs function calls9Richard Harnden
5 Apr 25 +* Re: enums and switch vs function calls6Stefan Ram
5 Apr 25 i+- Re: enums and switch vs function calls1David Brown
5 Apr 25 i+- Re: enums and switch vs function calls1Stefan Ram
6 Apr 25 i`* Re: enums and switch vs function calls3Tim Rentsch
6 Apr 25 i +- Re: enums and switch vs function calls1David Brown
7 Apr 25 i `- Re: enums and switch vs function calls1Tim Rentsch
5 Apr 25 `* Re: enums and switch vs function calls2Thiago Adams
6 Apr 25  `- Re: enums and switch vs function calls1David Brown

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal