批准的方式,以避免lvalue施加警告和错误? [英] Approved way to avoid lvalue cast warnings and errors?

查看:181
本文介绍了批准的方式,以避免lvalue施加警告和错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与JoGusto在投放错误:需要作为左操作数赋值的左值的回答相关。在回答中,他/她说:


但有一种情况是不正确的:转换,然后取消引用指针: / p>

  *((int *)chrPtrValue)= some_integer_expression; 


我想我发现Joseph Mansfield的答案来自为什么左值转换工作?,他引用了标准。但这让我困惑了,因为我可以区分左值和右值,但 xvalues prvalues



天真,在我看来,规则是出于某种原因,所以一些避免它的方法可能是[间接或直接]非法的。



我有一些与左值转换相关的问题。用例包括以下内容。在第一种情况下,底层类型是不同的。在第二种情况下,修改符已更改。

  float f; 
*(static_cast< int *>(& f))= 1;

int ptr = ...;
*(static_cast< volatile int *>(& ptr))= NULL;它是合法的C和C + +绕过左值转换错误使用间接然后解引用?

$

 

如果转换只改变限定符(即 static const



如果是合法的C和C ++,那么它是合法的违反其他规则,如GCC的别名规则?



最后,如果它违反了C或C ++或其他规则,那么什么是批准的方式 memcpy memmove )?

解决方案

JoGusto答案是(a)不是很好,和(b)在C问题。


首先,合法的C和C ++是否绕过了左值转换错误(间接然后取消引用)?


IDK绕过左值转换错误是什么意思。代码(T)x = y; 只是非法废话(除了C ++中 T 左值参考,因为约瑟夫曼斯菲尔德的答案涵盖)。你不绕过它;



代码 *(T *)ptr = y;

code>编译。它意味着调用存储在 ptr 中的地址处的 T 对象上的赋值运算符。它与C ++中的(T&)* ptr = y; 相同,它是 reinterpret_cast< T&>(* ptr)= y ;


是否违反其他规则,如GCC的抗锯齿规则?


行为受到对齐和严格别名混淆。如果实际上没有存储在该地址的 T 对象,也不是根据 T 列表中的严格别名规则(在这种情况下: int unsigned int ),那么它是未定义的行为。 / p>


那么批准的方式是什么(可能是memcpy或memmove)?


您可以这样写:

  int x = some_integer_expression; 
memcpy(chrPtrValue,& x,sizeof x);




我通常可以区分左值和右值,而不是xvalues和prvalues


在此示例中,您无法识别哪些表达式?


This is related to JoGusto's answer at Casting Error: lvalue required as left operand of assignment. In the answer, he/she states:

but there is one case where it is not true: casting, then dereferencing a pointer:

*((int *) chrPtrValue) = some_integer_expression;

I think I found the answer at Joseph Mansfield answer from Why does an lvalue cast work?, where he cited the standard. But that confused me more because I can differentiate between lvalues and rvalues, but xvalues and prvalues are still new to me.

Naively, it seems to me that the rule is present for a reason, so some of the methods for circumventing it would probably be [indirectly or directly] illegal also.

I have a few questions related to the lvalue cast. The use cases includ the following. In the first case, the underlying types are different. In the second case, the qualifiers were changed.

float f;
*(static_cast<int*>(&f)) = 1;

int ptr = ...;
*(static_cast<volatile int*>(&ptr)) = NULL; 

Is it legal C and C++ circumvent the lvalue cast error using indirection then dereferencing?

If the cast only changes the qualifiers (i.e., static const or volatile) , then is it still legal in C and C++?

If its legal C and C++, then does it violate other rules, like GCC's aliasing rules?

Finally, if it does violate C or C++, or other rules, then what is the approved way to do it (perhaps a memcpy or memmove)?

解决方案

The JoGusto answer is (a) not very good, and (b) on a C question.

First, is it legal C and C++ circumvent the lvalue cast error like that (indirection then dereferencing)?

IDK what you mean by "circumvent the lvalue cast error". The code (T)x = y; is just illegal nonsense (except for the case in C++ where T is an lvalue reference, as Joseph Mansfield's answer covers). You don't circumvent it; you write code that has a sensible meaning and does what you want to do.

The code *(T *)ptr = y; compiles. It means to invoke the assignment operator on a T object which is stored at the address in ptr. It's the same as (T &)*ptr = y; in C++, which is reinterpret_cast<T&>(*ptr) = y; .

does it violate other rules, like GCC's anti-aliasing rules?

The behaviour is subject to alignment and strict aliasing. If there is not actually a T object stored at that address, nor an object of type compatible with T according to the list in the strict aliasing rule (in this case: int or unsigned int), then it is undefined behaviour.

then what is the approved way to do it (perhaps a memcpy or memmove)?

You could write:

int x = some_integer_expression;
memcpy(chrPtrValue, &x, sizeof x);

I can usually differentiate between lvalues and rvalues, and not xvalues and prvalues.

Which expressions are you having trouble identifying in this example?

这篇关于批准的方式,以避免lvalue施加警告和错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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