2 ms·
I think that explicit casts really ought to be discounted, since if you're writing one, you are simply getting what you have asked for. This would be like sayin
by int_19h 2y ago
I think that explicit casts really ought to be discounted, since if you're writing one, you are simply getting what you have asked for. This would be like saying that e.g. Modula-2 is weakly typed because it has bitcast.
That aside, the only remaining footgun in C++ is the implicit numeric conversions. What else did you have in mind?
- simonask 2y agoI mean, the default behavior of single-argument constructors in C++ is implicit conversion. You have to opt into explicit conversions using the `explicit` keyword on constructors and assignment operators. Then you have all the shenanigans around placement-new and vtables. If it isn't downright weak, it's also not particularly strong.
- int_19h 2y agoThe issue with constructors is a real one, yeah. Although forcing `explicit` on single-argument constructors is a single linter rule (which is a good idea for this exact reason...). OTOH placement-new is pretty much impossible to use by accident. If used intentionally, I don't see it as being any different from an explicit cast - again, you get what you signed up for. Interestingly in some ways C++ is arguably more typesafe than languages like Java or C#, given how it handles dynamic type of object during construction & destruction...