3 ms·
> As soon as failure is inevitable, it has to be OK to detect it. That means if you're printing values 0 to 100, you'll never even get the first one printed. Yo
by Dylan16807 8y ago
> As soon as failure is inevitable, it has to be OK to detect it. That means if you're printing values 0 to 100, you'll never even get the first one printed. You'll abort before entering the loop. This is a bit strange to C/C++ programmers.
It's strange but compilers are allowed to do it and anyone sufficiently familiar with undefined behavior will not be surprised.
- comex 8y agoCompilers are not currently allowed to do it if “failure” is in the form of an abort from an inlined function, as in the case of .at(),
- deleted 8y ago[deleted]
- Dylan16807 8y agoAh, so the compiler doesn't understand it's a failure, and just sees normal program flow that it can't alter. That's a tricky thing to solve, yeah. At best, under current semantics, the compiler could emit a fast loop and a slow loop, and pick which one using an initial bounds check. But that's extremely awkward at best... Automatically handling the bounds check upfront is a type of control flow that's halfway between normal exceptions and undefined behavior, that C++ doesn't really grok.
- celrod 8y agoWhen I dabbled in gfortran, a feature I liked was that while no bounds-checking is the default, run time bounds-checks can be enabled with "-fcheck=bounds" or "-fcheck=all", which can be handy while developing/testing code. Alternatively, Julia has a macro "@inbounds" which disables checks, but this involves going through and annotating code after testing. You can also start Julia with the options "--check-bounds={yes|no}" to override declarations. Running testsuites enables all checks. Seems like it would be handy if these checks could be elided by defining a macro. Any reason this isn't done? The performance cost is often much higher than the time spent repeatedly checking bounds. It tends to defeat the auto-vectorizer. Another workaround is to manually check bounds before the loop. This lets the compiler prove all bounds checks pass if it gets to the loop, and solves the problem. I do wish that sort of thing could be automatic.
- 0815test 8y agoIf that's the semantics you want, why not just add a check at the beginning of the loop? Compilers could add a warning advising the developer to do this if she doesn't care about partial results, anytime that they can't fully eliminate an .at() bounds check inside a loop.