Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unglaublich, dass das funktioniert:

214 views
Skip to first unread message

Bonita Montero

unread,
Dec 17, 2023, 8:28:05 AM12/17/23
to
int main( int argc, char ** )
{
switch( argc )
{
for( ; ; )
{
case 1:;
}
}
}

Hab ich gerade in einem alten C-Programm von 1999 gesehen, dass
das jemand gemacht hat. Für manche mag das schlecht wirken weil
es unüblich ist.

Bo Persson

unread,
Dec 17, 2023, 12:12:06 PM12/17/23
to
It is similar to Duff's device from the 1980's

https://en.wikipedia.org/wiki/Duff%27s_device

A case label must be inside a switch, but it doesn't have to be
*directly* inside.




Pavel

unread,
Dec 17, 2023, 2:23:02 PM12/17/23
to
Det er dårlig fordi det er uvanlig.Det er dårlig fordi det er uvanlig.

James Kuyper

unread,
Dec 18, 2023, 12:01:55 AM12/18/23
to
On 2023-12-17 at 14:27, Bonita Montero wrote:
> int main( int argc, char ** )
> {
>     switch( argc )
>     {
>         for( ; ; )
>         {
>         case 1:;
>         }
>     }
> }

Why do you consider it "Unbelievable that that works"? Of course, you
first have to specify what you mean by "works". If argc is zero (which
is permitted), the program does nothing and exits immediately with a
successful exit status. If argc is non-zero, it enters an infinite loop
that does nothing - an implementation is permitted to implement such a
loop as if it did in fact terminate with a successful exit status.
Either way, nothing observable happens other than the exit status, which
means that an implementation is free to implement this as the equivalent
of "int main(void) {}".
Is that what's intended? If so, it does works.
Is it the unconventional location of the case label that you don't
understand? That location is permitted, and has well-defined
behavior.understand? That location is permitted, and has well-defined
behavior.

