什么时候const被认为真的是const? [英] when the const is considered really a const?

查看:78
本文介绍了什么时候const被认为真的是const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读stackoverflow中的一些问题和答案时,我遇到了这个问题

While reading some questions and answers in stackoverflow I come across this question

我试图理解它,但答案确实很难理解,尤其是像

I tried to understand it but the answers were really hard to understand especially the terms like


  • 静态存储时间

表达式不能在翻译阶段

等...

此外,我认为常量始终是常量(这是我从学校中学到的东西)

Besides, I thought that constants are always constants (This is what I learnt from school)

请有人可以使其变得容易理解吗?

Please can someone makes it a little bit easy to understand ?

推荐答案

在C语言中(不同于C ++),仅当表达式中的每个值都是数字常量或枚举值的名称。也就是说,尽管您可能已将变量声明为静态const int ,但仍不能在常量算术表达式中使用该(常量)变量。

In C (unlike C++), an arithmetic expression is a "constant expression" only if every value in the expression is a numeric constant or the name of an enumeration value. That is, although you might have declared a variable to be a static const int, you still cannot use that (constant) variable in a constant arithmetic expression.

请注意,常量表达式是由定义C语言的形式标准定义的短语。还有其他一些表达式在直观上是常量,但未包含在正式定义中。

Note that "constant expression" is a phrase defined by the formal standard which defines the C language. There are other expressions which are intuitively constant, but they are not included in the formal definition.

具有静态存储持续时间的变量只是存在于整个变量中的变量。执行程序。大多数此类变量是全局变量(即,不是任何函数的一部分,甚至不是 main 的一部分),但是在C和C ++中,您可以具有 static 函数范围内的变量。这样的变量仅初始化一次,并且无论该函数被调用多少次,都仅存在一个实例。

A variable with "static storage duration" is simply a variable which exists throughout the execution of the program. Most such variables are global variables (i.e. not part of any function, not even main), but in C and C++ you can have a static variable inside the scope of a function. Such a variable is initialized only once, and only a single instance of it exists regardless of how many times the function is called.

全局变量和其他具有静态存储的变量持续时间,只能根据上述定义初始化为常量表达式。 是否为都是 const 变量。问题很简单,因为变量具有静态存储持续时间,这意味着必须在程序执行之前将其初始化。 (具有静态存储持续时间的变量在程序的整个执行过程中都存在,因此,如果将其初始化(即,赋予初始值,而不是在程序执行期间为其赋值),

Global variables, and other variables with static storage duration, can only be initialized to a constant expression as per the above definition. This is the case whether or not they are const variables. The issue is simply that the variables have static storage duration, which means that they must be initialized before the program executes. (A variable with static storage duration exists throughout the execution of the program, so if it is initialized -- that is, given an initial value, as opposed to being assigned a value during the program's execution -- the initialization must occur before the program executes.)

在C ++中,声明为 static const 的变量是视为常量,因此它可以出现在常量表达式中。但是,在C中不是这种情况,因此C编译器不需要跟踪静态const 变量的初始值。

In C++, a variable declared static const is considered a constant value, so it can appear in constant expressions. In C, however, that is not the case, so a C compiler does not need to track the initial value of static const variables.

这篇关于什么时候const被认为真的是const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