Templates and generic programming
T.42
Use template aliases to simplify notation and hide implementation details
Reason
Improved readability. Implementation hiding. Note that template aliases replace many uses of traits to compute a type. They can also be used to wrap a trait.
Example
template<typename T, size_t N>
class Matrix {
// ...
using Iterator = typename std::vector<T>::iterator;
// ...
};
This saves the user of Matrix from having to know that its elements are stored in a vector and also saves the user from repeatedly typing typename std::vector<T>::.
Example
template<typename T>
void user(T& c)
{
// ...
typename container_traits<T>::value_type x; // bad, verbose
// ...
}
template<typename T>
using Value_type = typename container_traits<T>::value_type;
This saves the user of Value_type from having to know the technique used to implement value_types.
template<typename T>
void user2(T& c)
{
// ...
Value_type<T> x;
// ...
}
Note
A simple, common use could be expressed: "Wrap traits!"
Enforcement
- Flag use of
typenameas a disambiguator outsideusingdeclarations. - ???