铸造一个指针不会产生左值。为什么? [英] Casting a pointer does not produce an lvalue. Why?

查看:129
本文介绍了铸造一个指针不会产生左值。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

张贴我最有争议的答案之一<经过href=\"http://stackoverflow.com/questions/7444464/read-bytes-methods-in-c-c/7444502#7444502\">here,我不敢问几个问题,并最终在我的知识填补一些空白。

After posting one of my most controversial answers here, I dare to ask a few questions and eventually fill some gaps in my knowledge.

为什么不是那种一个前pression ((type_t *)x)视为有效左值,假设 X 本身就是一个指针和一个左值,而不仅仅是一些前pression?

Why isn't an expression of the kind ((type_t *) x) considered a valid lvalue, assuming that x itself is a pointer and an lvalue, not just some expression?

我知道很多人会说:标准的禁止它,但是从逻辑的角度来看,似乎是合理的。什么是标准不允许它的原因?毕竟,任何两个指针具有相同的尺寸和指针类型仅仅是一个编译时抽象指示应当应用操作的指针运算时适当的偏移

I know many will say "the standard disallows it", but from a logical standpoint it seems reasonable. What is the reason that the standard disallows it? After all, any two pointers are of the same size and the pointer type is just a compile-time abstraction that indicates the appropriate offset that should be applied when doing pointer arithmetic.

推荐答案

这是更好的例子,一元 + 产生一个右值一样, X + 0 。

An even better example, unary + yields an rvalue, as does x+0.

其根本原因在于,所有这些东西,包括你的施法,创造新的价值。铸造一个值到它已经是类型,同样地创建一个新的值,别提指针不同类型是否具有相同的重presentation与否。在某些情况下,新的值恰好等于原来的值,但在原则上它是一个新的值,它不打算用来作为对旧对象的引用,而这就是为什么它的右值

The underlying reason is that all these things, including your cast, create a new value. Casting a value to the type it already is, likewise creates a new value, never mind whether pointers to different types have the same representation or not. In some cases, the new value happens to be equal to the old value, but in principle it's a new value, it's not intended to be used as a reference to the old object, and that's why it's an rvalue.

有关这些是左值,该标准将不得不增加,而不是一个新的值,在旧的对象的引用上左值结果当用于某些操作某些特殊情况下,。 AFAIK有这些特殊情况没有很大的需求。

For these to be lvalues, the standard would have to add some special cases that certain operations when used on an lvalue result in a reference to the old object, instead of a new value. AFAIK there's no great demand for those special cases.

这篇关于铸造一个指针不会产生左值。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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