Typedef declaration of a template class

Is there a difference, for example, from prospective metaprogramming, between two declarations?

template<typename T>
struct matrix {
    typedef matrix self_type;    // or
    typedef matrix<T> self_type;
};

Thank you

+3
source share
2 answers

In this particular situation (inside the class template) matrixis a shorthand for matrix<T>. When you write a lot of hairy patterns all day, trying to fit everything into 80 columns, shorthand is welcome.

Note that you can also abbreviate method arguments:

template <typename T>
struct matrix
{
    typedef matrix my_type;
    matrix(); // constructor is abbreviated too
    matrix& operator=(matrix);
};

// Method argument types can be abbreviated too
// but not result types.
template <typename T>
matrix<T>& matrix<T>::operator=(matrix m)
{
    // ...
}
+6
source

. , , - : ( ), , . , T (.. ), , , , , . , .

0

Source: https://habr.com/ru/post/1755600/


All Articles