Remix.run Logo
▲ quietbritishjim 6 hours ago

In C++ you can write "int x;" and get an object of type int.

If the only way to initialise a class-type object was to call some custom static method on it then you'd have to write "Foo x = Foo::create();". Suddenly it looks more special than a built-in type.

▲ninkendo 6 hours ago | parent | next [-]

In C++, `int x;` leaves the int uninitialized, which isn't really constructing at all. In rust (which I program in lately so I'll use it as the example) you simply can't use uninitialized variables in the first place without unsafe code, so the equivalent would either be:

  let x: i32;
  foo(x); // compiler error
or if you really wanted to use uninitialized memory,

  let x: i32 = unsafe { std::mem::uninitialized() }; // compiler warning
  foo(x);
which, in rust, can be done with any other type too (substitute i32 for any T and the above examples are identical.) I don't see anything different about built-in types here vs standard ones, other than the compiler has literals for the built-ins, which is more a consequence of rust not allowing literals for library types. But that's a very different discussion.

I guess the argument might be that for C++, they want the author of the type to determine whether the value should be left uninitialized on declaration/construction, instead of the caller. Which is... not exactly the semantics I would have chosen.

▲steveklabnik 4 hours ago | parent | next [-]

Please note that mem::uninitialized is always unsound. Your example should be using MaybeUninit<T>. While that example would still be unsound directly anyway, it at least gives you the ability to do the right thing (foo would need to take the MaybeUninit<i32>).

▲pjmlp 5 hours ago | parent | prev | next [-]

It is up to the constructor to only initialize storage, or do whatever it feels like to have values assigned to it.

Thus a default constructor replicates a plain int x;, whereas the copy constructor or single value constructor is used for int x = 42;.

By the way, already in the 16 bit days you could configure to have warnings as errors for variables being used without initialisation.

▲ninkendo 5 hours ago | parent [-]

> It is up to the constructor to only initialize storage, or do whatever it feels like to have values assigned to it.

Right, see the "I guess the argument might be" part above... I find this to be a very questionable design choice, leaving you to have to go and read a header somewhere to determine whether `Foo f;` is safe or not. But it's not really a choice, is it? C++ had to do it this way to be compatible with how C programs were written back in the 1970s.

> By the way, already in the 16 bit days you could configure to have warnings as errors for variables being used without initialisation.

Ah yes, the "but C can be configured to be not-insane" argument, one of my favorite pjmlp-isms, right up there with "but $LANG's compiler is written in C++" and "but you already get memory safety with a GC". Keep on doing you!

▲quietbritishjim 6 hours ago | parent | prev [-]

Hmm interesting. Is there a way to "value initialise" (in C++ speak) an int in Rust to set it to zero? Like `let x: i32 = {};` (without explicitly writing `=0`)? How about a way to do that for a sort of plain struct where there are multiple members you just want to set to their default values?

▲tialaramex 5 hours ago | parent | next [-]

Rust has a Default trait, if your type Goose has a Default then

let foo: Goose = Default::default();

Those "primitive" types have a Default, the integers default to zero, as do the floating point types, bool defaults to false, I believe an empty string is the default string (in both the sense that's the default for String, and the default for str, the string slice reference roughly analogous to C++ string_view)

Some things don't have a sane default, just as those things wouldn't provide a zero argument constructor in C++. There isn't a default file descriptor, a default network socket, and so on and so those types do not implement Default

▲quietbritishjim 3 hours ago | parent [-]

That makes sense, thanks!

▲ninkendo 6 hours ago | parent | prev [-]

Yep, that's std::mem::zeroed(), which just writes zeroes into the allocated memory regardless of its type... also only available in unsafe code.

    #[derive(Debug)]
    struct Foo {
        _i: i32,
    }

    fn main() {
        let x: i32 = unsafe { std::mem::zeroed() };
        println!("x={x}");
        let f: Foo = unsafe { std::mem::zeroed() };
        println!("f={f:?}");
    }
prints:

    x=0
    f=Foo { _i: 0 }
It's unsafe because if Foo contained a String, it would be string with a null pointer inside it and you'd crash trying to read it. If you want "Default" values instead of zeroed (ie. you don't want to run unsafe code), that's what the Default trait is for, and this becomes:

    #[derive(Debug, Default)]
    struct Foo {
        _i: i32,
    }

    fn main() {
        let x: i32 = Default::default();
        println!("x={x}");
        let f: Foo = Default::default();
        println!("f={f:?}");
    }
and prints the same thing.
▲pwdisswordfishq 5 hours ago | parent | prev [-]

But you can also write int x = rand(); how is that special?

▲steveklabnik 3 hours ago | parent [-]

"uninitialized" is not a synonym for "random value", it's a totally different state.

https://www.ralfj.de/blog/2019/07/14/uninit.html