December 2001
Intermediate to advanced
304 pages
6h 25m
English
Difficulty: 7
This Item reviews traits templates and demonstrates some cool traits techniques. Even without traits, what can a template figure out about its type—and what can it do about it? The answers are nifty and illuminating, and not just for people who write C++ libraries.
What is a traits class?
Demonstrate how to detect and make use of template parameters' members, using the following motivating case: You want to write a class template C that can be instantiated only on types that have a const member function named Clone() that takes no parameters and returns a pointer to the same kind of object.
// T must provide T* T::Clone() const
template<typename T>
class C
{
// ...
};
Note: ...
Read now
Unlock full access