我想在不改变其作用域的情况下为类启用静态成员。 考虑下面的抽象例如:在不改变成员范围的情况下有条件地启用静态成员
template<uint R, uint C>
class Foo
{
static Foo ID;
/* other members */
};
现在我想用静态成员,如:
Foo<3, 3> foo = Foo<3, 3>::ID;
的问题是,ID
场只能存在R == C
时。
(Foo
实际上是一个Matrix
和ID
其身份其中仅存在对于方阵)
所以,我有当所述条件满足时有条件地使静态ID
构件。我目前的解决办法是这样的:
struct EmptyBase { };
template<uint R, uint C>
class _Foo_Square
{
static Foo<R, C> ID;
};
template<uint R, uint C>
class Foo : public std::conditional<R == C, _Foo_Square<R, C>, EmptyBase>::type
{
/* other members */
};
但现在我不能写Foo<3, 3>::ID
访问它。我必须写_Foo_Square<3, 3>::ID
。
不幸的是,我的应用程序的设计强迫它可以通过Foo
类作用域访问。 如果不是条件成员,我可以在Foo类中编写using _Foo_Square<3, 3>::ID;
。
有没有解决这个问题的方法?
子类似乎是个好主意。 – erip
“*但是现在我不能写'Foo <3, 3> :: ID'来访问它,我必须写'_Foo_Square <3, 3> :: ID'。*”要从哪里访问它?正如你所示,唯一的“问题”是“ID”是私人的...... – ildjarn
是的,你的代码在你公开的情况下工作:http://coliru.stacked-crooked.com/a/0ceb5c6660a0fd8b –