4 ms·
You could potentially argue that, but certainly Rust doesn't encounter this issue despite being const by default because they simply don't have constructors in
by chippiewill 5y ago
You could potentially argue that, but certainly Rust doesn't encounter this issue despite being const by default because they simply don't have constructors in the first place.
- mikepurvis 5y agoYeah I knew I was going to get called out with a Rust comparison. I think the Rust approach basically acknowledges the issue with what C++ did— that automatic initialization is cute but ultimately wasn't worth what it ended up costing in terms of hidden control flow, poor error handling, static initialization issues, etc. Anyway, Rust basically deals with it by giving the class designer the choice to supply factory functions or punt on it, making the user initialize every field themselves each time. And I think most agree that this is a good approach; it's the best of C++ (factories) with a better fallback than a default constructor.
- gpderetta 5y agoBut you have exactly the same options in C++.
- masklinn 5y agoReally? What's the factory function invoked for a copy or a move?
- gpderetta 5y agoWhatever you want it to be. By default of the compiler will generate calls to move and copy constructors.
- mikepurvis 5y agoSure, but the point is that providing factories requires extra work and consideration, so a lot of C++ classes instead lean on the default option of a constructor. Rust's removal of that forces the class designer to choose.
- aphexairlines 5y agoIt's also easy to provide a default factory: https://doc.rust-lang.org/std/default/trait.Default.html https://doc.rust-lang.org/std/default/trait.Default.html
- mikepurvis 5y agoTrue, but the naming does matter. Calling Thing::default() clearly communicates that your just getting baseline values and not a lot of magical other initialization stuff going on— with a Thing::Thing() in C++, you're really at the mercy of whatever the project conventions are for how "fat" the constructor is going to be. I think the naming is also important for cases where there are potentially multiple reasonable defaults, even something as basic as the difference between Vector3::Vector3() and Vector3::zero().
- TheMonarchist 5y ago> Calling Thing::default() clearly communicates that your just getting baseline values and not a lot of magical other initialization stuff going on— with a Thing::Thing() in C++, you're really at the mercy of whatever the project conventions are for how "fat" the constructor is going to be. In C++ the constructor without arguments is called default constructor. Of course the expectations depend on conventions, but usually it's something from uninitialized garbage to an empty state.