When inheriting from a class, you know the requirements and restrictions imposed on you by simply looking up the hierarchy. For example, you can find out just by looking at the parent class(es) which methods are required to be defined (pure virtual). If one chooses to use templates instead (if it fits the bill) then things are not as clear. The methods of the template class may be expecting several methods for the type to be present.
I ran across a complex class that I have to use. I did not know what methods my type (class/object) was supposed to have. The only way I could find out was by compiling where the compiler would let me know which methods were expected. It’s like inheriting from an abstract class without access to the header file. This is a bit of a pain as it is time consuming to figure out the method specifications (signature).
Now the original designer of the class could have put some documentation as to which methods are expected to be present and their signatures, but I can’t help but wonder if there is a way the code can be self documented (or is already and I don’t know how to view it?). Compiling and looking at the errors can’t be the only way, can it?
You’re essentially describing the kind of problem that C++0x concepts was going to solve by providing programmers a way to enforce requirements on
typeat compile time. However, it was removed in July 2009 due to time constraints.There is a library-only solution in Boost called the Concept Check Library (BCCL). However, there are differences between the BCCL and what C++0x concepts was going to be. It’s the closest thing to concepts we have for the current language.
If you much rather not have to use the BCCL, then documentation is the next best thing.