重用这个论点? [英] Re-use the argument?

查看:62
本文介绍了重用这个论点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你会选择哪些功能?


unsigned Func(unsigned const x)

{

返回3 * x - 2;

}


或:


unsigned Func (未签名x)

{

x * = 3;

x - = 2;


返回x;

}

解决方案

Tomásóhéilidhe说:


>

你会选择哪些功能?


unsigned Func(unsigned const x)

{

返回3 * x - 2;

}


或:


unsigned Func(unsigned x)

{

x * = 3;

x - = 2;


返回x;

}



两者都不。我会选择这个:


unsigned Func(unsigned int x)

{

返回3 * x - 2;

}


(重命名Func以反映函数的用途,并将有意义的数字替换为
- 命名符号常量)。

但是如果你的问题是:是否值得对它进行常数装饰参数?答案是不。 C是一种按值传递的语言。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


6月7日,8:26 * am,Richard Heathfield< r ... @ see.sig.invalidwrote:
< blockquote class =post_quotes>
但是如果你的问题是:它是否值得为它完成它的地狱装饰参数?答案是不。 C是一种按值传递的语言。



我纯粹是指重新使用函数参数。一个

的人可能会认为后一个版本会产生更多的b $ b b b高效代码,因为它不会使用临时变量来进行计算

,但是再次这个好处可以通过以下方式取消:i)

使用累加器寄存器或ii)编译器使用const变量自动重新确定
它知道他们不再需要了。或者

然后这个人可能只选择前者而没有任何

考虑因素,将它全部交给编译器。


我只是对我得到的是或否更加好奇。回答

来自人们。你会做一个简单的R值表达式:


返回3 * x - 2;


或者你会尝试进行单独的操作吗?论点:


x * = 3;

x - = 2;

返回x;


Tomásóhéilidhe写道:


>

你会选择哪些功能?


无符号Func(无符号常数x){

返回3 * x - 2;

}

或:

无符号Func(无符号x){

x * = 3;

x - = 2;

返回x ;

}



你为什么问这样愚蠢的问题?他们只是烦恼。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page] :< http://cbfalconer.home.att.net>

尝试下载部分。


**发自 http://www.teranews.com **



Which of these functions would you go with?

unsigned Func(unsigned const x)
{
return 3*x - 2;
}

or:

unsigned Func(unsigned x)
{
x *= 3;
x -= 2;

return x;
}

解决方案

Tomás ó héilidhe said:

>
Which of these functions would you go with?

unsigned Func(unsigned const x)
{
return 3*x - 2;
}

or:

unsigned Func(unsigned x)
{
x *= 3;
x -= 2;

return x;
}

Neither. I''d go with this one:

unsigned Func(unsigned int x)
{
return 3 * x - 2;
}

(after renaming Func to reflect the purpose of the function, and replacing
the magic numbers with meaningfully-named symbolic constants).
But if your question is: is it worth const-decorating parameters for the
hell of it?, the answer is "no". C is a pass-by-value language.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Jun 7, 8:26*am, Richard Heathfield <r...@see.sig.invalidwrote:

But if your question is: is it worth const-decorating parameters for the
hell of it?, the answer is "no". C is a pass-by-value language.


I was refering purely to the "re-using" of the function argument. A
person might think that the latter version would result in more
efficient code because it does not use a temporary variable for doing
the calculations, but then again this benefit could be rescinded by i)
the use of an acumulator register or ii) the compiler''s automatic re-
using of const variables when it knows they''re no longer needed. Or
then again the person might just choose the former without any
consideration, leaving it all in the hands of the compiler.

I''m just more curious as to what I''d get for a "Yes or No" answer to
this from people. Would you do a simple R-value expression:

return 3*x - 2;

or would you attempt to do individual operations on the argument:

x *= 3;
x -= 2;
return x;


Tomás ó héilidhe wrote:

>
Which of these functions would you go with?

unsigned Func(unsigned const x) {
return 3*x - 2;
}
or:
unsigned Func(unsigned x) {
x *= 3;
x -= 2;
return x;
}

Why are you asking such stupid questions? They only annoy.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **


这篇关于重用这个论点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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