C++ Logo

std-proposals

Advanced search

Re: [std-proposals] P2809R0 Infinite loops

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Wed, 5 Apr 2023 00:06:04 +0200
 u

wt., 4 kwi 2023 o 23:32 Jason McKesson via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On Tue, Apr 4, 2023 at 3:57 PM Marcin Jaczewski via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > wt., 4 kwi 2023 o 18:12 Barry Revzin <barry.revzin_at_[hidden]> napisał(a):
> > >
> > >
> > >
> > > On Tue, Apr 4, 2023, 10:59 AM Marcin Jaczewski via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > >>
> > >> I wonder why not make infinite loops simply explicit?
> > >> Like:
> > >>
> > >> ```
> > >> while (std::infiniti) { }
> > >> ```
> > >>
> > >> Now it will be clear to the reader and compiler that we expect it to
> > >> be infinite.
> > >> Reading `std::infiniti` would be considered an observable behavior or something
> > >> like that.
> > >>
> > >> Alternative add attribute to signal to everyone that this loop should not break.
> > >> We could even reuse attribute:
> > >>
> > >> ```
> > >> [[noreturn]] while(true) {}
> > >> ```
> > >>
> > >> This would have benefits that compilers could more aggressively warn
> > >> if they see a loop that can't break and therefore create UB.
> > >
> > >
> > > Because the goal is to make all the existing infinite loops non-UB, rather than add some new construct that people would have to go and update all their [not actually broken] code to.
> > >
> > > Barry
> >
> > It's hard to say that is "not broken" as it breaks a contract with the
> > compiler, of course we can argue who is right there.
> > Another thing, how do we know if the given loop was not an error? If
> > we try to guess what the intention was then we can guess wrongly,
> > better would be to allow the user to mark his intention to the
> > compiler and other programmers.
>
> The proposal in question only makes it not-UB for very specific cases:
> the case of a condition which is a constant expression that converts
> to the boolean `true`. I suggest that it's *very* difficult to write
> that by accident. Not impossible, but hardly something people do every
> day.

I mean error in case when I write:
```
while (ture)
{
   //...
   // lot of code
   // ...
   if (cond) break;
}
```
Now my intention was that it should always terminate at some point.
If we assume that all loops should break, then if is a chance for
compiler to determine
that `cond` is never true, it can signal that I did something wrong.

If we assume that this loop is allowed to be infinite, then `cond ==
false` could be my intention.
The compiler should keep his hands away from this function and do not
warn as the function
does exactly what it "suppose" to do.

Another question is how often `while (ture)` is used for true infinite
loops and when for some calculations
that are simply hard to determine the condition for end? Becasuewe
could have pessimization as
compilers need to leave some loops that do not calculate anything
(other effects could be removed by compiler)
only because the programmer could intend to have an infinite loop.


> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-04-04 22:06:15