Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #118068 > unrolled thread

Unglaublich, dass das funktioniert:

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2023-12-17 14:27 +0100
Last post2023-12-23 04:07 +0000
Articles 17 on this page of 37 — 12 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#118094

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-12-20 19:09 +0100
Message-ID<ulvakg$lgqb$1@raubtier-asyl.eternal-september.org>
In reply to#118093
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.

[toc] | [prev] | [next] | [standalone]


#118096

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-12-20 17:14 -0500
Message-ID<ulvp0j$nshb$1@dont-email.me>
In reply to#118081
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.

[toc] | [prev] | [next] | [standalone]


#118102

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-12-21 11:44 +0100
Message-ID<um14v8$11r7q$1@raubtier-asyl.eternal-september.org>
In reply to#118096
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.

[toc] | [prev] | [next] | [standalone]


#118097

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-12-20 22:56 -0800
Message-ID<um0niq$vqs2$1@dont-email.me>
In reply to#118068
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

[toc] | [prev] | [next] | [standalone]


#118099

FromRalf Goertz <me@myprovider.invalid>
Date2023-12-21 08:34 +0100
Message-ID<um0pph$100f7$1@dont-email.me>
In reply to#118097
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.

[toc] | [prev] | [next] | [standalone]


#118107

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-12-21 18:13 -0800
Message-ID<um2rck$19qhc$1@dont-email.me>
In reply to#118099
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

[toc] | [prev] | [next] | [standalone]


#118110

FromRalf Goertz <me@myprovider.invalid>
Date2023-12-22 11:12 +0100
Message-ID<um3nea$1h1gc$1@dont-email.me>
In reply to#118107
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?

[toc] | [prev] | [next] | [standalone]


#118111

FromAndreas Kempe <kempe@lysator.liu.se>
Date2023-12-22 12:02 +0000
Message-ID<um3trt$1hp7$3@nyheter.lysator.liu.se>
In reply to#118110
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.

[toc] | [prev] | [next] | [standalone]


#118112

FromAndreas Kempe <kempe@lysator.liu.se>
Date2023-12-22 12:24 +0000
Message-ID<um3v6n$1hp7$4@nyheter.lysator.liu.se>
In reply to#118111
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?

[toc] | [prev] | [next] | [standalone]


#118115

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-12-22 17:36 -0800
Message-ID<um5dih$1pfu5$2@dont-email.me>
In reply to#118112
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

[toc] | [prev] | [next] | [standalone]


#118117

FromAndreas Kempe <kempe@lysator.liu.se>
Date2023-12-23 10:15 +0000
Message-ID<um6c0c$29v5$1@nyheter.lysator.liu.se>
In reply to#118115
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.

[toc] | [prev] | [next] | [standalone]


#118114

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-12-22 17:32 -0800
Message-ID<um5dc9$1pfu5$1@dont-email.me>
In reply to#118110
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

[toc] | [prev] | [next] | [standalone]


#118123

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-12-23 21:36 +0000
Message-ID<877cl438l7.fsf@bsb.me.uk>
In reply to#118114
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.

[toc] | [prev] | [next] | [standalone]


#118127

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2023-12-24 15:53 -0500
Message-ID<MT0iN.8177$IfLe.369@fx36.iad>
In reply to#118123
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.
> 

[toc] | [prev] | [next] | [standalone]


#118129

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-12-24 22:21 +0000
Message-ID<87y1dj1bt1.fsf@bsb.me.uk>
In reply to#118127
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.

[toc] | [prev] | [next] | [standalone]


#118180

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2023-12-31 14:26 -0800
Message-ID<86jzout3es.fsf@linuxsc.com>
In reply to#118127
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."

[toc] | [prev] | [next] | [standalone]


#118116

FromKaz Kylheku <433-929-6894@kylheku.com>
Date2023-12-23 04:07 +0000
Message-ID<20231222195041.415@kylheku.com>
In reply to#118097
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.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web