typedef模板类的声明

Any*_*orn 3 c++ templates

从两个声明之间的元编程前景来看,是否存在差异?

template<typename T>
struct matrix {
    typedef matrix self_type;    // or
    typedef matrix<T> self_type;
};
Run Code Online (Sandbox Code Playgroud)

谢谢

Ale*_* C. 6

在这种特殊情况下(在类模板中),matrix是一种简写matrix<T>.当你整天写出大量毛茸茸的模板,同时试图将所有东西都装在80列中时​​,欢迎使用速记.

请注意,您还可以缩写方法参数:

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)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,该术语是*注入类名*,可以使用限定名称(如`:: matrix`)访问原始模板. (5认同)