Path: csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Unglaublich, dass das funktioniert:
Date: Sun, 31 Dec 2023 14:26:03 -0800
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <86jzout3es.fsf@linuxsc.com>
References: <877cl438l7.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="5ace88928e20b7842ad42526ffee8c84"; logging-data="1974666"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qXdQjQkN9yZVoEsgNXprefInCegNTm2M="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:9dxdLUb/uSA0tXsF4phIv/apPOg= sha1:MamlME0cc/AvBspTcjwly/KlAWY=
Xref: csiph.com comp.lang.c++:118180
Pavel writes:
> Ben Bacarisse wrote:
>
>> Andrey Tarasevich writes:
>>
>>> On 12/22/23 2:12 AM, Ralf Goertz wrote:
>>>
>>>> Am Thu, 21 Dec 2023 18:13:39 -0800
>>>> schrieb Andrey Tarasevich :
>>>>
>>>>> On 12/20/23 11:34 PM, Ralf Goertz wrote:
>>>>>
>>>>>> On Wed, 20 Dec 2023 22:56:24 -0800
>>>>>> Andrey Tarasevich wrote:
>>>>>> >>>>>> Meh... An old example of break-less switch-case branching
>>>>>>
>>>>>>> #include
>>>>>>>
>>>>>>> int main(int argc, char **)
>>>>>>> {
>>>>>>> switch (argc)
>>>>>>> if (0) case 1: std::cout << 1 << std::endl;
>>>>>>> else if (0) case 2: std::cout << 2 << std::endl;
>>>>>>> else if (0) case 3: std::cout << 3 << std::endl;
>>>>>>> else if (0) default: std::cout << "whatever" << std::endl;
>>>>>>> }
>>>>>>>
>>>>>>> As illustrated above, even if the body of switch is not enclosed
>>>>>>> into `{ ... }`, one can still stuff a lot into a single protracted
>>>>>>> statement, including multiple `case` labels.
>>>>>>
>>>>>> Interestingly enough, I get a warning with gcc:
>>>>>>
>>>>>> warning: statement will never be executed [-Wswitch-unreachable]
>>>>>> if (0) case 1: std::cout << 1 << std::endl;
>>>>>>
>>>>>> although of course I still see the output of ?1? when the program is
>>>>>> called without arguments.
>>>>>
>>>>> The compiler complains specifically about the `if (0)` part, not
>>>>> about the part that outputs 1.
>>>>
>>>> Hm, I obviously get that the `if (0)` condition can't ever be fulfilled,
>>>> but according to
>>>> selection statements (1,2) the statement after the condition statement
>>>> is part of the if statement. So part of the if statement may still be
>>>> executed.
>>>
>>> This has absolutely nothing to do with the fulfillability of if's
>>> condition. You can replace `if (0)` with `if (1)` and you will still get
>>> the same warning. The properties of that `if` are completely beside the
>>> point here.
>>
>> I don't find that to be the case. gcc complains about this if (0) being
>> unreachable:
>>
>> switch (argc)
>> if (0) case 1: std::cout << 1 << "\n";
>>
>> but not this one:
>>
>> switch (argc)
>> if (1) case 1: std::cout << 1 << "\n";
>
> I think "if (1)" *is* equivalent to no-op here or, in AT's terms,
> cannot "potentially generate executable instructions". I would say
> "there is no code to reach hence there is no switch-unreachable
> warning".
Some compilers complain. Other don't.
I've seen the 'if(1)' version get a warning, and not get a
warning with a different compiler.
I've seen a 'for(;0;) case 1: ...' get a warning in some
circumstances, and not in others.
I tried some other variations, and got various results
under different compilers.
I'm reminded of Abraham Lincoln's review: "For people who
like this sort of thing, this is the sort of thing that
those people like."