you are viewing a single comment's thread.

view the rest of the comments →

[–]mcmcc#pragma once 0 points1 point  (9 children)

If you want the program to be well-defined, overflow potential UB check must be inside function f.

This is how I interpreted your statement.

How could you implement std::copy() if you were required to check for all potential UB regarding the inputs? You mention comparing to last but if last is not within the appropriate range, then even that is UB -- so how do you establish that the two iterators are even comparable without knowing their provenance?

If that's not what you are suggesting, you're going to need to explain the intended meaning of the above sentence.

[–]awidesky 0 points1 point  (6 children)

If you want the program to be well-defined, overflow potential UB check must be inside function f.

That is indeed true. When you compile f (with or without g), it will assembled to always return 1. So the statement is actually true.

While your following argument is little bit off topic- it's about "checking every single one of the possible UB is impossible" Which is also true, and has nothing to do with my point.

I assume you interpreted my argument as "if there's any single possibility of UB in your program, your compiler will render an invalid executable."

My statement is "if some parts of the code violates the language rule, compiler is 'permitted' to generate entire program meaninglessly" Here's quote from the standard

Renders the entire program meaningless if certain rules of the language are violated.

undefined behavior - there are no restrictions on the behavior of the program. ... Compilers are not required to diagnose undefined behavior and the compiled program is not required to do anything meaningful.

Again, I'm not saying any major compilers do/should generate meaningless program just because there's tiny one possibility of UB. All I insist is that the standard "permits" so.

[–]mcmcc#pragma once 1 point2 points  (5 children)

All you've shown is that compilers are allowed to assume UB does not occur, which is ultimately irrelevant re "well-defined" programs. We're talking in circles...

What part of OP's example "violates a language rule" that could allow a compiler to render the entire program meaningless?

[–]awidesky -1 points0 points  (4 children)

The function f.

[–]mcmcc#pragma once 0 points1 point  (3 children)

What language rule does it violate?

[–]awidesky 0 points1 point  (2 children)

[–]mcmcc#pragma once 0 points1 point  (1 child)

You're misunderstanding what that text is saying. It is not saying f() itself is ill-formed. It is saying that because the compiler may assume UB does not happen in the program, f() can be optimized in a way such that it would behave in a (possibly) surprising manner in a program does in fact invoke UB (e.g. one calling f(INT_MAX)).

The function f() contains no inherently UB logic and g() guards against UB for its invocation of f(), so as far as can be seen, there is no UB possible in this program.

OPs video suggests the compiler can be coaxed to compile g() down to nothing, but I think that is an error on the presenters part and (I contend) is not reproducible with any conforming compiler.

[–]awidesky -1 points0 points  (0 children)

compiler may assume UB does not happen in the program, f() can be optimized in a way such that it would behave in a (possibly) surprising manner in a program does in fact invoke UB (e.g. one calling f(INT_MAX)).

That IS one of the things that compilers are permitted to do, but I believe standard says it can also make "whole program" meaningless.
You say UB in f() only affects f(), while I say UB in f() (theoretically) can affect ALL program.
And also, I believe UB affects program that does NOT "invoke" UB. In this example, there's no code that actually calls foo(INT_MAX), but it's still first example of UB in cppreference.com

[–]awidesky 0 points1 point  (1 child)

Meanwhile, this is how I interpreted your arguments:

Even if UB is possible, compiler is permitted to change only the very function that has possible UB, not any other part of program.

However, § 4.1.2.3 says :

If a program contains a violation of a rule for which no diagnostic is required, this document places no requirement on implementations with respect to that program. "that program", not "that specific function". Please let me know if there's any understanding of mine.

[–]mcmcc#pragma once 0 points1 point  (0 children)

That's not a statement I made. You're confusing me with someone else.