在常量表达式中使用 numeric_limits::max()
我想在类中定义一个常量,其值是最大可能的整数。像这样的:
class A
{
...
static const int ERROR_VALUE = std::numeric_limits<int>::max();
...
}
此声明无法编译并显示以下消息:
numeric.cpp:8: 错误:'std::numeric_limits::max()' 不能出现在常量表达式中 numeric.cpp:8: 错误:函数调用不能出现在常量表达式中
我理解为什么这不起作用,但有两件事对我来说看起来很奇怪:
在我看来,使用该值是一个自然的决定常量表达式。为什么语言设计者决定使 max() 成为一个函数,从而不允许这种用法?
规范在 18.2.1 中声称
<块引用>对于在 numeric_limits 模板中声明为 static const 的所有成员,特化应以可用作整型常量表达式的方式定义这些值。
这是否意味着我应该能够在我的场景中使用它,并且它是否与错误消息相矛盾?
谢谢。
I would like to define inside a class a constant which value is the maximum possible int. Something like this:
class A
{
...
static const int ERROR_VALUE = std::numeric_limits<int>::max();
...
}
This declaration fails to compile with the following message:
numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression
numeric.cpp:8: error: a function call cannot appear in a constant-expression
I understand why this doesn't work, but two things look weird to me:
It seems to me a natural decision to use the value in constant expressions. Why did the language designers decide to make max() a function thus not allowing this usage?
The spec claims in 18.2.1 that
For all members declared static const in the numeric_limits template, specializations shall define these values in such a way that they are usable as integral constant expressions.
Doesn't it mean that I should be able to use it in my scenario and doesn't it contradict the error message?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然当前标准缺乏对整数类型的支持 Boost.IntegerTraits 为您提供编译时间常数
const_min
和const_max
。问题源自§9.4.2/4:
请注意,它添加了:
正如其他人已经提到的那样,numeric_limit s min() 和 max() 根本不是整数常量表达式,即编译时常量。
While the current standard lacks support here, for integral types Boost.IntegerTraits gives you the compile time constants
const_min
andconst_max
.The problem arises from §9.4.2/4:
Note that it adds:
As others already mentioned
numeric_limit
smin()
andmax()
simply aren't integral constant expressions, i.e. compile time constants.看起来有点缺陷...
在 C++0x 中,
numeric_limits
将用constexpr
标记所有内容,这意味着您将能够使用min( )
和max()
作为编译时常量。Looks like a bit of a defect...
In C++0x,
numeric_limits
will have everything marked withconstexpr
, meaning you will be able to usemin()
andmax()
as compile-time constants.您想要:
将类/结构放在标头中,将定义放在 .cpp 文件中。
You want:
Put the class/struct in a header and the definition in a .cpp file.
它并不矛盾,因为
max
没有定义static const
。它只是一个静态成员函数。函数不能是 const,静态成员函数也不能在最右边附加 const。在双精度版本的限制中还有一个
double max()
,在 C++03 中,不能说static double const max = ...
>。因此为了保持一致,max()
是适用于所有版本的限制模板的函数。现在,众所周知,
max()
不能像这样使用是很糟糕的,C++0x 已经通过将其设为constexpr
函数来解决这个问题,允许您建议的用法。It doesn't contradict, because
max
is not definedstatic const
. It's just a static member function. Functions can't be const, and static member functions can't have a const attached at the very right either.There is also a
double max()
in the double version of the limits, and in C++03 it wouldn't work to saystatic double const max = ...
. So to be consistent,max()
is a function for all versions of the limit template.Now, it's known that
max()
not being able to be used like that is bad, and C++0x already solves it by making it aconstexpr
function, allowing your proposed usage.1-如果您希望使用函数初始化程序中的 static const int:
这适用于 VS 2008
2-如果您想获取最大和最小数对于给定的数据类型,然后使用这些定义
INT_MAX、INT_MIN、LONG_MAX 等等。3-
如果您需要使用这些 wrt 模板类型,那么
自己对模板进行硬编码
,然后
像这样调用它们
4- 如果您只处理二进制表示的类型,那么使用这个:
希望
这可以帮助您..
1- If you want a static const int in your program to be initialized with a function:
This works on VS 2008
2- If you want to get max and min number for a given data type, then use these definitions
INT_MAX, INT_MIN, LONG_MAX and so on..
3- If however you need to use these wrt template type, then
hard code the templates yourself
and
and call them like this
4- and if you are only dealing with binary represented types only, then use this:
and this
Hope this can help you..