is there a difference, from prospective of meta-programming for example, between the two declarations?
template<typename T>
struct matrix {
typedef matrix self_type; // or
typedef matrix<T> self_type;
};
thank you
is there a difference, from prospective of meta-programming for example, between the two declarations?
template<typename T>
struct matrix {
typedef matrix self_type; // or
typedef matrix<T> self_type;
};
thank you
They both refer to the same type. The only difference I can see is from a performance perspective: I suppose (depending on the compiler implementation), matrix alone is refering about the class itself, therefore it does not need anything from the environnement. matrix is talking about the templated class matrix templated by T (that is, matrix), therefore it perhaps need more deduction mechanism from the compiler. But I'm just guessing, I never read anything about that.
In this particular situation (inside a class template), matrix
is a shorthand for matrix<T>
. When you write lots of hairy templates all day long while trying to fit everything in 80 columns, the 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)
{
// ...
}