Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #118180
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Unglaublich, dass das funktioniert: |
| Date | 2023-12-31 14:26 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <86jzout3es.fsf@linuxsc.com> (permalink) |
| References | (3 earlier) <um2rck$19qhc$1@dont-email.me> <um3nea$1h1gc$1@dont-email.me> <um5dc9$1pfu5$1@dont-email.me> <877cl438l7.fsf@bsb.me.uk> <MT0iN.8177$IfLe.369@fx36.iad> |
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> writes:
> Ben Bacarisse wrote:
>
>> Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
>>
>>> On 12/22/23 2:12 AM, Ralf Goertz wrote:
>>>
>>>> Am Thu, 21 Dec 2023 18:13:39 -0800
>>>> schrieb Andrey Tarasevich <andreytarasevich@hotmail.com>:
>>>>
>>>>> On 12/20/23 11:34 PM, Ralf Goertz wrote:
>>>>>
>>>>>> On Wed, 20 Dec 2023 22:56:24 -0800
>>>>>> Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
>>>>>> >>>>>> Meh... An old example of break-less switch-case branching
>>>>>>
>>>>>>> #include <iostream>
>>>>>>>
>>>>>>> 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 <https://en.cppreference.com/w/cpp/language/statements>
>>>> 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."
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-17 14:27 +0100
Re: Unglaublich, dass das funktioniert: Bo Persson <bo@bo-persson.se> - 2023-12-17 18:11 +0100
Re: Unglaublich, dass das funktioniert: Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-12-17 14:22 -0500
Re: Unglaublich, dass das funktioniert: James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-12-18 00:01 -0500
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-18 18:35 +0100
Re: Unglaublich, dass das funktioniert: Kaz Kylheku <433-929-6894@kylheku.com> - 2023-12-19 02:37 +0000
Re: Unglaublich, dass das funktioniert: immibis <news@immibis.com> - 2023-12-19 14:40 +0100
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-19 14:47 +0100
Re: Unglaublich, dass das funktioniert: Kaz Kylheku <433-929-6894@kylheku.com> - 2023-12-19 17:27 +0000
Re: Unglaublich, dass das funktioniert: James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-12-19 12:54 -0500
Re: Unglaublich, dass das funktioniert: James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-12-19 02:04 -0500
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-19 14:48 +0100
Re: Unglaublich, dass das funktioniert: David Brown <david.brown@hesbynett.no> - 2023-12-19 19:34 +0100
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-20 09:40 +0100
Re: Unglaublich, dass das funktioniert: David Brown <david.brown@hesbynett.no> - 2023-12-20 14:59 +0100
Re: Unglaublich, dass das funktioniert: James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-12-19 11:55 -0500
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-19 18:04 +0100
Re: Unglaublich, dass das funktioniert: David Brown <david.brown@hesbynett.no> - 2023-12-20 15:04 +0100
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-20 15:19 +0100
Re: Unglaublich, dass das funktioniert: Kaz Kylheku <433-929-6894@kylheku.com> - 2023-12-20 18:01 +0000
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-20 19:09 +0100
Re: Unglaublich, dass das funktioniert: James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-12-20 17:14 -0500
Re: Unglaublich, dass das funktioniert: Bonita Montero <Bonita.Montero@gmail.com> - 2023-12-21 11:44 +0100
Re: Unglaublich, dass das funktioniert: Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-12-20 22:56 -0800
Re: Unglaublich, dass das funktioniert: Ralf Goertz <me@myprovider.invalid> - 2023-12-21 08:34 +0100
Re: Unglaublich, dass das funktioniert: Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-12-21 18:13 -0800
Re: Unglaublich, dass das funktioniert: Ralf Goertz <me@myprovider.invalid> - 2023-12-22 11:12 +0100
Re: Unglaublich, dass das funktioniert: Andreas Kempe <kempe@lysator.liu.se> - 2023-12-22 12:02 +0000
Re: Unglaublich, dass das funktioniert: Andreas Kempe <kempe@lysator.liu.se> - 2023-12-22 12:24 +0000
Re: Unglaublich, dass das funktioniert: Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-12-22 17:36 -0800
Re: Unglaublich, dass das funktioniert: Andreas Kempe <kempe@lysator.liu.se> - 2023-12-23 10:15 +0000
Re: Unglaublich, dass das funktioniert: Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-12-22 17:32 -0800
Re: Unglaublich, dass das funktioniert: Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-12-23 21:36 +0000
Re: Unglaublich, dass das funktioniert: Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-12-24 15:53 -0500
Re: Unglaublich, dass das funktioniert: Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-12-24 22:21 +0000
Re: Unglaublich, dass das funktioniert: Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-12-31 14:26 -0800
Re: Unglaublich, dass das funktioniert: Kaz Kylheku <433-929-6894@kylheku.com> - 2023-12-23 04:07 +0000
csiph-web