My favourite thing about C/C++:

Given foo(int x)

Did you know all of the following will decay into an int without a cast?

foo('a');
foo(42);
foo(3.5);
foo(true);
enum { a, b, c }; foo(a);

C++'s type promotion and decay rules are also such that

foo(double x)

Will accept both floats and doubles. Floats will be promoted to double automatically and silently. This isn't usually a problem, but I've had cases where I've had to be aware of which floating-point type is in use.

Follow

@Elizafox The cases where I can see is more in scientific simulations, where the difference between double and float could make algorithms non convergent.

Implicit conversions can be very dangerous. The Ariane 5 disaster happened because of a cast from 64-bit to 16-bit. Those are also implicit in C++ AFAIK.

en.wikipedia.org/wiki/Ariane_f

@abde Generally speaking promotions are preferred to decay but if casted then all bets are off.

en.cppreference.com/w/c/langua for minutiae about it

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.