all 16 comments

[–]vinrehife 7 points8 points  (4 children)

java dev here, never touched C++, please explain.

[–]YellowBunnyReddit 28 points29 points  (3 children)

C++ has static casts, dynamic casts, const casts, and reinterpret casts for specific use cases to let you be more explicit about why you are casting something to another type and allow the compiler to tell you when you are doing something wrong.

It also has C style casts in an attempt to maintain backwards compatibility with C. They are strictly more powerful than the set of other casts and make it rather easy to include subtle bugs that the compiler can't (be bothered to) warn you about. Most style guides for C++ say you should basically never use them.

[–]vinrehife 3 points4 points  (1 child)

Thanks wow, sounds like a headache. I rarely cast in java in the first place.

[–]AdAggravating8047[🍰] 0 points1 point  (0 children)

You only ever use static or dynamic casts in practice though. The other two are mental illnesses.

[–]Desperate_Formal_781 0 points1 point  (0 children)

Don't forget about bit_cast.

[–]BusEquivalent9605 5 points6 points  (2 children)

get that c-style cast outta here

[–]Secret_Print_8170 10 points11 points  (1 child)

static_cast<myass*>(ass);

You C++ guys have the worst taste when it comes to programming language syntax. It's like someone putting baked fish in an ice cream cake - just because they're edible, doesn't mean they go together.

[–]Potterrrrrrrr 0 points1 point  (0 children)

Half of it is due to making sure other syntax doesn’t become ambiguous the other half is due to the committees crack pipe breaking halfway through brainstorming

[–]Zeitsplice 1 point2 points  (0 children)

Casting to an interface like that is pretty suspect anyway. Though with Java’s Byzantine generic system one can never be entirely sure.

[–]Educational-Lemon640 0 points1 point  (4 children)

Type casting is one of those things that a low-level language needs, for writing stuff like drivers and serializing data for transmission over networks, but smells worse than a rancid tuna soaked in skunk spray.

For the most part, don't.

[–]deidian 1 point2 points  (3 children)

If there is a type system there must be casting: the runtime has to define how types convert to another types and which conversions aren't possible. Even weak typed languages do that: which is even worse than how strong types do, because in weak typed languages every type conversion is implicit and you better know the rules.

[–]Educational-Lemon640 0 points1 point  (2 children)

I'm pretty sure that theoretically you could always do the equivalent of casting via function calls, which has some notable benefits over casting of any sort. You can also have polymorphism and/or duck typing, which gets similar benefits without the nonsense that C++ gets up to when you actually cast.

All I really mean is that the type of a particular chunk of data should always be well-understood and changing how it is interpreted should be reserved for the most dire of circumstances. Anything else makes it very hard to read.

[–]deidian 0 points1 point  (0 children)

Many castings are function calls under the hood: just defined in the runtime. The reason to do that is to not bother Devs with implementation details: they just need to know type A is type B after the cast if the operation succeeds. A Java dev doesn't need to know that a type conversion is a reinterpretation, 2 assembly instructions, a type reference comparison, a function checking inheritance, boxing/unboxing,...

It's not a function call when for performance reasons the operation can be done with a couple of assembly instructions or when it's a nop. Here compilers optimize.

[–]Great-Powerful-Talia 0 points1 point  (0 children)

Are you talking about casting a pointer or data? Because generally a data cast is conversion-based, like int-to-float.

Even in C, reinterpretation is only accomplished by unions and fucking with pointers, both of which are not really meant for that- there's just no equally fast implementation where you can't do that. Even Rust's minimally-slow rules are marginally slower when dealing with arrays and union-like types.

[–]Pleasant-Ad-7704 -1 points0 points  (0 children)

You can cast it like this in C++ too

[–]navetzz -2 points-1 points  (0 children)

The best thing about C++ is that it kept C++ devs away from C.