Deutsch English Français Italiano |
<vsrjlk$2krsr$1@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: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: enums and switch vs function calls Date: Sat, 5 Apr 2025 12:53:24 -0300 Organization: A noiseless patient Spider Lines: 44 Message-ID: <vsrjlk$2krsr$1@dont-email.me> References: <vsr0ml$1to9m$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 05 Apr 2025 17:53:24 +0200 (CEST) Injection-Info: dont-email.me; posting-host="fa38cb642c73775963e3f485413cb502"; logging-data="2781083"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XGUiqj5XGuNVcvUFdaqkZgVgMvlEC4Cc=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:5aeyZLfBf973a6Y6TNDL+SlDtQw= In-Reply-To: <vsr0ml$1to9m$1@dont-email.me> Content-Language: en-GB Bytes: 2025 Em 4/5/2025 7:29 AM, Richard Harnden escreveu: > If I have: > > enum colour {RED, GREEN, BLUE} > > int colour_to_hex(enum colour colour) > { > switch (colour) > { > RED: return 0xff0000; > GREEN: return 0x00ff00; > // BLUE: return 0x0000ff; > } > > ... > } > > ... then the compiler will warn me that I've missed a case in the switch > statement. Which is good and very helpful. > > But if I do: > > 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. Sometimes enums are used as bit set. f(BIT1 | BIT2) this situation would generate a lot of warning because BIT1|BIT2 is not part of the enum, just BIT1 and BIT2 separately. One way that this could be fixed is adding an attribute [[bitset]] enum E{ BIT1 = 2, BIT2 = 4}; Then the compiler would not generate a warning in this case.