We need to implement a type trait that returns true
if the std::vector
type is passed to it as a template parameter and false
otherwise.
Some basic knowledge of the Boost.TypeTrait
or standard library type traits is required.
Let's see how to implement a type trait:
#include <vector> #include <boost/type_traits/integral_constant.hpp> template <class T> struct is_stdvector: boost::false_type {}; template <class T, class Allocator> struct is_stdvector<std::vector<T, Allocator> >: boost::true_type {};
Almost all the work is done by the boost::true_type
and boost::false_type
classes. The boost::true_type
class has a boolean ::value
static constant in it that equals true
. The boost::false_type
class has a boolean ::value
static constant in it that equals false
. These two classes also have some typedef
s to cooperate well with the Boost.MPL
library.
Our first is_stdvector
structure is a generic...