临时的,非常规的还有r值?! [英] Temporaries, non-const yet r-value?!

查看:65
本文介绍了临时的,非常规的还有r值?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我们都知道temporaries是非const的,我将用以下代码演示



struct Blah

{

int monkey;


void SetMonkey(提供int const)

{

monkey =供应;

}

};

Blah SomeFunc()

{

返回Blah();

}

int main()

{

Blah() .SetMonkey(5);


SomeFunc()。monkey = 5;

}

但与此同时,临时性是r值。以下是

非法:


int Blah()

{

返回5; < br $>
}


int main

{

Blah()= 6;

}

此外,在前面的代码中,Blah()。monkey = 5是非法的。

将临时引用绑定到非const引用是不合法的,因为它是b-b和r值。

您可以使用const_cast如果

原始对象实际上是非常量的,则抛弃constness ...


struct Blah

{

int a;

double b;

char c;

float ** p_p_f;

wchar_t k;

bool f;

};


int main()

{

Blah const& poo = Blah();


Blah& cow = const_cast< Blah&>(poo);


cow.a = 5;

}


对此有何看法?

- JKop

Firstly, we all know that temporaries are non-const, which I shall
demonstrate with the following code:

struct Blah
{
int monkey;

void SetMonkey(int const supplied)
{
monkey = supplied;
}
};
Blah SomeFunc()
{
return Blah();
}
int main()
{
Blah().SetMonkey(5);

SomeFunc().monkey = 5;
}
But, at the same time, temporaries are "r-value"''s. The following is
illegal:

int Blah()
{
return 5;
}

int main
{
Blah() = 6;
}
Also, in the previous code, "Blah().monkey = 5" is illegal.
It''s not legal to bind a temporary to a non-const reference, because it is
an "r-value".
You''re allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...

struct Blah
{
int a;
double b;
char c;
float** p_p_f;
wchar_t k;
bool f;
};

int main()
{
Blah const &poo = Blah();

Blah &cow = const_cast<Blah&>(poo);

cow.a = 5;
}

Any thoughts on this?
-JKop

推荐答案

* JKop:

对此有何看法?

Any thoughts on this?




这是正确的观察。是的,这是有问题的。 Mojo的文章

(现在很久以前,以软件开发时间衡量)开始了当前辩论的



< url:http: //www.cuj.com/documents/s=8246/cujcexp2102alexandr/alexandr.htm>。

最近在这个论坛上,Chris Theis提出了什么是什么? />
从那时起发生。


这是[comp.std.c ++]中的新线程NRVO,但很少具体

即将到来。


-

答:因为它弄乱了人们通常阅读文字的顺序。

Q :为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?



It''s a correct observation. And yes it''s problematic. The Mojo article
that (now long ago, measured in software development time) started the
current debate:
<url: http://www.cuj.com/documents/s=8246/cujcexp2102alexandr/alexandr.htm>.

And recently in this forum Chris Theis raised the question of what''s
happened since then.

That''s a new thread NRVO in [comp.std.c++], but very little concrete
has been forthcoming.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


JKop在新闻中写道:wL ******************* @ news.indigo.ie in comp.lang.c ++:
JKop wrote in news:wL*******************@news.indigo.ie in comp.lang.c++:
你被允许使用const_cast。如果
原始对象实际上是非常量的,则抛弃常量...


这是正确的,但是......

struct Blah
{
};

int main()
{
Blah const& poo = Blah();


问题出在这里,rvalue允许通过临时的

进行绑定,尽管我们都知道实际的临界值(*)是否定的

比rvalue更恒定,标准允许它。


*)这里:现实==台式电脑'


所以实际的对象是poo。指的是一个真正的常数。

Blah& cow = const_cast< Blah&>(poo);


没关系,但是......

cow.a = 5;


即UB。

}
You''re allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...

Thats correct, but ...
struct Blah
{ };

int main()
{
Blah const &poo = Blah();
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.

*) Here: reality == Desktop PC''s

So the actual object "poo" refers to is a real constant.

Blah &cow = const_cast<Blah&>(poo);
That is OK, but ...

cow.a = 5;
That is UB.
}




微软VC ++有一种模式可以检查' (在运行时)对于

缓冲区溢出,这样的系统也可用于检查临时poo的常量。也是绑定的,你的代码

不再有效。


同样在这种情况下,因为Blah使用的是一个值

初始化的POD,常量可能实际上是只读的内存,bang再次代码你的代码(或者它可能没有编译) )。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


问题出在这里,rvalue允许通过临时绑定
尽管我们都知道实际上的温度(*)不会比rvalue更稳定,标准允许它。


不,不。临时不一定是常数。可以修改一个临时的类型b $ b类。

这没关系,但是......

The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not. Temporaries are not necessarily const. A temporary
of class type can be modified.

That is OK, but ...

cow .a = 5;

那是UB。
cow.a = 5;

That is UB.



No.

在这种情况下,由于Blah,使用的是一个值初始化的POD,常量可以实际上是只读的内存,然后再次执行你的代码(或者它可能没有编译)。


No.
Also in this case, since the "Blah" that was used is a value
initialized POD, the constant could actualy be in read-only
memory, bang goes your code again (or maybe it doesn''t compile).




编号



No.


这篇关于临时的,非常规的还有r值?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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