I am trying to understand what traits are, for example expressions like typedef typename traits_type::off_type off_type
in the GNU implementation of fstream
.
This question came up when I was working with files larger than 2/4 GB. I found that recompiling the STL library, with the appropriate flags usually solves the large file issues.
Traits are a way of "adding" properties to existing types. Let's say we are creating a container type, which contains a typedef to tell its contained data type. The classic way would be:
template <class T>
struct Cont { typedef T contained_type; }
This has the disadvantage that we have to create our class just to contain the typedef - eg. third party containers and basic types cannot be used by code that assumes Cont::contained_type
type. So we introduce a traits struct, which adds an indirection in the process:
template <class C>
struct container_traits; // this struct would contain the contained_type for container C
template <class T>
struct Cont { ... } // no typedef here
template <class T>
struct container_traits<Cont<T> >
{
typedef T contained_type; // by this, we say that the contained_type of Cont<T> is T
};
template <class T, unsigned N>
struct container_traits<T[N]>
{
// this is the advantage of traits - we can add information for arrays, which can have no member typedefs
typedef T contained_type;
};
Alos, the traits template can be a parameter of algorithms using it, which permits us to use different traits with a single data type (see the std::string
class).
That said, I don't believe traits have much to do with 64-bit systems.