Indeed, they just work. They use some fairly simple template properties, sometimes called static polymorphism. If you are familiar with the term, it is essentially a duck shape. (If it looks like a duck and it dips like a duck, it must be a duck)
The trick is simple. Here is a very simple example:
template <typename T>
void say_hello(const T& t) {
t.hello();
}
say_hello , . - "promises" , . , , - , . , , hello. , , hello.
STL . std::for_each:
template <typename iter_type, typename func_type>
void for_each(iter_type first, iter_type last, func_type f){
for (iter_type cur = first; cur != last; ++cur) {
f(*cur);
}
}
, , ; iter_type pre-increment ++ -. , ! =, * -dereference.
func_type -, , , iter_type. for_each , , . iter_type , . , " , ". , , .