我有一些像这样的类:
template<int DIMENSION, typename T> Vector { ... }
现在,我想专门化 typename 并使用 typedef 提供一个新类型。因此,我在 StackOverflow 上的 C++ typedef for partial templates 找到了答案。
我这样做了:
template < int DIMENSION> using VectorDouble= Vector<DIMENSION, double>;
这不会编译(错误 C2988:无法识别的模板声明/定义)。这是因为我的编译器 (Visual Studio 2008) 不允许这样做,还是我错过了什么?
谢谢。
最佳答案
下一个标准支持模板类型定义——也就是说,基本上,只有真正、真正、真正的新编译器支持它。即使是最新的 VS 也不支持它,因为它还不够新,这就是新的 C++0x。
在 C++03 中,通常使用结构。
template<int dimension> struct vector_double {
typedef Vector<dimension, double> type;
};
现在可以访问了
int main() {
vector_double<5>::type vec_double;
}
关于c++ - 模板和类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539011/