I'm not my father, nor my mother. Inheritance shouldn't be used at all in this case. People who advocate MI usually don't get even the single inheritance right and you are a great example of it.
In many serious companies doing C++ development, using of multiple inheritance is a worse crime than using a goto.
Can't seem recall a java dev saying something like that. |
I was saying many times on this forum, that you shouldn't use Java for low-latency applications requiring to run with very little memory. E.g. software running on microcontrollers for controlling machines in a factory. These are usually simple algorithms that can be perfectly coded in assembly, C or PLC ladder language.
You also shouldn't use Java-the-language for writing complex algorithmic stuff, because Scala does it much better.
As for the enable_if hack, I know of it, but it is merely an ugly hack and doesn't provide all the goodness you get from generic bounds in Java. It just moves the problem one step further away, but doesn't solve it.
1 2 3 4 5 6 7 8
|
public class <T> SomeClass {
public <U extends SomeOtherClass> U process(U object) { ... }
public void someCode() {
T item = ...;
process(item);
}
| |
Such code when translated to C++ using enable_if, would compile, because actual generic typechecking is moved to the phase when all types are concretized. In Java that would fail with compile error. C++ templates are not a part of the type system and C++ compilers can't reason about generic types. That's why you can't be sure if your generic code is ok from the typesystem point of view until someone concretizes it. Actually, if you get syntax right, template code compiles just fine in most of cases - there is no type checking at that time. Exactly the same as old C macros, with a little exception that at least in C++ it checks the syntax and obey namespace scope.
A side effect of that (besides accepting broken template code as correct) is impossibility to get nice tool features like intellisense or type-aware error highlighting in generic code.
1 2 3 4
|
<typename T>
void method(T item even with all the enable_if ugly stuff) {
item. // what your IDE offers here?
}
| |
Java generics have been significantly improved in Scala. It could be easy to translate STL API to Scala, but it would be impossible to translate Scala collections API to C++. Lack of type bounds are not the only problem, but also lack of perfect forwarders, lack of variance control, type constructors etc.
I also tend to notice java devs cursing features that exist in c++ that does not exist in java, but praise features that exist only in java. |
Features that exist only in Java? Nooo... Can't be. Many times I hear Java feature set is a subset of C++ features... :D But I understand where it comes from. Usually people start with C++ (college) then learn Java (industry). You immediately notice the missing features, while learning the new ones take some time. I know of one guy who went the other way, from Java to C++, and he says something like that:
"ok, so now I must admit C++ is not the C++ I learned at college. It is far better, more intuitive and faster to code, with all the Google goodness we use here. However, I'm still nowhere near that productive that I can be with even plain Java (leave alone Scala for a while)"