translate.google.com (apologies for any mistranslation - my own German
is not any better than Google's):

Warum halten Sie es als "unglaublich, dass das funktioniert"? Natürlich
müssen Sie zuerst angeben, was Sie mit "Werken" meinen. Wenn argc Null
ist (was zulässig ist), tut das Programm nichts und verlässt sofort
einen erfolgreichen Ausstiegsstatus. Wenn argc ungleich Null ist, tritt
eine unendliche Schleife ein, die nichts tut - eine Implementierung darf
eine solche Schleife implementieren, als ob sie tatsächlich einen
erfolgreichen Ausstiegsstatus beendet hätte. In beiden Fällen passiert
nichts anderes als den Exit -Status, was bedeutet, dass eine
Implementierung frei ist, dies als das Äquivalent von "int main (void)
{}" zu implementieren.
Ist es das, was beabsichtigt ist? Wenn ja, funktioniert es.
Ist es der unkonventionelle Ort des Falletiketts, den Sie nicht
verstehen? Dieser Ort ist erlaubt und hat ein gut definiertes Verhalten.

One aspect of the translation that I did fiddle with: Google translated
"Unglaublich, dass das funktioniert" as "Unbelievable that this works",
but during the reverse translation it used "dies" rather than "das". If
I used "Unbelievable that that works", it reproduced the original
Subject line.
As a native English speaker, "that this works" feels more appropriate to
me. Is "das" more appropriate than "dies" in German?

Bonita Montero

unread,
Dec 18, 2023, 12:35:42 PM12/18/23
to
Am 18.12.2023 um 06:01 schrieb James Kuyper:

> Why do you consider it "Unbelievable that that works"? Of course, you
> first have to specify what you mean by "works". If argc is zero (which
> is permitted), the program does nothing and exits immediately with a
> successful exit status. If argc is non-zero, it enters an infinite loop
> that does nothing - an implementation is permitted to implement such a
> loop as if it did in fact terminate with a successful exit status.
> Either way, nothing observable happens other than the exit status, which
> means that an implementation is free to implement this as the equivalent
> of "int main(void) {}".

I didn't mean that the code does sth. which makes sense but that the
code has a case inside a for inside a switch. Others understood that
better than you.

Kaz Kylheku

unread,
Dec 18, 2023, 9:38:12 PM12/18/23
to
A case/default statement belongs to the closest enclosing switch, no
matter in how many other statements it is enclosed.

This has its uses, such as:

switch (state) {
case FOO:
// FOO-only prologue code here, skipped by BAR case.

if (false) {
case BAR:
// BAR-specific prologue code, skipped by FOO case.
}
// fallthrough
case XYZZY:
// code common to FOO, BAR, XYZZY
...
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazi...@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

James Kuyper

unread,
Dec 19, 2023, 2:04:36 AM12/19/23
to
On 2023-12-18, Bonita Montero <Bonita....@gmail.com> wrote:
No, I understood that. I was merely pointing out your poor choice of
words. "funktionert" is the past participle of "funktionen", for which
the German language version of Wiktionary provides the definition
"vorschriftsmäßig arbeiten", which translates as "work in accordance
with". That makes it a good match for the 16th definition provided by
Wiktionary for the English word "work" as a verb: "To function
correctly; to act as intended; to achieve the goal designed for."

I gather that you weren't surprised by the fact that it worked in
accordance with the relevant requirements, which is what your chosen
Subject: header implies. You were actually surprised by the fact that
there was any required behavior defined for such code by the standard. I
would guess that's because you were unaware of the fact that a case
label can be placed anywhere within the body of a switch statement that
an ordinary label can be placed.

immibis

unread,
Dec 19, 2023, 8:40:45 AM12/19/23
to
On 12/19/23 03:37, Kaz Kylheku wrote:
> This has its uses, such as:
>
> switch (state) {
> case FOO:
> // FOO-only prologue code here, skipped by BAR case.
>
> if (false) {
> case BAR:
> // BAR-specific prologue code, skipped by FOO case.
> }
> // fallthrough
> case XYZZY:
> // code common to FOO, BAR, XYZZY
> ...
> }
>

Note: this case is better served by a goto, or a function call, or just
writing if(state==FOO) {/*FOO code*/} else if(state==BAR) {/*BAR code*/}
.....

Some people are allergic to goto and find this switch syntax to be
preferable. They're idiots.

Bonita Montero

unread,
Dec 19, 2023, 8:47:37 AM12/19/23
to
Am 19.12.2023 um 03:37 schrieb Kaz Kylheku:
> On 2023-12-18, Bonita Montero <Bonita....@gmail.com> wrote:
>> Am 18.12.2023 um 06:01 schrieb James Kuyper:
>>
>>> Why do you consider it "Unbelievable that that works"? Of course, you
>>> first have to specify what you mean by "works". If argc is zero (which
>>> is permitted), the program does nothing and exits immediately with a
>>> successful exit status. If argc is non-zero, it enters an infinite loop
>>> that does nothing - an implementation is permitted to implement such a
>>> loop as if it did in fact terminate with a successful exit status.
>>> Either way, nothing observable happens other than the exit status, which
>>> means that an implementation is free to implement this as the equivalent
>>> of "int main(void) {}".
>>
>> I didn't mean that the code does sth. which makes sense but that the
>> code has a case inside a for inside a switch. Others understood that
>> better than you.
>
> A case/default statement belongs to the closest enclosing switch, no
> matter in how many other statements it is enclosed.
> ...

Do you think it occurred to me with the code I saw that it was also
legal code?


Bonita Montero

unread,
Dec 19, 2023, 8:48:43 AM12/19/23
to
Am 19.12.2023 um 08:04 schrieb James Kuyper:

> No, I understood that. I was merely pointing out your poor choice of
> words. "funktionert" is the past participle of "funktionen", for which
> the German language version of Wiktionary provides the definition
> "vorschriftsmäßig arbeiten", ...

Idiot.
The wording was correct in German and you're not German.


James Kuyper

unread,
Dec 19, 2023, 11:56:13 AM12/19/23
to
On 12/19/23 08:48, Bonita Moreno wrote:
> Am 19.12.2023 um 08:04 schrieb James Kuyper:
>
>> No, I understood that. I was merely pointing out your poor choice of
>> words. "funktionert" is the past participle of "funktionen", for which
>> the German language version of Wiktionary provides the definition
>> "vorschriftsmäßig arbeiten", ...
>
> Idiot.
> The wording was correct in German and you're not German.

Actually, I am 1/4 German. More relevantly, I studied German in school
for a couple of years a few decades ago. I claim no great amount of
fluency - I rely on translate.google.com, though I feel competent to
review it's translations. I would ordinarily very happily defer to the
expertise of a native speaker of German, or even a non-native speaker
who's had more years of study than I have, or more recently. However,
you are so routinely wrong about so many things that I would not trust
you to use German correctly even if you are a native speaker.

However, if you can cite a respectable online dictionary's definition
that supports your use of "functionert" to mean something like "can be
compiled" or "is syntactically correct", please do so.

Bonita Montero

unread,
Dec 19, 2023, 12:04:43 PM12/19/23
to
Am 19.12.2023 um 17:55 schrieb James Kuyper:

> Actually, I am 1/4 German. More relevantly, I studied German in school
> for a couple of years a few decades ago. I claim no great amount of
> fluency - I rely on translate.google.com, though I feel competent to
> review it's translations. I would ordinarily very happily defer to the
> expertise of a native speaker of German, or even a non-native speaker
> who's had more years of study than I have, or more recently. However,
> you are so routinely wrong about so many things that I would not trust
> you to use German correctly even if you are a native speaker.
>
> However, if you can cite a respectable online dictionary's definition
> that supports your use of "functionert" to mean something like "can be
> compiled" or "is syntactically correct", please do so.

When you say that sth. "funktioniert" it just means that it works.
It's unbelievable that you deny that compared to a native speaker.

Kaz Kylheku

unread,
Dec 19, 2023, 12:28:05 PM12/19/23
to
The way the case/default statements are "unhinged" (to some extent) is
likely surprising to most coders when they learn about it.

Wow, you can do *what*?

James Kuyper

unread,
Dec 19, 2023, 12:55:07 PM12/19/23
to
On 2023-12-19, Bonita Montero <Bonita....@gmail.com> wrote:
> Am 19.12.2023 um 03:37 schrieb Kaz Kylheku:
...
>> A case/default statement belongs to the closest enclosing switch, no
>> matter in how many other statements it is enclosed.
>> ...
>
> Do you think it occurred to me with the code I saw that it was also
> legal code?

Probably not until after you thought about it a while, otherwise why
would you say "Unglaublich" (unbelievable)? If you were sufficiently
familiar with Duff's device, it should have been completely "glaubhaft"
(believable).

David Brown

unread,
Dec 19, 2023, 1:34:54 PM12/19/23
to
Do you think only Germans can understand or write correctly in German?
And do you think the /wording/ was the issue?

Given how often you write English that has correct wording, but
nonsensical or incorrect content, I'd assume you do the same in German.
And I'd assume that James was correct and you were wrong, regardless of
the language - that's just the statistics for posts in this group.


Bonita Montero

unread,
Dec 20, 2023, 3:40:40 AM12/20/23
to
Am 19.12.2023 um 19:34 schrieb David Brown:
'
> Do you think only Germans can understand or write correctly in German?

I can, you can't.

David Brown

unread,
Dec 20, 2023, 8:59:35 AM12/20/23
to
I know /I/ can't write German - it is a very long time since I studied
it at school. But that's not the point, as I never suggested I knew any
German at all. As usual, you are quite good at writing correct English,
but very bad at reading it.

David Brown

unread,
Dec 20, 2023, 9:05:01 AM12/20/23
to
It is not your use of language that is at issue - it is your
understanding of the concept "work". You would have provoked the same
reaction if you had written "It is unbelievable that this worked" in
English.

I would like to be able to say that it is unbelievable that you are
still missing the point, but unfortunately that is not the case.

Bonita Montero

unread,
Dec 20, 2023, 9:19:53 AM12/20/23
to
Am 20.12.2023 um 15:04 schrieb David Brown:

> It is not your use of language that is at issue - it is your
> understanding of the concept "work".  You would have provoked
> the same reaction if you had written "It is unbelievable that
> this worked" in English.

James' initial objection was that this wasn't correct German.
And for sure only nerds are provoked by that.

Kaz Kylheku

unread,
Dec 20, 2023, 1:02:00 PM12/20/23
to
It's pretty clear to me. The program basically does one of two things
based on the value of argc. So without any description of what "worked"
refers to, the correct debate-level assumption to make is that "worked"
refers to those obvious behaviors that the program has: "I can't believe
that a program executable was produced, and actually has the behavior
that the source code ostensibly seems to be calling for." Which can
only mean, I can't believe that such a combination of constructs is
allowed (if not by the standard, then at least my compiler).

Bonita Montero

unread,
Dec 20, 2023, 1:09:33 PM12/20/23
to
Am 20.12.2023 um 19:01 schrieb Kaz Kylheku:

> It's pretty clear to me. The program basically does one of two things
> based on the value of argc. So without any description of what "worked"
> refers to, the correct debate-level assumption to make is that "worked"
> refers to those obvious behaviors that the program has: "I can't believe
> that a program executable was produced, and actually has the behavior
> that the source code ostensibly seems to be calling for." ...

Yes, it just means that I was surprised that the code compiled. James
was blind for that and thought that I was referring to what the code
does, and not how.

James Kuyper

unread,
Dec 20, 2023, 5:14:57 PM12/20/23
to
On 2023-12-19 at 12:04, Bonita Moreno wrote:
> Am 19.12.2023 um 17:55 schrieb James Kuyper:
,,,
>> However, if you can cite a respectable online dictionary's definition
>> that supports your use of "functionert" to mean something like "can be
>> compiled" or "is syntactically correct", please do so.
>
> When you say that sth. "funktioniert" it just means that it works.
> It's unbelievable that you deny that compared to a native speaker.

I don't deny it. I very explicitly endorsed translating "functioniert"
with the word "works" - but of the 22 definitions that Wiktionary
provides for the English verb "work", only the 16th is a good match to
the definition of "functionieren": "To function correctly; to act as
intended; to achieve the goal designed for.". Precisely because it is a
good match, it doesn't fit what you were trying to say. You didn't
consider it unbelievable that this program functioned correctly, or that
it acted as intended, or that it achieved the goal it was designed for.
What you seem to have found unbelievable is that it had any defined
behavior at all, rather than being a syntax error.
I'm not surprised by that - most people are surprised when they first
encounter switch() being used in this fashion - but it's not hard to
verify that the standard says nothing negative about such use.

Andrey Tarasevich

unread,
Dec 21, 2023, 1:56:38 AM12/21/23
to
On 12/17/23 5:27 AM, Bonita Montero wrote:
> int main( int argc, char ** )
> {
>     switch( argc )
>     {
>         for( ; ; )
>         {
>         case 1:;
>         }
>     }
> }
>

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.

--
Best regards,
Andrey

Ralf Goertz

unread,
Dec 21, 2023, 2:34:21 AM12/21/23
to
On Wed, 20 Dec 2023 22:56:24 -0800
Andrey Tarasevich <andreyta...@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.

Bonita Montero

unread,
Dec 21, 2023, 5:45:09 AM12/21/23
to
Am 20.12.2023 um 23:14 schrieb James Kuyper:

> I don't deny it. I very explicitly endorsed translating "functioniert"
> with the word "works" - but of the 22 definitions that Wiktionary
> provides for the English verb "work", only the 16th is a good match ***

OMG, you're silly. Every german uses that wording and it fits.


Andrey Tarasevich

unread,
Dec 21, 2023, 9:13:54 PM12/21/23
to
The compiler complains specifically about the `if (0)` part, not about
the part that outputs 1. The `if (0)` part precedes the first `case`
label and for that reason it is indeed unreachable.

--
Best regards,
Andrey

Ralf Goertz

unread,
Dec 22, 2023, 5:12:38 AM12/22/23
to
Am Thu, 21 Dec 2023 18:13:39 -0800
schrieb Andrey Tarasevich <andreyta...@hotmail.com>:

> On 12/20/23 11:34 PM, Ralf Goertz wrote:
>> On Wed, 20 Dec 2023 22:56:24 -0800
>> Andrey Tarasevich <andreyta...@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. Furthermore, it says -Wswitch-unreachable. That is misleading
imho since it is exactly the switch case label that is reachable.

> The `if (0)` part precedes the first `case` label and for that reason
> it is indeed unreachable.

I understand that but gcc knows that labeled part might be executed
despite the unfulfilled condition. So it should refrain from warning.
And why are the other `if (0) case x:` statements not complained about?
They live in an else branch of a guaranteed false condition, so they are
not unreachable and that branch will be executed. So gcc should
warn about them, too. No?

Andreas Kempe

unread,
Dec 22, 2023, 7:02:18 AM12/22/23
to
I don't know if we have any specific language for having the compiler
warn about the test in the if-statement being unreachable while the
true branch is. Considering this is such a weird edge case, I don't
know if we need it.

As for the statements not warned about, they aren't warned about
because the if (0) tests are reachable via fallthrough from the
previous case labels. The test is reachable via fallthrough while
the branch is reachable via switching to the case labels so all that
code is reachable.

Andreas Kempe

unread,
Dec 22, 2023, 7:25:08 AM12/22/23
to
After thinking some more, I relise I don't know if my fallthrough idea
holds water considering the else if construct. The first statement is
false so the second statement should be tested, but we're jumping into
the true branch, which if taken should prevent the other test from
happening. Does anyone have pointers to relevant parts of the standard
that handle this weirdness?

Andrey Tarasevich

unread,
Dec 22, 2023, 8:33:15 PM12/22/23
to
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.

What matters is that the compiler sees some tangible code (i.e. not a
no-op declaration, but something that can potentially generate
executable instructions), which precedes the very first `case` label.
That code is unreachable for obvious reasons, since `switch` will always
jump over it. Hence the warning.

--
Best regards,
Andrey

Andrey Tarasevich

unread,
Dec 22, 2023, 8:36:28 PM12/22/23
to
On 12/22/23 4:24 AM, Andreas Kempe wrote:
>
> After thinking some more, I relise I don't know if my fallthrough idea
> holds water considering the else if construct. The first statement is
> false so the second statement should be tested, but we're jumping into
> the true branch, which if taken should prevent the other test from
> happening. Does anyone have pointers to relevant parts of the standard
> that handle this weirdness?

Yes, the standard states it explicitly right at the beginning of "8.5.2
The if statement [stmt.if]" (https://timsong-cpp.github.io/cppwp/stmt.if#1)

1 [...] If the first substatement is reached via a label, the condition
is not evaluated and the second substatement is not executed. [...]

--
Best regards,
Andrey


Kaz Kylheku

unread,
Dec 22, 2023, 11:08:08 PM12/22/23
to
Why would you ever do this instead of just break?

It has the advantage that the protractive statement can be built up
catenatively, via macros, and no balancing closing macro is required.

Like with this this translation scheme. Macros on the left, generated
code right:

CASE(x) -> switch (x) if (0) { }
OF(1) -> else if (0) case (1):
{ ... code ...} -> { ... code ... }
OF(2) -> else if (0) case (1):
{ ... code ...} -> { ... code ... }
OTHERWISE -> else if (0) default:
{ ... code ...} -> { ... code ... }

(The programmer has to be careful not to put a stray else statement
after this, due to the hidden if.)

If we wanted to use break, we'd need to open a compound statement after
the switch, which would have to be closed by a pairing closing macro.

Andreas Kempe

unread,
Dec 23, 2023, 5:15:52 AM12/23/23
to
Thank you. It makes sense that GCC warns, but the wording in the
standard makes me think that all the selection parts of the if
statements should be considered unreachable since the code will always
reach the substatements through labels and never execute any of the
selection statements.

Ben Bacarisse

unread,
Dec 23, 2023, 4:36:15 PM12/23/23
to
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";

> What matters is that the compiler sees some tangible code (i.e. not a no-op
> declaration, but something that can potentially generate executable
> instructions), which precedes the very first `case` label. That code is
> unreachable for obvious reasons, since `switch` will always jump over
> it. Hence the warning.

I suspect something more subtle is going on.

--
Ben.

Pavel

unread,
Dec 24, 2023, 3:54:16 PM12/24/23
to
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".

Ben Bacarisse

unread,
Dec 24, 2023, 5:21:58 PM12/24/23
to
Sounds reasonable.

--
Ben.

Tim Rentsch

unread,
Dec 31, 2023, 5:26:16 PM12/31/23
to
Pavel <pauldont...@removeyourself.dontspam.yahoo> writes:

> Ben Bacarisse wrote:
>
>> Andrey Tarasevich <andreyta...@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 <andreyta...@hotmail.com>:
>>>>
>>>>> On 12/20/23 11:34 PM, Ralf Goertz wrote:
>>>>>
>>>>>> On Wed, 20 Dec 2023 22:56:24 -0800
>>>>>> Andrey Tarasevich <andreyta...@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."
0 new messages