这有不确定的行为 [英] does this have undefined behaviour

查看:82
本文介绍了这有不确定的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:


#include< iostream>

#include< cstdlib>


使用命名空间std;


int main()

{

const double& ref = 100;

double& d = const_cast< double&>(ref);

cout<< d<< endl;

d = 1.1;

cout<< ref<<结束;


返回EXIT_SUCCESS;

}


在g ++和VC ++ 2005 Express Edition中,输出为

100

1.1


但我怀疑是:使用''d''调用未定义的行为还是它

有效。


double& d = const_cast< double&>(ref);


请澄清。


谢谢

V. Subramanian

Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<double&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using ''d'' invoke undefined behavior or is it
valid."

double& d = const_cast<double&>(ref);

Kindly clarify.

Thanks
V.Subramanian

推荐答案

su**************@yahoo.com 写道:

请考虑以下代码:


#include< iostream>

#include< cstdlib>


使用命名空间std;


int main()

{

const double& ref = 100;

double& d = const_cast< double&>(ref);

cout<< d<< endl;

d = 1.1;

cout<< ref<<结束;


返回EXIT_SUCCESS;

}


在g ++和VC ++ 2005 Express Edition中,输出为

100

1.1


但我怀疑是:使用''d''调用未定义的行为还是它

有效。


double& d = const_cast< double&>(ref);
Consider the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const double& ref = 100;
double& d = const_cast<double&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;

return EXIT_SUCCESS;
}

In both g++ and VC++ 2005 Express Edition, the output is
100
1.1

But my doubt is: "does using ''d'' invoke undefined behavior or is it
valid."

double& d = const_cast<double&>(ref);



你在这里做的是改变绑定到const引用的临时

的值。这样的临时性不是常数,所以,

通过抛弃常数来改变它的值应该没问题。

我在标准中找不到任何地方禁止

这样的功能。然后我的猜测是允许的。


V

-

请删除大写''A'' s通过电子邮件回复

我没有回复最热门的回复,请不要问

What you''re doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can''t find any place in the Standard that would prohibit
such functionality. Then my guess is that it''s allowed.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月21日,上午5:04,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Dec 21, 5:04 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

subramanian10 ... @ yahoo.com写道:
subramanian10...@yahoo.com wrote:

请考虑以下代码:
Consider the following code:


#include< iostream>

#include< cstdlib>
#include <iostream>
#include <cstdlib>


using namespace std;
using namespace std;


int main()

{

const double& ref = 100;

double& d = const_cast< double&>(ref);

cout<< d<< endl;

d = 1.1;

cout<< ref<< ENDL;
int main()
{
const double& ref = 100;
double& d = const_cast<double&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;


返回EXIT_SUCCESS;

}
return EXIT_SUCCESS;
}


在g ++和VC ++ 2005 Express Edition中,输出是



1.1
In both g++ and VC++ 2005 Express Edition, the output is
100
1.1


但我怀疑是:使用''d''调用未定义的行为还是

有效。
But my doubt is: "does using ''d'' invoke undefined behavior or is it
valid."


double& d = const_cast< double&>(ref);
double& d = const_cast<double&>(ref);



你在这里做的是改变绑定到const引用的临时

的值。这样的临时性不是常数,所以,

通过抛弃常数来改变它的值应该没问题。

我在标准中找不到任何地方禁止

这样的功能。然后我的猜测是允许的。


What you''re doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can''t find any place in the Standard that would prohibit
such functionality. Then my guess is that it''s allowed.



我认为OP可能认为他实际上改变了ref的价值

(常数)。我不认为他意识到创建了一个临时的b / b $ b因此改变了d的值。不会改变参考的价值。


问候,


Werner

I think the OP might have thought that he actually changed the value
of "ref" (the constant one). I don''t think he realized that a
temporary was created - therefore changing the value of "d" does
not change the value of "ref".

Regards,

Werner


