[英]why is RAND_MAX a macro in C++?
Why is it not a const
? 为什么不是
const
? I think this is not a clear C++ way. 我认为这不是一个清晰的C ++方法。 Perhaps there is a more C++ way to generate random numbers, is there?
也许还有一种更多的C ++方式来生成随机数,是吗?
RAND_MAX
comes from the C standard library, where it is defined as a macro. RAND_MAX
来自C标准库,在这里它被定义为宏。
In C, macros are the way manifest constants are defined. 在C语言中,宏是定义清单常量的方式。 A
const
object isn't actually a constant in C (this means a const
object cannot be used in constant expressions). const
对象实际上不是C中的常量(这意味着const
对象不能在常量表达式中使用)。
If you're looking for a "more C++-way", you could use boost::random
. 如果您正在寻找“更多的C ++方式”,则可以使用
boost::random
。
Anyway, RAND_MAX
is a macro, because it comes from "legacy C" rand()
function, where using preprocessor symbols for declaring constants was a de-facto standard. 无论如何,
RAND_MAX
是一个宏,因为它来自“传统C” rand()
函数,在该函数中,使用预处理器符号声明常量是事实上的标准。
It's a macro because it comes from C where it's been a macro for a long time. 这是一个宏,因为它来自C,在很长一段时间以来一直是宏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.