好吧,它似乎在Microsoft C中工作,但不是我使用的其他编译器 .
使用模板时尤其需要 . 也许我不知道“标准兼容的方式”来做这个,但它真的很有用,我希望代码可以在gcc,clang等上编译 .
template<class T, class T1, ... /* messy, verbose */ >
class MyTemplate {
protected:
typedef MyTemplate<T,T1, ... /* messy, verbose */ > SimplerName;
// declare a constructor
SimplerName(int arg) { ... }
};
class SubClass
: public MyTemplate<x,y... /* messy */ >
{
public:
SubClass(int arg, blah, blah) : SimplerName(arg) { ... }
}.
我用gcc和emscripten得到了严肃的扒窃
In file included from /home/peterk/didi-wasmtest/build/include/artd/Matrix4Base.h:2:
/home/peterk/didi-wasmtest/build/include/artd/../../../artdlib-cpp/vecmath/Matrix4Base.h:91:21: error: expected member name or ';' after declaration specifiers
inline Matrix4Base() {
~~~~~~~~~~~~~~~~~~ ^
Headers 中的代码
template<class Real, class Super>
class ARTD_API_ARTD_VECMATH Matrix4ColumnMajorT
: public Matrix4CommonT<Real, Super>
{
public:
typedef Matrix4ColumnMajorT<Real, Super> Matrix4Base;
protected:
inline Matrix4Base() {
SType().setIdentity();
}
inline Matrix4Base(bool asIdentity) {
if (asIdentity) SType().setIdentity();
}
inline Matrix4Base(const Super &src) {
SType().set(src);
}
public:
...
};
这一切都在Microsoft C上编译和运行,但是在gcc和基于clang的编译器上使用typedef名称barf声明的所有构造函数 .
我正在尝试使用它的地方,我选择基于宏的基类,但希望子类不必声明两组构造函数或使用宏来定义它们 . 我尝试使用和不使用模板参数的变体 . 似乎需要模板参数 . 此代码在视觉工作室2017上编译并在MSC上生成适当的代码,但我的主要目标是gcc和emscripten(clang)
class ARTD_API_ARTD_VECMATH Matrix4f
#ifdef Matrix4f_RowMajor
: public Matrix4RowMajorT<float, Matrix4f>
#else
: public Matrix4ColumnMajorT<float, Matrix4f>
#endif
{
public:
Matrix4f() : Matrix4Base()
{}
Matrix4f(bool asIdentity) : Matrix4Base(asIdentity)
{}
Matrix4f(const Matrix4f &src) : Matrix4Base(src)
{}
1 回答
模板名称用作类模板本身范围内的注入类名 . 这是标准且有效的:
实例
你甚至不需要子类的别名:
实例