logoalt Hacker News

William_BB • today at 4:43 PM • 1 reply • view on HN

If my understanding is correct, C++ built-in types can be declared without initializing them:

  int x;   // Unitialized, garbage value
  int x{}; // Value-initialized (falls back to zero-initialization)
We want classes to have the same behaviour:

  Struct s;   // Default-initializes (presumably we want members uninitialized)
  Struct s{}; // Value-initializes (presumably we want members initialized)

Replies

ninkendo • today at 4:50 PM

As I replied in a sibling comment to yours, at least one language without constructors (Rust) has the exact same semantics for built-in types vs library types here. You must initialize in typical code, and the escape hatch is std::mem::uninitialized() which lets you work with uninitialized data, and this is true for any type, built-in or otherwise.

➕ show 1 reply