问题描述
有一种特定的编码风格,尽可能使用const。
赞
const int foo(const int a,const int b)
{
const int retval = pow(a,b);
返回retval;
}
使用这样的代码的一个参数是编译器理论上可以优化
优化。
这是真的吗?我以为那只是指针?
Theres a certain style of coding that uses const as much as possible.
Like
const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}
one argument to use code like this is that compilers can theoretically
optimize better.
Is that true? I thought that was only with pointers?
推荐答案
我读了一篇文章,它更喜欢以下风格。
const int foo(int a,int b){
cosnt aa = a;
cosnt bb = b;
int retval;
/*...*/
retval = pow(aa,bb);
返回retval;
}
I read an article and it prefers the following style.
const int foo(int a, int b){
cosnt aa = a;
cosnt bb = b;
int retval;
/*...*/
retval = pow(aa, bb);
return retval;
}
对不起,代码会随着更改而变得更好,
cosnt int ca = a;
cosnt int cb = b;
sorry, the code will be better with the changes,
cosnt int ca = a;
cosnt int cb = b;
对不起,代码会随着更改而变得更好,
cosnt int ca = a;
cosnt int cb = b;
sorry, the code will be better with the changes,
cosnt int ca = a;
cosnt int cb = b;
更好:
const int ca = a;
const int cb = b;
:-)
Giorgio Silvestri
better:
const int ca = a;
const int cb = b;
:-)
Giorgio Silvestri
这篇关于const整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!