• Re: Unglaublich, dass das funktioniert:

    From Bo Persson@21:1/5 to Bonita Montero on Sun Dec 17 18:11:47 2023
    On 2023-12-17 at 14:27, Bonita Montero wrote:
    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pavel@21:1/5 to Bonita Montero on Sun Dec 17 14:22:42 2023
    Bonita Montero wrote:
    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.
    Det er dårlig fordi det er uvanlig.Det er dårlig fordi det er uvanlig.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Bonita Montero on Mon Dec 18 00:01:42 2023
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Tue Dec 19 02:37:59 2023
    On 2023-12-18, Bonita Montero <Bonita.Montero@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.

    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: @Kazinator@mstdn.ca
    NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Bonita Montero on Tue Dec 19 02:04:19 2023
    On 2023-12-18, Bonita Montero <Bonita.Montero@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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From immibis@21:1/5 to Kaz Kylheku on Tue Dec 19 14:40:27 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Bonita Moreno on Tue Dec 19 11:55:59 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Bonita Montero on Tue Dec 19 12:54:54 2023
    On 2023-12-19, Bonita Montero <Bonita.Montero@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).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Bonita Montero on Tue Dec 19 17:27:52 2023
    On 2023-12-19, Bonita Montero <Bonita.Montero@gmail.com> wrote:
    Am 19.12.2023 um 03:37 schrieb Kaz Kylheku:
    On 2023-12-18, Bonita Montero <Bonita.Montero@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?

    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*?

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Tue Dec 19 19:34:41 2023
    On 19/12/2023 14:48, Bonita Montero 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.


    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Wed Dec 20 15:04:48 2023
    On 19/12/2023 18:04, Bonita Montero wrote:
    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Bonita Montero on Wed Dec 20 14:59:18 2023
    On 20/12/2023 09:40, Bonita Montero wrote:
    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to David Brown on Wed Dec 20 18:01:47 2023
    On 2023-12-20, David Brown <david.brown@hesbynett.no> wrote:
    On 19/12/2023 18:04, Bonita Montero wrote:
    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.

    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.

    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).

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Bonita Moreno on Wed Dec 20 17:14:42 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Bonita Montero on Wed Dec 20 22:56:24 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Goertz@21:1/5 to Andrey Tarasevich on Thu Dec 21 08:34:09 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Ralf Goertz on Thu Dec 21 18:13:39 2023
    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. The `if (0)` part precedes the first `case`
    label and for that reason it is indeed unreachable.

    --
    Best regards,
    Andrey

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Goertz@21:1/5 to All on Fri Dec 22 11:12:26 2023
    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. 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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Kempe@21:1/5 to All on Fri Dec 22 12:02:05 2023
    Den 2023-12-22 skrev Ralf Goertz <me@myprovider.invalid>:
    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. 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?


    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Kempe@21:1/5 to All on Fri Dec 22 12:24:55 2023
    Den 2023-12-22 skrev Andreas Kempe <kempe@lysator.liu.se>:
    Den 2023-12-22 skrev Ralf Goertz <me@myprovider.invalid>:
    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. 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?


    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.

    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Andreas Kempe on Fri Dec 22 17:36:15 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrey Tarasevich@21:1/5 to Ralf Goertz on Fri Dec 22 17:32:55 2023
    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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Andrey Tarasevich on Sat Dec 23 04:07:56 2023
    On 2023-12-21, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
    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.

    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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Kempe@21:1/5 to All on Sat Dec 23 10:15:40 2023
    Den 2023-12-23 skrev Andrey Tarasevich <andreytarasevich@hotmail.com>:
    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. [...]


    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Andrey Tarasevich on Sat Dec 23 21:36:04 2023
    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";

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pavel@21:1/5 to Ben Bacarisse on Sun Dec 24 15:53:57 2023
    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".

    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.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Pavel on Sun Dec 24 22:21:46 2023
    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".

    Sounds reasonable.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Pavel on Sun Dec 31 14:26:03 2023
    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."

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)