c – 是否有等价的#if可以评估模板参数?
发布时间:2020-12-16 10:03:14 所属栏目:百科 来源:网络整理
导读:#if预处理器指令评估预处理器已知的“常量”的表达式.有没有类似的可以评估模板参数的表达式? 在实践中,我有这样的事情: templateint i class Elem{ /*...*/};#ifndef NUM_ELEM#define NUM_ELEM 2 #endif class MyClass{ #if NUM_ELEM = 1 Elem1 e_1; #end
|
#if预处理器指令评估预处理器已知的“常量”的表达式.有没有类似的可以评估模板参数的表达式?
在实践中,我有这样的事情: template<int i>
class Elem{ /*...*/};
#ifndef NUM_ELEM
#define NUM_ELEM 2
#endif
class MyClass
{
#if NUM_ELEM >= 1
Elem<1> e_1;
#endif
#if NUM_ELEM >= 2
Elem<2> e_2;
#endif
#if NUM_ELEM >= 3
Elem<3> e_3;
#endif
/*...*/
}
但我真的想让MyClass成为一个模板: template<int num_elem>
MyClass{
#if num_elem >= 1 //but #if can't understand num_elem
Elem<1> e_1;
#endif
#if num_elem >= 2
Elem<2> e_2;
#endif
#if num_elem >= 3
Elem<3> e_3;
#endif
/*...*/
};
解决方法
简短的回答是否定的,但如果你愿意稍微改变一下你的要求,你可以这样做:
// A run of elements - Elem<n>,...,Elem<2>,Elem<1>
template <int n> class NumElems
{
template <int u,int v> friend class ElemGetter;
NumElems<n-1> before;
public:
Elem<n> e;
// method to retrieve an element by number
template <int m> Elem<m> &getElem();
};
// helper class to retrieve an element.
// 'n' is the element number to retrieve
// 'm' is the number of elements
// by default,ElemGetter<n,m> defers to ElemGetter<n,m-1>.
template <int n,int m> class ElemGetter
{
public:
static Elem<n> &getElem(NumElems<m> &numElems)
{
return ElemGetter<n,m-1>::getElem(numElems.before);
}
};
// specialisation of ElemGetter: if the element to get is the same as the
// number of elements (i.e. is the last element) then return it
// immediately.
template <int n> class ElemGetter<n,n>
{
public:
static Elem<n> &getElem(NumElems<n> &numElems)
{
return numElems.e;
}
};
// get an element by number; defers to the ElemGetter helper.
template <int n> template <int m> Elem<m> &NumElems<n>::getElem()
{
return ElemGetter<m,n>::getElem(*this);
}
template <> class NumElems<0>
{
};
…然后您可以声明您的Elem成员集: NumElems<NUM_ELEM> elems; 您可以使用以下方式访问它们: Elem<2> &e = elems.getElem<2>(); 原始提议的代码 我提出的原始代码实际上并没有编译,但我会在这里包含它,因为它更好地展示了上述的意图: // Original,doesn't compile - but it would be nice if it did :/
template <int n> class NumElems : private NumElems<n-1>
{
Elem<n> e;
template <int m> Elem<m> &getElem()
{
return NumElems<n-1>::getElem<m>();
}
template <> Elem<n> &getElem<n>()
{
return e;
}
};
template <> class NumElems<0>
{
};
不幸的是,C不允许以这种方式对成员模板函数进行专门化,虽然它(我)为什么不明白 – 代码肯定更简单,无需创建辅助类,如上面的工作代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
