tags:

views:

91

answers:

2

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

A: 

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.

Scharron
+5  A: 

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)
{
    // ...
}
Alexandre C.
Note that the term is *injected class name* and the original template can be accessed using a qualified name like `::matrix`.
Georg Fritzsche