Sujet : Re: enums and switch vs function calls
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 06. Apr 2025, 16:16:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vsu5sa$19k7e$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 06/04/2025 16:47, Scott Lurndal wrote:
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
scott@slp53.sl.home (Scott Lurndal) writes:
>
ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
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
>
That example exemplifies the seriously screwed up error reporting in the
gnu compiler collection.
>
One line of informative error message followed by 5 lines of useless
cruft to wade through. And no way to turn it off completely.
>
That's true, but it can be reduced to only two lines total (so only one
line more than needed), by using -fno-diagnostics-show-caret.
Yes, I use that. I still find that the "note:" lines clutter the
output and provide zero benefit.
Sometimes you can immediately spot the cause of an error - you've made a typo and all you need is a vague indication that there is a mistake somewhere in that area. Other times the cause can be quite subtle, especially in C++, and the extra information can be of great help. Unfortunately, it's impossible for the compiler to tell which situation you are in.
I'm sure you are aware of the "-fdiagnostic" options for fine-tuning the appearance of error messages - I have no idea if they can be used to trim them down to something that suits you.