为什么左值转换工作? [英] Why does an lvalue cast work?

查看:114
本文介绍了为什么左值转换工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天第一次看到这种演员,我很好奇为什么这个演出。我认为以这种方式投入将分配给临时,而不是类成员。使用VC2010。

I saw this kind of cast for the first time today, and I'm curious as to why this works. I thought casting in this manner would assign to the temporary, and not the class member. Using VC2010.

class A
{
public:

   A() :
      m_value(1.f)
   {
      ((float)m_value) = 10.f;
   }

   const float m_value;
};


推荐答案

使用cast符号的 float 的显式类型转换将是一个prvalue(§5.4):

It shouldn't work. An explicit type conversion to float with cast notation will be a prvalue (§5.4):


表达式(T) cast-expression 的结果类型为 T 。如果T是一个左值引用类型或对函数类型的右值引用,并且如果T是对象类型的右值引用,则结果为左值;

The result of the expression (T) cast-expression is of type T. The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type and an xvalue if T is an rvalue reference to object type; otherwise the result is a prvalue.

我强调的是

赋值运算符需要左值作为其左操作数(§5.17):

The assignment operator requires an lvalue as its left operand (§5.17):


所有都需要可修改的左值左操作数并返回引用左操作数的左值。

All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.

prvalue不是左值。

A prvalue is not an lvalue.

这篇关于为什么左值转换工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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