C++ Logo

std-proposals

Advanced search

Re: [std-proposals] P2809R0 Infinite loops

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 4 Apr 2023 22:07:44 +0200
wt., 4 kwi 2023 o 22:01 Alejandro Colomar <alx.manpages_at_[hidden]> napisaƂ(a):
>
> Hi Marcin,
>
> On 4/4/23 17:58, Marcin Jaczewski via Std-Proposals 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) {}
> > ```
>
> Already implemented in C23:
>
> for (;;) {
> // ...
> }
>
> unreachable();
>
> <https://thephd.dev/ever-closer-c23-improvements#unreachable-macro-for-optimization-and-code-improvement>
>

Ok, this could work too, probably some wording updates as C++ have too
`std::unreachable()` now.


> Cheers,
> Alex
>
> >
> > This would have benefits that compilers could more aggressively warn
> > if they see a loop that can't break and therefore create UB.

Received on 2023-04-04 20:07:56