Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

in fact it is exactly UB that allows that. If, say, the standard specified wrap behaviour, a conforming compiler wouldn't be allowed to trap.


You are right, but OP asserted that IB (implementation defined behavior) would be needed for this. IB would also allow this, but it is not necessary.

All of undefined/unspecified/implementation-defined allows this.

edit:

Whether unspecified or implementation-defined would allow trap depends on the exact wording. If the wording is along the lines of "[on overflow] the value of the expression is unspecified/implementation-defined" then trap is not allowed. But it can be also "[on overflow] the behavior of the program is implementation-defined", which would also allow trap. Point being, unspecified/implementation-defined also have a defined scope, while undefined behavior always just spoils the whole program.

I'm fine with the UB overflow though.


> the behavior of the program is implementation-defined

You're basically defining undefined behavior. The way to do this "properly" would be "the program is allowed to do x, y, z, or also trap"–there's really not much else you can do.


The difference here is that the behavior would need to be documented.

That's why I didn't bother with "the behavior of the program is unspecifed", because that really is just undefined behavior.

But yeah, actually listing allowed behaviors and stating that the actual behavior out of them is unspecified/implementation-defined is more practical.

One example is conversion to signed integer in C17:

> ...either the result is implementation-defined or an implementation-defined signal is raised.

https://cigix.me/c17#6.3.1.3.p3


Well, it is not that easy. Specifying a subset of allowed outcomes might still prevent future evolution.

For example bound checking, while fairly cheap for serial code, makes it very hard to vectorize code effectively. Then again so does IEEE FP math and that's why we have -ffast-math...

In the end, it is not impossible, it is just tradeoffs.

I'm generally on the opinion that there should be as little UB as possible, but probably this is better done by compilers that can push the limit of static and dynamic checking technology instead of the standard mandating it.


Right, that’s my point at the top of the thread :) I’m just saying if you don’t end up with something that looks like what I just mentioned you’re really not specifying anything, so you might as well go for undefined behavior. This is fine.


Yes, I think we are in complete agreement :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: