做所有 C++编译器允许使用 static const int 类成员变量作为数组边界吗?
在 VC++ 中,当我需要为类成员变量指定数组绑定时,我这样做:(
class Class {
private:
static const int numberOfColors = 16;
COLORREF colors[numberOfColors];
};
请不要告诉我这里使用 std::vector )
这样我就有一个可以用作数组绑定的常量稍后在类代码中指定循环语句约束,同时它在其他任何地方都不可见。
问题是这种 static const int 成员变量的使用是否只被 VC++ 允许,还是通常被其他广泛使用的编译器所允许?
In VC++ when I need to specify an array bound for a class member variable I do it this way:
class Class {
private:
static const int numberOfColors = 16;
COLORREF colors[numberOfColors];
};
(please don't tell me about using std::vector here)
This way I have a constant that can be used as an array bound and later in the class code to specify loop-statement constraints and at the same time it is not visible anywhere else.
The question is whether this usage of static const int
member variables only allowed by VC++ or is it typically allowed by other widespread compilers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是有效的 C++,大多数(全部?)相当现代的编译器都支持它。如果您使用 boost,则可以以
BOOST_STATIC_CONSTANT
宏的形式获得对此功能的可移植支持:如果编译器支持,该宏将扩展为
static const int numberOfColors = 16
否则它会求助于enum { numberOfColors=16 };
。This is valid C++ and most (all?) reasonably modern compilers support it. If you are using boost, you can get portable support for this feature in the form of
BOOST_STATIC_CONSTANT
macro:The macro is expanded to
static const int numberOfColors = 16
if the compiler supports this, otherwise it resorts toenum { numberOfColors=16 };
.根据 C++ 标准,该行为是有效的。任何最新的编译器都应该支持它。
That behavior is valid according to the C++ Standard. Any recent compiler should support it.
我相信 Visual Studio 2005 及更高版本都支持它。还有 XCode C++ 编译器(实际上是 gcc)。
如果你想安全,你可以随时使用我从Effective C++ 中学到的旧枚举技巧。它是这样的:
希望这有帮助。
I believe that Visual Studio 2005 and beyond supports it. The XCode C++ compiler as well (this is gcc actually).
If you want to be safe you could always use the old enum hack that I learned from Effective C++. It goes like this:
Hope this helps.
十多年来,它一直是 C++ 的标准。它甚至得到了 VC 的支持——你还想要什么呢? (@Neil:SunCC 怎么样?
:^>
)This has been standard C++ for more than a decade now. It's even supported by VC -- what more could you want? (@Neil: What about SunCC?
:^>
)是的,它 100% 合法并且应该是便携式的。 C++ 标准在 5.19 - 常量表达式中说了这一点”(强调我的):
也就是说,VC6 似乎不支持它。请参阅StackedCrooked 的回答提供了一个很好的解决方法。事实上,我通常更喜欢 StackedCrooked 提到的这种类型的
enum
方法。仅供参考,“
static const
”技术适用于 VC9、GCC 3.4.5 (MinGW)、Comeau 和 Digital Mars。并且不要忘记,如果您使用“static const”成员,您将 除了声明之外还需要一个定义。然而,在这种情况下,几乎所有编译器都会让您跳过定义。
Yes, it's 100% legal and should be portable. The C++ standard says this in 5.19 - Constant expressions" (emphasis mine):
That said, it appears that VC6 doesn't support it. See StackedCrooked's answer for a good workaround. In fact, I generally prefer the
enum
method StackedCrooked mentions for this type of thing.As an FYI, the "
static const
" technique works in VC9, GCC 3.4.5 (MinGW), Comeau and Digital Mars.And don't forget that if you use a "`static const'" member, you'll need a definition for it in addition to the declaration strictly speaking. However, virtually all compilers will let you get away with skipping the definition in this case.
除了其他答案之外,您还可以使用以下函数来确定静态分配数组中的元素数量:
Besides other answers you can use following function do determine number of elements in statically alocated arrays:
我非常确定这也适用于 gcc 和 Solaris,但目前我无法验证这一点。
将来,您可以像这样扩展这个想法:
并像这样使用它:
这样您就不会仅限于应用程序中的一个缓冲区大小。
I'm pretty sure that this will also work with gcc and Solaris, but I can't verify this at the moment.
In the future you could extend the idea like this:
and use it like this:
so that you are not limited to exactly one buffer size in your application.
几年前我就不再担心它的可移植性了。也许仍然有不支持它的编译器,但我最近还没有遇到过它们。
I've stopped bothering about the portability of that years ago. There are perhaps still compilers which don't support it, but I haven't met any of them recently.
可以通过参考 ISO C++ 规范来回答这样的问题,但该规范对于人们来说很难获得,也很难阅读。
我认为最简单的答案取决于两件事:
It is possible to answer questions like this by referencing the ISO C++ speicifcation but the spec is hard for people to get and harder to read.
I think the simplest answer hinges on two things: