Type trait implementation
To understand type traits, we’ll look at the classic implementations of boost::is_void
and boost::is_pointer
.
boost::is_void
First, let’s look at one of the simplest traits classes, the is_void
trait, which was created by Boost. It defines a generic template that’s used to implement the default behavior; that is, accept a void
type, but not anything else. Hence, we have is_void::value =
false
:
//primary class template is_voidtemplate< typename T > struct is_void{ static const bool value = false; //default value=false };
Then, we fully specialize it for the void
type, like so:
//"<>" means a full specialization of template class is_voidtemplate<> struct is_void< void >{ //fully specialization for void static const bool value = true; //only true for void type };
Thus, we have a complete traits
type that can be used to detect whether any given type, T
, is void by checking the following expression...