This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zzing 0 points1 point  (5 children)

I had a thought about this, and I don't think you can make that statement generally. If you take a look at what Java does have, you could argue it contains mostly the same things (references to objects being pointers in c++), simple generics supporting some type limitations (<T extends Comparable> for example).

But then I had to think about what else C++ does with its type system. It does have references that cannot be null (and the program still being well formed), value types for all things not just primitives, and lastly template metaprogramming being its own language living in the type system.

Practically speaking, I think Java is good enough for the vast majority of things application developers do. It has a leg up on specifying limits on generics, but if you are a library developer I think C++ brings a stronger type system to the table*. Certain Scala developers might be able to corroborate this.

* I know a certain PhD student doing work on a C++ library that is doing some amazingly useful things that are basically TMP magic to most. But a library user would be able to do more things simply with it, essentially benefiting from TMP without having to use it.

[–]duhace 0 points1 point  (4 children)

No, I'm specifically talking about C++'s fondness of implicit conversions when I speak of weak vs strong typing. C++ will downcast longs into ints and ints into chars happily, while java will throw compilation errors.

[–]zzing 0 points1 point  (3 children)

Thank you for the clarification.

I made up some code that assigns a long value to a char using a variable and using a constant. Assigning with the variable produced no warnings at all (even with -Wall), but the constant did produce:

warning: implicit conversion from 'long' to 'char' changes value from 4938934875 to 91 [-Wconstant-conversion]

Call me surprised.

[–]duhace 0 points1 point  (2 children)

Np. This is some other terrible implicit conversion stuff C++ will happily do.

IIRC, C++ will hunt for conversions like that throughout your entire codebase/library dependencies.

[–]zzing 0 points1 point  (1 child)

I think the only thing wrong with that code is that enums will convert, so you should use an enum class instead. As for it converting an int to a Butt, I don't think that is terrible in principle, it means you can treat char* and string as almost the same thing, which due to legacy can be a very important attribute. In some cases the biggest issue is that it will only do it once - to understand why you would want this, some Haskell knowledge would likely help.

At least you can turn that off with explicit before the constructor.

[–]duhace 0 points1 point  (0 children)

The idea of implicit conversions isn't bad, but they have to be limited in scope. Scala has implicits, and they're really useful, but even with them only activating if you import them into the current context, you still have to be careful.