【发布时间】:2011-05-26 10:48:53
【问题描述】:
void foo (int x)
{
struct A { static const int d = 0; }; // error
}
除了来自标准的参考之外,这背后是否有任何动机来禁止内部类中的 static 字段?
error: field `foo(int)::A::d' in local class cannot be static
编辑:但是,static 成员函数是允许的。对于这种情况,我有一个用例。假设我希望只为 POD 调用 foo(),那么我可以像这样实现它,
template<typename T>
void foo (T x)
{
struct A { static const T d = 0; }; // many compilers allow double, float etc.
}
foo() 应该只传递给 POD(如果允许 static)而不传递给其他数据类型。这只是我想到的一个用例。
【问题讨论】:
-
@Vache:我不认为这是一个骗局(来自那里的一些 OP 的 cmets)。
标签: c++ static-members local-class