That feels a bit like begging the question? Library types only need constructors to replicate built-in types if built-in types use constructors themselves. But do built-in types need constructors? What is lost by just using regular functions?
Also I was under impression that Rust at least has a similar goal?
Both those feel a bit like circular arguments? They need constructors because that's how they are currently defined, but that doesn't say anything about whether they could also be defined without constructors.
The latter was what I was interested in, and I apologize for not making that clear. For instance, in Rust terms both those seem replaceable in principle via calls to appropriate trait functions like Default::default() or From::from().
They could have just as easily said "C semantics are used when we know we're parsing C code" and declared that C++ code doesn't have implicit constructors. No, the decision to have constructors was made because it's what they wanted to do when the language was designed. You can't lean on "but C" as an excuse when those problems are so easily solved other ways.
> Well, in Rust, you can't make your own Box without using #[lang = "owned_box"] and #[fundamental], plus it has special orphan rule handling.
Sure, but are those "just" characteristics of Rust's current implementation or are those inherent to a constructor-less model in general? The latter is the more interesting question to me in this particular context, and I think it's similar to the question you express.
I think guaranteed in-place initialization is probably the strongest example of something that cannot be expressed through From or Default alone. There are proposals to solve this via traits. I haven't dug deep enough into them, but I'd assume those would be "special" traits that rely on compiler-provided semantics not otherwise expressible in ordinary library code.
Right now, both Box::new() and the vec![] macro resort to compiler intrinsics (write_via_move and write_box_via_move), particularly to avoid unnecessary intermediate storage and excessive stack usage.
> Also I was under impression that Rust at least has a similar goal?
Rust's choice here is slightly different, Rust wants to give all of the types features, whereas C++ wanted to specifically give the user defined classes as much functionality as possible.
So in Rust 5 and "Five" and '5' and [5, 5, 5, 5, 5] all can have the features you get in your own custom type named Five whereas in C++ they do not. In C++ they're not allowed to have methods ("member functions") because they are "just" primitive types. In Rust the operators are actually implemented for all the types they can be used on, not just for your own user defined classes and so on.
In a few cases this is "fake", for your own type to have a + operator you'd have to write an implementation of addition and the Rust stdlib provides an "implementation" for the machine integers which says nothing of importance because of course in reality that code is never executed, the Rust compiler knew how to add two integers together already. However most of them are real albeit often trivial.
Because Rust doesn't privilege a specific user defined type, all the user defined types get the features which make sense, including not only struct but also enum and union. It still surprises me that C++ never gave enum class (its improvement on the C enum user defined type) methods, of course a Rust enum has methods.
Also I was under impression that Rust at least has a similar goal?
Replies (2)
Replies (1)
The latter was what I was interested in, and I apologize for not making that clear. For instance, in Rust terms both those seem replaceable in principle via calls to appropriate trait functions like Default::default() or From::from().
Replies (2)
Which is why the only difference between a struct and a class, is the default access method.
Thus that C file could be made extensible in C++, with zero code changes on the call sites.
Replies (2)
The question is, at least for me, do you really miss out on anything by not having an ability to do so? I think you miss anything, but you gain a lot.
Replies (1)
Sure, but are those "just" characteristics of Rust's current implementation or are those inherent to a constructor-less model in general? The latter is the more interesting question to me in this particular context, and I think it's similar to the question you express.
Replies (1)
I think guaranteed in-place initialization is probably the strongest example of something that cannot be expressed through From or Default alone. There are proposals to solve this via traits. I haven't dug deep enough into them, but I'd assume those would be "special" traits that rely on compiler-provided semantics not otherwise expressible in ordinary library code.
Right now, both Box::new() and the vec![] macro resort to compiler intrinsics (write_via_move and write_box_via_move), particularly to avoid unnecessary intermediate storage and excessive stack usage.
Continue this thread (1 replies) →
Rust's choice here is slightly different, Rust wants to give all of the types features, whereas C++ wanted to specifically give the user defined classes as much functionality as possible.
So in Rust 5 and "Five" and '5' and [5, 5, 5, 5, 5] all can have the features you get in your own custom type named Five whereas in C++ they do not. In C++ they're not allowed to have methods ("member functions") because they are "just" primitive types. In Rust the operators are actually implemented for all the types they can be used on, not just for your own user defined classes and so on.
In a few cases this is "fake", for your own type to have a + operator you'd have to write an implementation of addition and the Rust stdlib provides an "implementation" for the machine integers which says nothing of importance because of course in reality that code is never executed, the Rust compiler knew how to add two integers together already. However most of them are real albeit often trivial.
Because Rust doesn't privilege a specific user defined type, all the user defined types get the features which make sense, including not only struct but also enum and union. It still surprises me that C++ never gave enum class (its improvement on the C enum user defined type) methods, of course a Rust enum has methods.