const 其实是编译器判断实现的常量,是一个假常量,在实际中,使用const定义一个常量,最终还是一个变量,只是在编译器内进行了检查,发现有修改则报错。出于这种原因,const其实是可修改的(全局const变量则不能修改,因为是在常量区的)。
当然,如果const变量的值是在编译时就可确定的,如const int a = 5; 同时代码中没有显式获取该变量的地址,那么编译器也会将其优化成一个真常量
c11 draft n1570
6.3.2 Other operands
6.3.2.1 Lvalues, arrays, and function designators
1 An lvalue is an expression (with an object type other than void) that potentially designates an object; if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
c++14 draft n4296
3.10 Lvalues and rvalues [basic.lval]
1 Expressions are categorized according to the taxonomy in Figure 1.
(1.1) — An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment
expression) designates a function or an object. [ Example: If E is an expression of pointer type, then
*E is an lvalue expression referring to the object or function to which E points. As another example,
the result of calling a function whose return type is an lvalue reference is an lvalue. — end example ]
(1.2) — An xvalue (an “eXpiring” value) also refers to an object, usually near the end of its lifetime (so that its
resources may be moved, for example). Certain kinds of expressions involving rvalue references (8.3.2)
yield xvalues. [ Example: The result of calling a function whose return type is an rvalue reference to
an object type is an xvalue (5.2.2). — end example ]
(1.3) — A glvalue (“generalized” lvalue) is an lvalue or an xvalue.
(1.4) — An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment
expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated
with an object.
(1.5) — A prvalue (“pure” rvalue) is an rvalue that is not an xvalue. [ Example: The result of calling a function
whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is
also a prvalue. — end example ]
Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue,
or prvalue. This property of an expression is called its value category. [ Note: The discussion of each built-in
operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it
expects. For example, the built-in assignment operators expect that the left operand is an lvalue and that
the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and
the categories of values they expect and yield are determined by their parameter and return types. — end
note ]