Deutsch   English   Français   Italiano  
<vsrj37$2jlgk$3@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: enums and switch vs function calls
Date: Sat, 5 Apr 2025 17:43:35 +0200
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <vsrj37$2jlgk$3@dont-email.me>
References: <vsr0ml$1to9m$1@dont-email.me>
 <enum-20250405122329@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 05 Apr 2025 17:43:36 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f18e61aba241dcc58e6a1ecca4ee4272";
	logging-data="2741780"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/fMmgSOTv628b9TzRLrMhvmF2G0ub9nIs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pJK/9C7gPkC1Mj32NYJi017bbUc=
In-Reply-To: <enum-20250405122329@ram.dialup.fu-berlin.de>
Content-Language: en-GB
Bytes: 1931

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).