←back to thread

611 points LorenDB | 1 comments | | HN request time: 0.199s | source
Show context
monkeyelite ◴[] No.43913647[source]
This article compares onlye one specific feature of C++ with Rust - integral type conversions.
replies(1): >>43914108 #
tialaramex ◴[] No.43914108[source]
All of C++ has implicit type conversions. The language even has a keyword (explicit) to try to reign this in because it's so obviously a bad idea.

In Rust what they'd do if they realised there's a problem like this is make explicit conversion the default, with a language Edition, and so within a few years just everybody is used to the improved language. In C++ instead you have to learn to write all the appropriate bugfix keywords all over your software, forever.

replies(1): >>43916271 #
monkeyelite ◴[] No.43916271[source]
> because it's so obviously a bad idea.

Agreed. The history here is compatibility with C type conversion.

I just expected a more compelling Rust /C++ comparison but we got an emphasis of a poorly designed feature which the standard has taken steps to improve already.

replies(1): >>43916477 #
tialaramex ◴[] No.43916477[source]
No, implicit conversion is a deliberate C++ feature and no analog existed in C. Like a lot of awful things about C++ this is their own choice and it's frustrating that they try to blame C for their choices.

In C++ when we define a class Foo (a thing which doesn't exist in C) and we write a constructor Foo(Bar x) (which doesn't exist in C) which takes a single parameter [in this case a Bar named x], that is implicitly adopted as a conversion for your new user defined type and by default without any action on your part the compiler will just invoke that constructor to make a Bar into a Foo whenever it thinks that would compile.

This is a bad choice, and it's not a C choice, it's not about "compatibility".

replies(2): >>43916553 #>>43923549 #
seanhunter ◴[] No.43923549[source]
My copy of K&R disagrees with you that implicit conversion didn’t exist in C. See section 2.7 “Type conversions”.

   “Implicit arithmetic conversions work much as expected. In general, if an operator like + or * that takes two operands (a binary operator) has operands of different types, the ``lower'' typekis promoted to the ``higher'' type before the operation proceeds.”
It goes on including giving a table of all the conversions and a set of heuristics which normally apply.
replies(1): >>43952209 #
1. monkeyelite ◴[] No.43952209[source]
Where do you see the disagreement?

Numbers are implicitly converted to the “largest” and perhaps “floatiest” representation.