werasm写道:
werasm wrote:

12月21日上午5:04,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Dec 21, 5:04 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

> subramanian10 ... @ yahoo.com写道:
>subramanian10...@yahoo.com wrote:

>>请考虑以下代码:
>>Consider the following code:


>> #include< iostream>
#include< cstdlib>
>>#include <iostream>
#include <cstdlib>


>> using namespace std;
>>using namespace std;


>> int main()
{
const double& ref = 100;
double& d = const_cast< double&>(ref);
cout<< d<< endl;
d = 1.1;
cout<< ref<< ENDL;
>>int main()
{
const double& ref = 100;
double& d = const_cast<double&>(ref);
cout << d << endl;
d = 1.1;
cout << ref << endl;


>>返回EXIT_SUCCESS;
}
>>return EXIT_SUCCESS;
}


>>在g ++和VC ++ 2005 Express Edition中,输出为
100
1.1
>>In both g++ and VC++ 2005 Express Edition, the output is
100
1.1


> >但我怀疑是:使用''d''调用未定义的行为还是
有效。
>>But my doubt is: "does using ''d'' invoke undefined behavior or is it
valid."


>> double& d = const_cast< double&>(ref);
>>double& d = const_cast<double&>(ref);


你在这里做的是改变绑定到const引用的临时值。这样的临时性不是常数,所以,通过抛弃常量来改变它的价值应该没问题。
我在标准中找不到任何禁止这种功能的地方。然后我的猜测是允许的。


What you''re doing here is changing the value of the temporary
bound to a const reference. Such temporary is not const, so,
changing its value by casting away const-ness should be OK.
I can''t find any place in the Standard that would prohibit
such functionality. Then my guess is that it''s allowed.



我认为OP可能认为他实际上改变了ref的价值

(常数)。我不认为他意识到创建了一个临时的b / b $ b因此改变了d的值。

不会改变ref的值。


I think the OP might have thought that he actually changed the value
of "ref" (the constant one). I don''t think he realized that a
temporary was created - therefore changing the value of "d" does
not change the value of "ref".



不,这是不正确的,IMO。首先,你不能改变价值

的ref,因为它是一个参考,它没有真正的价值

(除了事实它指的是某个特定的对象)。

临时创建的类型为''double'',它的初始值为

为100.00并且它的生命周期就开始了''ref''绑定

给它。积分表达式''100''是初始值为

临时值。


绑定''ref''的临时值不是const宾语。它可以改变它的价值。
完全没问题。

这样做是否有意义是另一个问题,但这个想法与
中的相同。
void foo(int const& rci)

{

int& ri = const_cast< int&>(rci);

ri = 666; /// 这个可以吗? - 我说,是的,它是!

}


#include< iostream>

#include< ostream> ;

int main(){

int a = 42;

std :: cout<< 在a =之前 << a<< std :: endl;

foo(a);

std :: cout<< a =之后<< a<< std :: endl;

}


V

-

请删除资金''通过电子邮件回复时的答案

我没有回复最热门的回复,请不要问

No, that''s incorrect, IMO. First off, you can''t change the value
of ref because it''s a reference, it doesn''t really have a value
(aside from the fact that it refers to some particular object).
The temporary created is of type ''double'', it has initial value
of 100.00 and it starts its lifetime just before ''ref'' is bound
to it. The integral expression ''100'' is the initialiser for that
temporary.

The temporary to which ''ref'' is bound is not a const object. It
is perfectly OK to change its value. Whether it makes sense to
do so or not is another question, but the idea is the same as in

void foo(int const& rci)
{
int& ri = const_cast<int&>(rci);
ri = 666; /// Is this OK? -- I say, yes, it is!
}

#include <iostream>
#include <ostream>
int main() {
int a = 42;
std::cout << "Before a = " << a << std::endl;
foo(a);
std::cout << "After a = " << a << std::endl;
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于这有不确定的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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