Sujet : Re: Compile time checking of standards conformance
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.c comp.lang.c++Date : 23. May 2024, 19:13:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2o107$1s6ap$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 5/23/24 02:23, Paavo Helde wrote:
...
There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++.
Citation, please? How does it fail to conform?
... The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
The C standard contains the following wording:
"An iteration statement may be assumed by the implementation to
terminate if its controlling expression is not a constant expression200)
, and none of the following operations are performed in its body,
controlling expression or (in the case of a for statement)
itsfexpression-3201) :
— input/output operations
— accessing a volatile object
— synchronization or atomic operations."
In essence, those restrictions mean that the body of the loop does
nothing that need take up any time, so it can be optimized to a nop.
Infinitely many repetitions times 0 execution time has a product that is
mathematically meaningless, but the C standard permits optimizing to
terminate after a finite amount of time.
Note that this does not apply to endless loops occurring by other means,
such as goto.
The optimization you describe would therefore be permitted for C if
those conditions are met - but I can find no corresponding language in
the latest draft I have of the C++ standard (n4928.pdf, dated
2022-12-18). It's a big standard - I might have missed something.