An unofficial, read-only Hacker News reader. Join the beta →

In C++, = default and {} are not the same constructor (opens external site)

129 points by surprisetalk 2 days ago | lemire.me | 198 comments on HN

View full thread · View on Hacker News

Comment thread (5)

  1. MaybeUninit::uninit().assume_init() is, exactly like the deprecated† core::mem::uninitialized() immediately UB unless your type is zero size and thus there's no difference between initialized and uninitialized.

    You can unsafely ask for an uninitialized value of any type, but you should not ever assert that you've initialized it unless you actually have. Essentially, you can write this in Rust, but you need to be specific about what you're doing and when you are or are not initializing the variable or else things will go badly for you.

    It wouldn't be worse in C++, historically, but C++ people are more used to their world being set on fire by an angry compiler because they did something wrong. In Rust you'd be quite surprised by the compiler's wrath.

    † Edited to add: Technically today you do not get this if you call core::mem::uninitialized, because it was so dangerous, instead if you call the deprecated function on a non-ZST - ironically you get a a value scribbled with 0x01 bytes. Doing this ensures in most cases LLVM won't set fire to your program immediately but at a price of doing all that extra work of writing 0x01 bytes. But we did tell you (for several years now) how to do better.

    Replies (1)
    1. I completely agree, but the point was to show Rust code that does the same thing as the (equally unsafe) C++ code, and particularly that it doesn't treat built-ins any different than other types. But nobody should be writing code like this in either language.

      To me the whole point of an "uninitialized variable" is to declare it in one place and then assign it in exactly one code path, which is exactly what rust supports without any unsafe code:

        let i: i32;
        if is_tuesday {
            i = 0;
        else {
            i = 1;
        }
        foo(i);
      
      is already perfectly valid, safe rust code, because the compiler knows that all code paths result in assignment before use. And the answer to "what happens on that first line?" is "nothing", because it's just a declaration. In the equivalent C++ you'd be running an arbitrary constructor on the first line, and hoping the compiler optimizes it away. Or if the default constructor doesn't exist or is deleted, you simply can't code that way.
      Replies (2)
      1. Your code snippet is fine, but MaybeUninit comes into its own when it's not obvious to the compiler that what you wrote is OK - MaybeUninit::assume_init is how you tell the compiler "I promise this is correctly initialized" and so long as you're correct, everything is fine. By Rice's Theorem no compiler could ever be sure all the time, and in fact Rust choose to be extremely pessimistic, obvious things like yours work, non-obvious things do not and if you write a ticket about a case you feel ought to be obvious but was not (please don't) the Rust folks will say that is as intended, here are some alternatives to achieve what you meant, up to and including MaybeUninit
      2. In C++ you can do: int i = is_tuesday ? 0 : 1;
        Replies (1)
        1. Well, sure, in Rust you can do `let i = if is_tuesday { 0 } else { 1 };`, what's your point?

          Imagine it's 10 variables instead of one, and the value you set them to is based on much more complex/nested logic. Sometimes for readability it's nice to declare and specify their types up front, then you go about initializing them all via some complex logic, then you use them later. The compiler should guarantee that they're all initialized before use. There's no real way to do this in C++ every time: For primitives it works, but for other types, it needs a no-argument constructor available or the code won't compile, and even if a no-argument constructor is available, the compiler will needlessly run that constructor even though it doesn't need to (and maybe it'll optimize that away but who knows.)

          You can obviously make this work in C++ by (subjectively) decreasing readability, but wouldn't it be nicer if declaration was an explicit thing separate from initialization, and the compiler would simply enforce that it's safe?