如果我有一点点代码......
template <typename _T>
class Foo
{
public:
typedef const T& ParamType;
void DoStuff(ParamType thingy);
};
Run Code Online (Sandbox Code Playgroud)
如果这可能是非最佳的sizeof(_T) <= sizeof(_T*).
因此,我希望有条件typedef.如果大小_T小于或等于指针的大小,则只需将其传递给值.否则,通过const引用传递它.这可能吗?我听到所有这些关于模板完成图案的东西,但这让我感到很伤心.
我这里有这个问题,我无法弄清楚如何解决.我想要一个模板类,它接受一个整数作为模板参数,并相应地设置另一个类的模板参数:
template <int T>
class Solver
{
public:
#if T <= 24
typedef MyMatrix<float> Matrix;
#else if T <= 53
typedef MyMatrix<double> Matrix;
#else
typedef MyMatrix<mpreal> Matrix;
#endif
Matrix create();
};
Run Code Online (Sandbox Code Playgroud)
然后像这样调用它:
Solver<53>::Matrix m = Solver<53>::create();
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?目前使用上面的代码,编译器抱怨它不知道"Matrix",所以我不确定你是否可以在模板参数上使用预处理器.
我想根据a的值制作预处理器定义typedef.
我们的想法是检查index_type_t并生成适当的INDEX_TYPE_GL定义.
以下内容无法正常工作.
typedef uint32_t index_type_t;
#ifdef INDEX_TYPE_GL
#undef INDEX_TYPE_GL
#endif
#if (index_type_t == uint8_t)
#define INDEX_TYPE_GL GL_UNSIGNED_BYTE
#elif (index_type_t == uint32_t)
#define INDEX_TYPE_GL GL_UNSIGNED_INT
#elif (index_type_t == uint16_t)
#deine INDEX_TYPE_GL GL_UNSIGNED_SHORT
#endif
Run Code Online (Sandbox Code Playgroud)
请注意uint8_t,uint16_t并且uint32_t在typdefs其他地方是分开的,GL_UNSIGNED_BYTE等等是整数值,而不是类型.