为什么本地类中的字段不能是静态的?
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
编辑:但是,静态
成员函数是允许的。对于这种场景,我有一个用例。假设我希望仅针对 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
> 是允许的),但不适用于其他数据类型。这只是我想到的一个用例。
void foo (int x)
{
struct A { static const int d = 0; }; // error
}
Other than the reference from standard, is there any motivation behind this to disallow static
field inside an inner class ?
error: field `foo(int)::A::d' in local class cannot be static
Edit: However, static
member functions are allowed. I have one use case for such scenario. Suppose I want foo()
to be called only for PODs then I can implement it like,
template<typename T>
void foo (T x)
{
struct A { static const T d = 0; }; // many compilers allow double, float etc.
}
foo()
should pass for PODs only (if static
is allowed) and not for other data types. This is just one use case which comes to my mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为,类的
static
成员需要在全局作用域中定义,例如foo.h
foo.cpp
,因为
void foo(int x)
内部的作用域是局部的该函数,没有范围来定义其静态成员。Because,
static
members of a class need to be defined in global a scope, e.g.foo.h
foo.cpp
Since the scope inside
void foo(int x)
is local to that function, there is no scope to define itsstatic
member[s].Magnus Skog 给出了真正的答案:静态数据成员只是一个声明;该对象必须在命名空间范围内的其他地方定义,并且类定义在命名空间范围内不可见。
请注意,此限制仅适用于静态数据成员。这意味着有一个简单的解决方法:
这为您提供了完全相同的功能,但代价是
使用函数语法来访问它。
Magnus Skog has given the real answer: a static data member is just a declaration; the object must be defined elsewhere, at namespace scope, and the class definition isn't visible at namespace scope.
Note that this restriction only applies to static data members. Which means that there is a simple work-around:
This provides you with exactly the same functionality, at the cost of
using the function syntax to access it.
因为没有人认为有必要吗?
[编辑]:静态变量只需定义一次,通常在类外部(内置变量除外)。允许它们出现在本地类中还需要设计一种方法来定义它们。 [/edit]
添加到语言中的任何功能都是有代价的:
有时,不实现某个功能是正确的决定。
本地函数和类已经给语言增加了难度,但收效甚微:可以使用
静态
函数和未命名的命名空间来避免它们。坦率地说,如果我必须做出决定,我会完全删除它们:它们只会扰乱语法。
举一个例子:最令人烦恼的解析。
Because nobody saw any need for it ?
[edit]: static variables need be defined only once, generally outside of the class (except for built-ins). Allowing them within a local class would require designing a way to define them also. [/edit]
Any feature added to a language has a cost:
Sometimes, not implementing a feature is the right decision.
Local functions, and classes, add difficulty already to the language, for little gain: they can be avoided with
static
functions and unnamed namespaces.Frankly, if I had to make the decision, I'd remove them entirely: they just clutter the grammar.
A single example: The Most Vexing Parse.
我认为这也是阻止我们在模板实例化中使用本地类型的命名问题。
名称
foo()::A::d
不是一个适合链接器解析的好名称,那么它应该如何找到静态成员的定义呢?如果函数 baz() 中还有另一个结构体 A 怎么办?I think this is the same naming problem that has prevented us from using local types in template instantiations.
The name
foo()::A::d
is not a good name for the linker to resolve, so how should it find the definition of the static member? What if there is another struct A in function baz()?有趣的问题,但我很难理解为什么你想要本地类中的静态成员。静态通常用于在程序流中维护状态,但在这种情况下,使用作用域为 foo() 的静态变量不是更好吗?
如果我必须猜测为什么存在这种限制,我会说这与编译器难以知道何时执行静态初始化有关。 C++ 标准文档可能提供更正式的理由。
Interesting question, but I have difficulty understanding why you'd want a static member in a local class. Statics are typically used to maintain state across program flow, but in this case wouldn't it be better to use a static variable whose scope was
foo()
?If I had to guess why the restriction exists, I'd say it was something to do with the difficulty for the compiler in knowing when to perform the static initialisation. The C++ standards docs might provide a more formal justification.
只是因为。
C++ 的一个令人烦恼的事情是,它强烈依赖“全局上下文”概念,其中所有内容都必须唯一命名。即使是嵌套的命名空间机制也只是字符串欺骗。
我想(只是一个大胆的猜测)一个严重的技术问题是与为 C 设计的链接器一起工作,并且只是进行了一些调整以使它们与 C++ 一起工作(并且 C++ 代码需要 C 互操作性)。
如果能够获取任何 C++ 代码并“包装它”以便能够在较大的项目中使用它而不会发生冲突,那就太好了,但由于链接问题,情况并非如此。我认为在函数级别禁止静态或非内联方法(甚至嵌套函数)没有任何合理的哲学原因,但这就是我们(目前)得到的。
即使是声明/定义的二元性及其令人讨厌的冗长和含义也只是与实现问题有关(并且能够在不提供源代码的情况下出售可用的目标代码,这种东西现在由于充分的原因而不太受欢迎)。
Just because.
One annoying thing about C++ is that there's a strong dependence on a "global context" concept where everything must be uniquely named. Even the nested namespaces machinery is just string trickery.
I suppose (just a wild guess) that one serious technical issue is working with linkers that were designed for C and that just got some tweak to get them working with C++ (and C++ code needs C interoperability).
It would be nice to be able to get any C++ code and "wrap it" to be able to use it without conflicts in a larger project, but this is not the case because of linkage problems. I don't think there is any reasonable philosophical reason for forbidding statics or non-inline methods (or even nested functions) at the function level but this is what we got (for now).
Even the declaration/definition duality with all its annoying verbosity and implications is just about implementation problems (and to give the ability to sell usable object code without providing the source, something that is now a lot less popular for good reasons).