Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: "C++ on the Move" by Darryl K. Taft Date: Wed, 08 Mar 2023 14:29:11 -0800 Organization: None to speak of Lines: 51 Message-ID: <87cz5iri6g.fsf@nosuchdomain.example.com> References: <87pm9jqgeh.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="2b019f001728a998ba6c8fb5547b4de1"; logging-data="1177374"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UHYzlnlEkuLK7Id3/GyuV" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:o1zCZAe+LEs+av5K4Wgo0a4wkxI= sha1:tdtoJF93hV/rFjbXvfxM3a1y6o8= Xref: csiph.com comp.lang.c++:89552 "Alf P. Steinbach" writes: > On 2023-03-08 8:10 PM, James Kuyper wrote: >> On 3/8/23 13:20, Alf P. Steinbach wrote: >>> On 2023-03-08 6:52 PM, Keith Thompson wrote: >> ... >>>> I'd be interested in seeing some cases of undefined behavior that could >>>> reasonably be detected at compile time or at run time. I'm sure there >>>> are some, but I'd be surprised it were as many as half. >>>> >>>> Just one example: Signed integer overflow has undefined behavior, and >>>> defining it would require runtime checks. >>> >>> I guess you meant to write, "detecting it", not "defining it". >> No, he meant "defining it". If they were to choose to define the >> behavior of signed integer overflow, then on platforms where the defined >> behavior was not the same as that which would naturally occur, run-time >> checks would be required to change the behavior. > > Can you give a concrete (even if hypothetical) example of what you mean? For example, a future C++ standard might specify that signed integer overflow throws a language-defined exception, or it might require saturation (so INT_MAX + 1 == INT_MAX). If the target hardware doesn't support that in some way, then checks have to be added. If the standard defined signed integer overflow as two's-complement wraparound (INT_MAX + 1 == INT_MIN), additional checks would not be required on most target systems (perhaps all current ones). I didn't have that specific definition in mind. Or it might define out-of-bounds array indexing (for plain C-like arrays) to wrap around, so that given `int a[10];` the expression `a[15]` or `a[-5]` would be equivalent to `a[5]`. Or it could require an exception to be thrown. Ada defines signed integer overflow and out-of-bounds indexing to raise an exception, so there is precedent. Both require the compiler to insert run-time checks, which can be removed during optimization if the compiler can prove that the error will never occur. And there are mechanisms to suppress the checks; if an exception would have been raised but the check is suppressed, the behavior is undefined (Ada calls it "erroneous execution"). Again, I didn't intend to concentrate on the case of signed integer overflow. My question was about which cases of undefined behavior could be made into defined behavior without adding runtime checks. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */