Maxatar 1 day ago

It's so ironic hearing a comment like this. If what you really want is for C++ to do only what you strictly specified, then you'd always release your software with all optimizations disabled.

But I'm going to go out on a limb here and guess you don't do that. You actually do allow the C++ compiler to make assumptions that are not explicitly in your code, like reorder instructions, hoist invariants, eliminate redundant loads and stores, vectorize loops, inline functions, etc...

All of these things I listed are based on the compiler not doing strictly what you specified but rather reinterpreting the source code in service of speed... but when it comes to the compiler reinterpreting the source code in service of safety.... oh no... that's not allowed, those are training wheels that real programmers don't want...

Here's the deal... if you want uninitialized variables, then explicitly have a way to declare a variable to be uninitialized, like:

    int x = void;
This way for the very very rare cases where it makes a performance difference, you can explicitly specify that you want this behavior... and for the overwhelming majority of cases where it makes no performance impact, we get the safe and well specified behavior.

5
sixthDot 1 day ago

> int x = void;

this is what the D programming language does. Every var declaration has a well know value, unless it is initialized with void. This is nice, optimizing compilers are able to drop useless assignments anyway.

trealira 1 day ago

Nah, they'd never add new syntax like that, given it's inconsistent with the rest of C++.

If they added an explicit uninitialized value representation to the language, I bet it would look something like this:

  int x {std::uninitialized<int>::value};

Maxatar 1 day ago

You're not far off. In C++26 the syntax will be:

   int x [[indeterminate]];
I'm not kidding here;

gpderetta 1 day ago

C++ hasn't done it this way for nullptr or nullopt, why would it do it for an explicit uninitialized?

trealira 1 day ago

I guess nullptr was put in there because because because "#define NULL 0" had some bad consequences for C++ and they needed a replacement.

std::nullopt doesn't seem so different to what I was talking about; I guess it's just less verbose. When I wrote that, I was thinking of things like "std::is_same<T1, T2>::value" being there.

tlb 1 day ago

That's about the right level of ceremony to request an uninitialized variable.

frollogaston 1 day ago

How about int x = 0 if you want 0. Just `int x;` doesn't make it clear that you want 0.

kstrauser 1 day ago

Safe defaults matter. If you're using x to index into a array, and it's randomly initialized as +-2,000,000,000 because that's what happened to be in that RAM location when the program launched, and you use it before explicitly setting it, you're gonna have a bad time.

And if you used it with a default value of 0, you're going to end up operating on the 0th item in the array. That's probably a bug and it may even be a crasher if the array has length 0 and you end up corrupting something important, but the odds of it being disastrous are much lower.

waynecochran 1 day ago

The whole advantage of UB is that this places less restraints on what the optimizer can do. If I say something does not need to be initialized I am giving the optimizer the freedom to do more!

TheBicPen 1 day ago

So what's the issue with introducing explicit syntax to do exactly that if you want to? A safe default does not preclude you from opting out of safety with a bit of syntax or perhaps a compiler flag.

monkeyelite 1 day ago

The issue is that the language was already designed with the old behavior.

monkeyelite 1 day ago

> It's so ironic hearing a comment like this. If what you really want is for C++ to do only what you strictly specified, then you'd always release your software with all optimizations disabled

The whole idea of optimizations is producing code that’s equivalent to the naiive version you wrote. There is no inconsistency here.

RUnconcerned 1 day ago

Optimizations are not "exactly and only what the programmer specifies and no more". They actually fall into the "more" category, believe it or not.

monkeyelite 1 day ago

Show me some O2 optimizations that will act contrary to code I wrote - meaning they violate the “as if” rule.

Maxatar 1 day ago

Two points... the first is you want an optimization that violates the "as if" rule, sure... copy constructors are allowed to violate the "as if" rule, so here you go:

    https://godbolt.org/z/jzWWTW85j
Compile that without optimizations and you get one set of output, compile it with optimizations and you get another. There are actually an entire suite of exceptions to the "as-if" rule.

The second point is that the whole reason for having an "as if" rule in the first place is to give permission for the compiler to discard the literal interpretation of the source code and instead only consider semantics that are defined to be observable, which the language standard defines not you the developer.

There would be no need for an "as if" rule if the compiler strictly did exactly what it was told. Its very existence should be a clue that the compiler is allowed to reinterpret the source code in ways that do not reflect its literal interpretation.

William_BB 8 hours ago

The standard says that "Copy elision is <...> one of the two allowed forms of optimization, alongside allocation elision and extension,(since C++14) that can change observable side-effects"

I agree you have a valid point though. I'd be interested to know the committee's reasoning.

monkeyelite 13 hours ago

> if the compiler strictly did exactly what it was told.

What does this mean since I am not writing assembly and there is no specified correspondence between assembly and C++.

RUnconcerned 1 day ago

I dunno about what you wrote, but here is one that clearly violates what OP said!

https://godbolt.org/z/Y4Yjb7z9c

clang notices that in the second loop I'm multiplying by 0, and thus the result is just 0, so it just returns that. Critically, this is not "exactly and only what the programmer specifies", since I very much told it to do all those additions and multiplications and it decided to optimize them away.

monkeyelite 13 hours ago

What? But the result is the same!