4 ms·
C++ Fun with(out) keyword explicit
- mappu 11y agoI wonder which compiler it was?
- jsharf 11y agoMe too. I posted a comment on the blog about it, maybe the author will respond there?
- sankoz 11y agoSaw a response from the author. The compiler is Embarcadero’s C++ Builder 2010.
- blub 11y agoEmbarcadero C++ 2010, formerly known as Borland C++.
- cheez 11y agoFWIW, their latest incarnation uses Clang as a front-end for at least the 64-bit version.
- dgemm 11y agoImplicit casts are the worst feature of C, and C++ manages to make things even worse with implicit conversions by default. Marvelous. I should create a new subset of C++ and call it C += 0.5.
- mlvljr 11y agoThere's one already, and at V. 1.0! https://github.com/ErisBlastar/cplusequality https://github.com/ErisBlastar/cplusequality
- oselhn 11y agoIn C++11 you can even have implicit conversion for multiple args constructors. But it is harder to use unintentionally than single argument implicit conversions.
- echelon 11y agoThe choices made by C++ are some of the reasons why I am so happy that Rust exists.
- Animats 11y agoThis is why it's a rule in C++ that implicit conversions generate ambiguities and error messages like that. In such ambiguous situations, one interpretation has to be significantly "better" for it to be accepted without an error message. This is better than silently picking one of the possibilities when there's an ambiguity. I would have thought that the no-conversion case (std::string vs std::string) would clearly beat the conversion case (Annie vs Annie), but I'm not really current on C++ conversion priority. This sort of thing is why operator overloading isn't that great an idea. It seems attractive at first, but it gets complicated fast. As a rule of thumb, unless you're doing something mathematical, like matrices, for which the rules are well understood, don't overload mathematical operators. A classic in Python: (1,2) + (3,4) -> (1,2,3,4). But numpy.array((1,2)) + numpy.array((3,4)) -> array([4,6]). This, combined with Python's lack of parameter type checking, can lead to strange results if a library function is called with the wrong type of numerical list object.
- blub 11y agoIf one has compile time safety, operator overloading is just fine though. In this case I'm pretty sure this was a compiler bug, "no conversions" should have priority over "conversions"
- dietrichepp 11y agoCompile-time safety doesn't protect you from being stupid when you choose the semantics of overloaded operators, just like compile-time safety doesn't protect you from giving your functions stupid names. My hypothesis is that so many C++ programmers abused operator overloading (even in the standard library, e.g. the absurdity of <<) that it made language designers remove it altogether from e.g. Java and many other languages.
- blub 11y agoThe argument against operator overloading can be reduced to "programmers can't be trusted to design their classes properly". And Java was exactly like that in the beginning, a language designed for the average developer, giving them only the basics they could be trusted with. Interestingly, Java's been busy adding back most of the things it decided against. Wouldn't be surprised if they add operator overloading in a future version. << is not IMO absurd, it makes streaming much nicer. The boost serialization & operator is however the kind of overloading that gives one pause when they see it.
- 72deluxe 11y agoI put the default constructor in private and mark the constructor I want everyone to use (typically the only one) as explicit. Seems safer than implicit conversions.