Re:Are Implicit Operators a Path to Clean Code or a Buggy Ni...
by jonas21 on Tuesday August 08, 2023 @11:38PM
(#37058437)
C#'s implicit keyword was presumably a reaction to the surprising behavior you get in C++ when you forget to mark a single-argument constructor "explicit". They just flipped the default.
Re:Are Implicit Operators a Path to Clean Code or a Buggy Ni...
by Guvante on Wednesday August 09, 2023 @12:10AM
(#37058598)
Having a vector with a sized based constructor transform an integer into a vector is surprising.
Re:Are Implicit Operators a Path to Clean Code or a Buggy Ni...
by gumby on Wednesday August 09, 2023 @02:42AM
(#37059412)
Yeah, I find that weird too — everybody does. It’s a legacy of the early 90s.
But the case under discussion is single-argument constructors, which can also be considered conversion operators.
Re:Are Implicit Operators a Path to Clean Code or a Buggy Ni...
by Guvante on Friday August 11, 2023 @02:52AM
(#37085866)
No, the topic was "implicit isn't the right default"
It was a mistake that can't be fixed saying "C++ doesn't need implicit as a keyword so it is better" is wrong when explicit is the better default if you need one.
This makes sense: people are already unsurprised when they initialize a float with an integer. `float foo = 1;` doesn't put a 1 bit in the lsb, but initializes the float to 1.0.
But sometimes (often!) you don't want that, so specify your constructor as `explicit` and then it won't convert something else to satisfy your constructor's argument specification.