我有一个类的大数据成员,它只用于测试:
template <bool testing>
class foo {
int testbuf[1000];
}我怎样才能做到这一点呢?仅当testing为true时,才包含testbuf[]
发布于 2012-11-10 06:27:49
专门化:
template <bool> class foo { };
template <> class foo<true>
{
// everything needed for testing
};更新:澄清评论中提出的一些问题:您可以为每个要专门化的项目编写一个这样的“测试”模板,这样就不会有代码重复。假设您的实际类模板实际上是bar
template <bool Testing>
class bar
: private foo<Testing> // specializable base
{
// common stuff
Widget<Testing> widget; // specializable member
Gadget gadget; // unconditional member
};您也可以使用组合而不是继承;以最适合的方式为准。如果您使用继承,请确保拼写为this->testbuf。
发布于 2012-11-10 06:28:29
你可以使用ifdef的东西
#define DEBUG_MODE
class Foo{
#ifdef DEBUG_MODE
int testbuf[1000];
#else
int testbuf[10];
#endif
}发布于 2012-11-10 06:28:15
template <bool testing>
class foo {
}和
template <>
class foo <true>{
int testbuf[1000];
}https://stackoverflow.com/questions/13316919
复制相似问题