C++ Logo

std-proposals

Advanced search

Re: [std-proposals] P2809R0 Infinite loops

From: Chris Ryan <chrisr98008_at_[hidden]>
Date: Tue, 4 Apr 2023 12:57:36 -0700
I understand what you are asking for. It is a very common embedded idiom
(from a past life).
An infinite loop might be undesirable to some but it is not clear to me
that an infinite loop is actually UB.
Or were you saying falling off the end of a noreturn function?
I could see that being UB (undefined function return cleanup) but not the
hypothetical noreturn loop?

Why not just use what we already have?
Use a [[noreturn]] on a single statement function.
Won't this do just exactly what you are asking for:

//const bool ever = true;
const bool forever = true;

[[noreturn]] void Run()
{
    while(forever) // or while(true) or for(;ever;)
    {
        //...
    }
}

void Init(){}

int main()
{
Init();
    Run();
}

Others: [[noreturn]] Does not preclude a throw. [dcl.attr.noreturn]
But a return does warn or anything that lets it hit the end of the function
(implied return), like a break.


Chris++;


On Tue, Apr 4, 2023 at 8: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.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-04-04 19:57:49