How the hell classes work and what do they do?

I am reading Scott Meyers Performance C ++ . He talks about the dash classes, I realized that I need them to determine the type of the object at compile time, but I can’t understand his explanation of what these classes actually do? (from a technical point of view)

+61
c ++ traits
Oct 20 '10 at 15:50
source share
2 answers

Perhaps you expect some kind of magic that makes character traits work. In this case, be disappointed - there is no magic. Type types are manually defined for each type. For example, consider iterator_traits , which provides typedefs (e.g. value_type ) for iterators.

Using them, you can write

 iterator_traits<vector<int>::iterator>::value_type x; iterator_traits<int*>::value_type y; // `x` and `y` have type int. 

But to make this work, there is actually an explicit definition somewhere in the <iterator> header that reads something like this:

 template <typename T> struct iterator_traits<T*> { typedef T value_type; // … }; 

This is a partial specialization of the iterator_traits type for T* type types, that is, pointers of some general type.

In the same vein, iterator_traits specialized for other iterators, for example. typename vector<T>::iterator .

+49
Oct. 20 '10 at 18:18
source share

Classes do not determine the type of object. Instead, they provide additional information about the type, usually by defining typedefs or constants within the attribute.

+42
Oct 20 2018-10-10
source share



All Articles