- 论坛徽章:
- 0
|
本帖最后由 幻の上帝 于 2012-09-25 12:52 编辑
[
编辑1:修正字体。
编辑2:禁用表情。
]
我的观点综述:
(0.以下所有引文的正文部分原样标示表示正式概念的斜体。)
1.变量(variable)是变量,变量名(variable name)是变量名,绝不可能无条件等价。否则就变量名这个概念就没存在的意义。
2.变量名是语言中变量的表达形式,用于引用变量。
鉴于C和C++之间求值方式的相似性,以下主要使用C和C++举例说明。
变量名举例:C中的声明对象的标识符、C++中对象或引用的qualified name和unqualified name(统称为name,是identifier的一种使用)。
不论是C还是C++,声明、作用域(scope)、链接这些概念作用于idntifier上。
一般语言的语法即确定了变量名就是表达式。对于C/C++,可以分别到ISO C/C++的Annex A查阅。
3.变量不仅仅存在于具体语言中。讨论变量时,不总是只讨论形式或语法上的表达。
变量不必然是语言直接抽象的实体(即便C++11里也不是,下述),但变量名应指称实体。指称是人为建立的抽象意义上的描述语义的关系,并非字面上的对语言使用时的引用,可以认为和具体语言的语法(表达式等)无关。
出现在语言外的概念,如int tmp = 1;中tmp叫作“临时变量”,应该可以理解(但这里的“临时”不同于C++中的temporary,后者有确定的含义——是个对象)。
4.关于C++里的variable:
ISO C++03 Clause 3
3 An entity is a value, object, reference, function, enumerator, type, class member, template, template specialization, namespace, parameter pack, or this.
3 An entity is a value, object, subobject, base class subobject, array element, variable, function, instance of a function, enumerator, type, class member, template, or namespace.
4 A name is a use of an identifier (2.10) that denotes an entity or label (6.6.4, 6.1). A variable is introduced by the declaration of an object. The variable’s name denotes the object. ISO C++11 Clause 3
3 An entity is a value, object, reference, function, enumerator, type, class member, template, template specialization, namespace, parameter pack, or this.
4 A name is a use of an identifier (2.11), operator-function-id (13.5), literal-operator-id (13.5.8), conversion-function-id (12.3.2), or template-id (14.2) that denotes an entity or label (6.6.4, 6.1).
5 Every name that denotes an entity is introduced by a declaration. Every name that denotes a label is introduced either by a goto statement (6.6.4) or a labeled-statement (6.1).
6 A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name denotes the reference or object.
首先,可以看出object和variable显然不是一回事。object有storage duration,但自身没有scope或linkage等——只有和声明关联起来才讨论。
表面上C++11已经算迁就了——C++98/C++03里entity包含variable。但是,关于C++中variable的变迁:http://www.open-std.org/jtc1/sc2 ... ers/2009/n2993.html。
根本原因并非是variable不够资格当entity,而是variable已经是两类entity的统称了,不适合继续出现在概念定义中。这并不妨碍variable和某种entity而不是name的直接关联。
还有一点,要在C++使用variable不可能脱离声明,而声明引入name。所以就C++的具体使用来说,只讨论变量而回避变量名是不可能的。还没写成代码之前,或者讨论语言构造的概念直接的关联时,有讨论的价值。
5.关于C的variable和entity:
ISO C没有出现entity和variable的正式定义。variable基本上用于variable length array等合成词组中。
C99 Rationale V5.10 p35
6.2.4
...
The scope of a variable starts at its declaration. Therefore, although the variable exists as soon as the block is entered, it cannot be referred to by name until its declaration is reached.
variable和object不同。object不讨论scope。讨论scope的是identifier,在这里可以是variable。对于这个上下文来说,用variable指代variable name的确是没有疑义的。但也仅此而已。
C的object、scope概念和C++等价,C的identifier在使用上是C++的name的一种特殊情况。所以套用C++的概念定义作为补充,逻辑上完全不成问题。唯一麻烦的是ISO C没说清楚什么算entity(似乎认为读者不言自明),偏偏正文中自己就在用,如:
ISO C11(N1570)
2 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
6.其它语言如Java也有把variable当成表达式的:
The Java Language Specification/Java SE Edition 7
15.1 Evaluation, Denotation, and Result
When an expression in a program is evaluated (executed), the result denotes one
of three things:
• A variable (§4.12) (in C, this would be called an lvalue)
• A value (§4.2, §4.3)
• Nothing (the expression is said to be void)
... ISO C11(N1570)
6.3.2.1
1 An lvalue is an expression (with an object type other than void) that potentially designates an object;...
但因为实体概念和求值方式跟C/C++有比较大的不同,只是用来说明variable可以当作的可能性,并非判断是否合适的依据。
|
|