const 用于非引用参数
如果我有这段代码:
void Foo(aBasicType aIn) //Where aBasicType is int, char etc.
{
//...
}
将其设置为 const aBasicType 是否有任何意义,因为无论如何它都会被复制?我问的原因之一是因为我在第 3 方代码中看到了它,并且想知道是否有我不知道的东西。
If I have this code:
void Foo(aBasicType aIn) //Where aBasicType is int, char etc.
{
//...
}
Is there any point in making it const aBasicType
since it is going to be copied anyway? One of the reasons I am asking is because I have seen it in 3rd party code and was wondering if there is something I am not aware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您知道函数在执行过程中不需要修改其值,那么将其声明为 const 也没什么坏处。
请注意,当参数按值传递时,更改其参数的函数应该很少见。
声明变量
const
可以防止您编写if (aIn = someValue)
。It cannot hurt to declare it
const
if you know that your function needs not modify its value during execution.Note that functions that change their arguments, when arguments are passed by value, should be rare.
Declaring your variable
const
can prevent you from writingif (aIn = someValue)
.我有时(不经常)这样做,当有诱惑就地修改
aIn
而不是制作另一个副本时,但该方法依赖于aIn
在整个过程中保持不变。不过,这往往是一次千钧一发的机会。I sometimes (infrequently) do it, when there is temptation to modify
aIn
in-place instead of making another copy, yet the method relies onaIn
remaining unchanged throughout. It tends to be a close call though.原因很丰富:您希望编译器在赋值左侧出现值传递参数时发出警告/错误。
这有点麻烦,在那些不太了解 C 或 C++ 的库上可以看到(这对于两种语言都是一样的)。
The reason is informative: you want the compiler to warn/error when a value-passed argument is seen on the left of an assignment.
It's a bit cumbersome, seen on libs whose audience may be less than "well informed" on C or C++ (it's the same for both languages).
这将使该函数的值
const
可能有用,就像在函数顶部声明常量可能有用一样。That would make the value
const
for that function, which might be useful in the same way declaring a constant at the top of your function might be useful.不,将 const 添加到标量按值调用参数是没有意义的,只会造成混乱。
No, adding const to a scalar call-by-value parameter is meaningless and will only be confusing.
我更喜欢向输入参数添加 const 限定符,无论参数传递方法如何(按值、按指针或按引用)。因此,const 参数仅表示“输入参数”,非常量参数表示“输出参数”(或者很少是 inout 参数)。我认为这样的约定使代码更容易理解,但这当然是品味问题。
I prefer to add const qualifier to input paramters regardless to parameter passing method (by value, by pointer or by reference). So const parameter simply means "input parameter" and non-const parameter means "output parameter" (or, rarely, inout parameter). I suppose such a convention makes code more understandable but it is matter of taste, of course.
我想我可以更简单地表述这一点。当
footype
不是模板参数时:const footype &
是对调用者的保证函数以及函数实现者的约束。
const footype
只是的约束
实现者与调用者无关。
当 footype 是模板参数时,只能针对各个模板实例来测试规则。
顺便说一句,如果您看到 const 约束,那么连接的代码更容易阅读,因为代码可以执行的操作的可能性受到很大限制。这是 C++ 比 C# 或 Java 更容易阅读的众多原因之一。
I think I can formulate this much simpler. When
footype
is not a template parameter:const footype &
in the signature is a guarantee for the caller ofthe function and a constraint for the implementer of the function.
const footype
on the other hand is only a constraint for theimplementer and irrelevant to the caller.
When
footype
is a template parameter, then the rules can only be tested against the individual template instantiations.BTW, if you see const constraints, then the connected code is much easier to read because the possibilities of what the code can do is much restricted. This is one of the many reasons why C++ is easier to read than C# or Java.