const 用于非引用参数

发布于 2024-11-09 15:42:29 字数 212 浏览 0 评论 0原文

如果我有这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

薔薇婲 2024-11-16 15:42:29

如果您知道函数在执行过程中不需要修改其值,那么将其声明为 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 writing if (aIn = someValue).

差↓一点笑了 2024-11-16 15:42:29

我有时(不经常)这样做,当有诱惑就地修改 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 on aIn remaining unchanged throughout. It tends to be a close call though.

等往事风中吹 2024-11-16 15:42:29

原因很丰富:您希望编译器在赋值左侧出现值传递参数时发出警告/错误。
这有点麻烦,在那些不太了解 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).

你的笑 2024-11-16 15:42:29

这将使该函数的值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.

纸短情长 2024-11-16 15:42:29

不,将 const 添加到标量按值调用参数是没有意义的,只会造成混乱。

No, adding const to a scalar call-by-value parameter is meaningless and will only be confusing.

甲如呢乙后呢 2024-11-16 15:42:29

我更喜欢向输入参数添加 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.

愚人国度 2024-11-16 15:42:29

我想我可以更简单地表述这一点。当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 of
    the function and a constraint for the implementer of the function.

  • const footype on the other hand is only a constraint for the
    implementer 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文