4 ms·
how does int x = x; has ever made sense? In hostoric minimalist direction? (and if you have to ask, x is initialized — with the uninitialized value of x)
by 112233 1y ago
how does
int x = x;
has ever made sense? In hostoric minimalist direction?
(and if you have to ask, x is initialized — with the uninitialized value of x)
- int_19h 1y agoIt is very occasionally useful when you need to define a self-referential data structure, i.e. something like: struct foo { foo& p; ... }; foo x{x, ...}; Still, this is hardly a good justification for it to be the default behavior. There's a reason why ML has `let rec`.
- 112233 1y agoThank you for this, it illustrates an important distinction. (Your example would be clearer if you used a pointer instead of a reference) Address of variable does not depend on it's value, and can be known and used before the variable is defined. At no point in your example value of "x" is used before the end of initialization. However, in order to allow that, the language goes and allows the use of uninitialized value too. That is just plain horrible design.
- int_19h 1y ago> Your example would be clearer if you used a pointer instead of a reference I actually used the reference deliberately here because, unlike a pointer, a reference cannot be rebound. But yes, I agree, this isn't good design. Off the top of my head I can think of at least one way to enable the same thing with a tiny bit of special (and rather obvious) syntax without introducing a footgun. It wouldn't even need any new keywords! foo x{this x, ...};