This is too long, but it might be informative.
Generics in Java are a type erasure mechanism, and automatic code generation of type casts and type checks.
template
s in C++ are code generation and pattern matching mechanisms.
You can use C++ template
s to do what Java generics do with a bit of effort. std::function< A(B) >
behaves in a covariant/contravariant fashion with regards to A
and B
types and conversion to other std::function< X(Y) >
.
But the primary design of the two is not the same.
A Java List<X>
will be a List<Object>
with some thin wrapping on it so users don't have to do type casts on extraction. If you pass it as a List<? extends Bar>
, it again is getting a List<Object>
in essence, it just has some extra type information that changes how the casts work and which methods can be invoked. This means you can extract elements from the List
into a Bar
and know it works (and check it). Only one method is generated for all List<? extends Bar>
.
A C++ std::vector<X>
is not in essence a std::vector<Object>
or std::vector<void*>
or anything else. Each instance of a C++ template
is an unrelated type (except template pattern matching). In fact, std::vector<bool>
uses a completely different implementation than any other std::vector
(this is now considered a mistake because the implementation differences "leak" in annoying ways in this case). Each method and function is generated independently for the particular type you pass it.
In Java, it is assumed that all objects will fit into some hierarchy. In C++, that is sometimes useful, but it has been discovered it is often ill fitting to a problem.
A C++ container need not inherit from a common interface. A std::list<int>
and std::vector<int>
are unrelated types, but you can act on them uniformly -- they both are sequential containers.
The question "is the argument a sequential container" is a good question. This allows anyone to implement a sequential container, and such sequential containers can as high performance as hand-crafted C code with utterly different implementations.
If you created a common root std::container<T>
which all containers inherited from, it would either be full of virtual
table cruft or it would be useless other than as a tag type. As a tag type, it would intrusively inject itself into all non-std
containers, requiring that they inherit from std::container<T>
to be a real container.
The traits approach instead means that there are specifications as to what a container (sequential, associative, etc) is. You can test these specifications at compile time, and/or allow types to note that they qualify for certain axioms via traits of some kind.
The C++03/11 standard library does this with iterators. std::iterator_traits<T>
is a traits class that exposes iterator information about an arbitrary type T
. Someone completely unconnected to the standard library can write their own iterator, and use std::iterator<...>
to auto-work with std::iterator_traits
, add their own type aliases manually, or specialize std::iterator_traits
to pass on the information required.
C++11 goes a step further. for( auto&& x : y )
can work with things that where written long before the range-based iteration was designed, without touching the class itself. You simply write a free begin
and end
function in the namespace that the class belongs to that returns a valid forward iterator (note: even invalid forward iterators that are close enough work), and suddenly for ( auto&& x : y )
starts working.
std::function< A(B) >
is an example of using these techniques together with type erasure. It has a constructor that accepts anything that can be copied, destroyed, invoked with (B)
and whose return type can be converted to A
. The types it can take can be completely unrelated -- only that which is required is tested for.
Because of std::function
s design, we can have lambda invokables that are unrelated types that can be type-erased into a common std::function
if needed, but when not type erased their invokation action is known from there type. So a template
function that takes a lambda knows at the point of invokation what will happen, which makes inlining an easy local operation.
This technique is not new -- it was in C++ since std::sort
, a high level algorithm that is faster than C's qsort
due to the ease of inlining invokable objects passed as comparators.
In short, if you need a common runtime type, type erase. If you need certain properties, test for those properties, don't force a common base. If you need certain axioms to hold (untestable properties), either document or require callers to claim those properties via tags or traits classes (see how the standard library handles iterator categories -- again, not inheritance). When in doubt, use free functions with ADL enabled to access properties of your arguments, and have your default free functions use SFINAE to look for a method and invoke if it exists, and fail otherwise.
Such a mechanism removes the central responsibility of a common base class, allows existing classes to be adapted without modification to pass your requirements (if reasonable), places type erasure only where it is needed, avoids virtual
overhead, and ideally generates clear errors when properties are found to not hold.
If your ENGINE
has certain properites it needs to pass, write a traits class that tests for those.
If there are properties that cannot be tested for, create tags that describe such properties. Use specialization of a traits class, or canonical typedefs, to let the class describe which axioms hold for the type. (See iterator tags).
If you have a type like ENGINE_BASE
, don't demand it, but instead use it as a helper for said tags and traits and axiom typedefs, like std::iterator<...>
(you never have to inherit from it, it simply acts as a helper).
Avoid over specifying requirements. If usually_important
is never invoked on your Worker<X>
, probably your X
doesn't need a b
in that context. But do test for properties in a way clearer than "method does not compile".
And sometimes, just punt. Following such practices might make things harder for you -- so do an easier way. Most code is written and discarded. Know when your code will persist, and write it better and more extendably and more maintainably. Know that you need to practice those techniques on disposable code so you can write it correctly when you have to.