3 ms·
C++ addresses this with the `explicit` specifier: all single-argument constructors are inherently conversion operators so don't need to be labeled "implicit" as
by gumby 3y ago
C++ addresses this with the `explicit` specifier: all single-argument constructors are inherently conversion operators so don't need to be labeled "implicit" as they are in C#.
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.
- deleted 3y ago[deleted]
- Guvante 3y agoHaving a vector with a sized based constructor transform an integer into a vector is surprising.
- gumby 3y agoYeah, 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.
- Guvante 3y agoNo, 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.