In Java you can define generic class that accepts only types that extend a class of your choice:
public class ObservableList<T extends List> {
/* ... */
}
This is done using extends keyword.
Is there some simple equivalent to this keyword in C++?
I suggest using Boost’s static assert feature in concert with
is_base_offrom the Boost Type Traits library:In some other, simpler cases, you can simply forward-declare a global template, but only define (explicitly or partially specialise) it for the valid types:
[Minor EDIT 6/12/2013: Using a declared-but-not-defined template will result in linker, not compiler, error messages.]