临时工和常数 [英] temporaries and const&

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

问题描述

大家好,请考虑以下功能: -


const int& foo(const double& d)

{

返回d;

}


g ++编译它有警告和solaris CC给出错误。我想

知道代码是否符合标准?


/ P

Hi everyone, please consider the following function:-

const int& foo ( const double& d )
{
return d;
}

g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?

/P

推荐答案

* dragoncoder:
* dragoncoder:

大家好,请考虑以下功能: -


const int& foo(const double& d)

{

返回d;

}


g ++编译它有警告和solaris CC给出错误。我想

知道代码是否符合标准?
Hi everyone, please consider the following function:-

const int& foo ( const double& d )
{
return d;
}

g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?



这在技术上是正确的。


在返回表达式中,int const&引用必然是一个临时的int和
(初始化)。这是用

" double"初始化的,它被隐式转换为int。


然而,使用调用foo的结果(产生未定义的行为,

,因为你正在返回对临时的引用。 C ++就是这样,

默认非常宽松。它高高兴兴地让你自己射击腹股沟,而不是逮捕你[1],如果这就是你想要的那样。


干杯,


- Alf

注意:

[1]< url:http://news.bbc.co。 uk / 1 / hi / england / south_yorkshire / 3891311.stm> ;.


-

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

问:为什么这么糟糕?

A:热门帖子。

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

It''s technically correct.

In the return expression an "int const&" reference is bound to
(initialized with) a temporary "int" that''s initialized with the
"double", which is implicitly converted to int.

However, using the result of a call to foo() yields Undefined Behavior,
because you''re returning a reference to a temporary. C++ is like that,
very permissive by default. It cheerfully lets you shoot yourself in
the groin, instead of arresting you[1], if that''s what you say you want.

Cheers,

- Alf
Notes:
[1] <url: http://news.bbc.co.uk/1/hi/england/south_yorkshire/3891311.stm>.

--
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?


dragoncoder写道:
dragoncoder wrote:

大家好,请考虑以下功能: -


const int& foo(const double& d)

{

返回d;

}


g ++编译它有警告和solaris CC给出错误。我想

知道代码是否符合标准?
Hi everyone, please consider the following function:-

const int& foo ( const double& d )
{
return d;
}

g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?



它是正确的,词汇和语法。问题是

整个事情是无用的,因为通过引用返回const

将导致创建一个临时对象,它只能存活

直到结束对''foo''的调用。 IOW函数

返回后临时对象已被销毁。


V

-

请在通过电子邮件回复时删除资金''A'

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

It''s "correct", lexically and syntactically. The problem is that
the whole thing is useless because returning by a reference to const
will cause creation of a temporary object, which will survive only
until the end the call to ''foo''. IOW right after the function
returns the temporary object has already been destroyed.

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


4月30日下午1:54,Alf P. Steinbach < a ... @ start.nowrote:
On Apr 30, 1:54 pm, "Alf P. Steinbach" <a...@start.nowrote:

* dragoncoder:
* dragoncoder:

大家好,请考虑以下函数: -
Hi everyone, please consider the following function:-


const int& foo(const double& d)

{

返回d;

}
const int& foo ( const double& d )
{
return d;
}


g ++用警告编译它,solaris CC给出错误。我想

知道代码是否符合标准?
g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?



这在技术上是正确的。


在返回表达式中,int const&"引用必然是一个临时的int和
(初始化)。这是用

" double"初始化的,它被隐式转换为int。


然而,使用调用foo的结果(产生未定义的行为,

,因为你正在返回对临时的引用。 C ++就是这样,

默认非常宽松。它高高兴兴地让你自己射击腹股沟,而不是逮捕你[1],如果这就是你想要的那样。


It''s technically correct.

In the return expression an "int const&" reference is bound to
(initialized with) a temporary "int" that''s initialized with the
"double", which is implicitly converted to int.

However, using the result of a call to foo() yields Undefined Behavior,
because you''re returning a reference to a temporary. C++ is like that,
very permissive by default. It cheerfully lets you shoot yourself in
the groin, instead of arresting you[1], if that''s what you say you want.



感谢您的回复。在相同的上下文中,此代码是否调用

未定义的行为?


#include< iostream>


模板<类T1,类T2>

const T1& max(const T1& a,const T2& b)

{

return(a b)? a:b;

}


int main(){

int i = 20; double d = 40;

std :: cout<< max(i,d)<< std :: endl;

返回0;

}

Thanks for the response. In the same context, does this code invoke
undefined behaviour ?

#include <iostream>

template <class T1, class T2>
const T1& max ( const T1& a, const T2& b )
{
return ( a b ) ? a : b;
}

int main() {
int i = 20; double d = 40;
std::cout << max ( i, d ) << std::endl;
return 0;
}


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

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