English 中文(简体)
为什么在所有其他字面都高估时,字面直面字面被打上?
原标题:Why are string literals l-value while all other literals are r-value?

<>C++03 5.1 初级表达方式2 说:

字面是主要表述。 其类型取决于其形式(2.13)。 缩略词是一种价值;所有其他字面都是高值。

同样,C99 6.5.1 §4:

缩略语是主要表述。 它是6.4.5所详述的类型。

这样做的理由是什么?

我的理解是,打字字面是物体,而所有其他字面不是。 而一升值总是指物体。

但问题是,为什么在所有其他字面都不是这样的情况下,字面上的物体被打上? 在我看来,这种理由似乎更像一个鸡蛋或鸡蛋问题。

我理解,这个问题的答案可能与硬件结构有关,而不是作为方案拟订语言的C/C++,但我要听到同样的看法。

问题回答

A string literal is a literal with array type, and in C there is no way for an array type to exist in an expression except as an lvalue. String literals could have been specified to have pointer type (rather than array type that usually decays to a pointer) pointing to the string "contents", but this would make them rather less useful; in particular, the sizeof operator could not be applied to them.

请注意,C99采用“复合物”,也为“价值”,因此“面值”不再是特别例外;它更接近于成为规范。

String literals are arrays - objects of inherently unpredictable size (i.e of user-defined and possibly large size). In general case, there s simply no other way to represent such literals except as objects in memory, i.e. as lvalues. In C99 this also applies to compound literals, which are also lvalues.

任何人为隐瞒字面直面字面为l Values的企图,都将造成大量不必要的困难,因为能够用点字面直面直面直面,以及能够作为阵列来,关键取决于其是否在语言层面具有很高的价值。

同时,ar类的字面有固定的汇编时间。 与此同时,这种字面很可能直接植入特定硬件结构的机器指令中。 例如,如果你写上<条码>i = i * 5 + 2,则字面值<条码>5和<条码>2成为生成的机码中明确(甚至含)部分。 它们并不存在,也不需要作为数据储存的单独地点存在。 在数据记忆中,存储数值没有点52

还值得注意的是,在许多(如果不是多数或全部)硬件结构中,浮动点字面实际上被作为“隐蔽的”<代码>l Values加以实施。 (尽管这些语言并不暴露于这些语言)。 在像X86号机组这样的平台上,浮点组的机器指挥不支持嵌入式直接操作。 这意味着,几乎每个浮点字面必须储存在(和从)数据记忆中。 E.g. 页: 1 它变成了类似的东西。

const double unnamed_double_5_5 = 5.5;
const double unnamed_double_2_1 = 2.1;
i = i * unnamed_double_5_5 + unnamed_double_2_1;

换言之,floating-point Hauss 往往会在内部成为“un official”l Values。 然而,很有必要的是,语言说明并没有试图揭露这种执行细节。 在语文一级,<代码>arithmeticlichs具有更大的意义,成为r Values

I d guess that the original motive was mainly a pragmatic one: a string literal must reside in memory and have an address. The type of a string literal is an array type (char[] in C, char const[] in C++), and array types convert to pointers in most contexts. The language could have found other ways to define this (e.g. a string literal could have pointer type to begin with, with special rules concerning what it pointed to), but just making the literal an lvalue is probably the easiest way of defining what is concretely needed.

C++中的l Value 并非总是指标。 这也可以指职能。 此外,物体不必通过<代码>l Values提及。 可通过<代码>r Values查询,包括阵列(C++和C)。 然而,在旧的C89中,点转换阵列不适用于<代码>r Value阵列。

现在,<代码>r Value表示寿命已满、有限或即将届满。 然而,整个方案都有一段直言。

http://code>stringlichs_l Values is actual.

答案和评论中有许多宝贵的信息。 几个要点值得强调。

射线可以是高值。 详情见。 例如,以下法典涉及一个高价值阵列:

template <typename T>
using alias = T;

int main() {
    return alias<int[]>{23, 37, 53}[1];
}

因此,说明字面为阵列,使之具有价值,是不错的。

最好记住,在方案一生中,描述字面。 尽管,但有理由看看看,如何根据字面说明是按其一生的价值。

many。 关于价值类别的讨论,把字面描述为微值,在很大程度上是出于对迄今为止语文发展所发生情况的实际考虑,以及从我们当时所处的位置上可以做些什么。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签