值参数的Const正确性 [英] Const correctness for value parameters

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

问题描述

我知道有几个关于const正确性的问题,其中声明一个函数的声明和它的定义不需要同意值参数。这是因为值参数的常数只在函数内部起作用。这很好:

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine:

// header
int func(int i);

// cpp
int func(const int i) {
    return i;
}

这是真的最好的做法吗?因为我从来没有见过任何人做到。我已经在其他地方已经看过这个引号(不确定来源),这已经讨论过了:

Is doing this really a best practice? Because I've never seen anyone do it. I've seen this quotation (not sure of the source) in other places this has been discussed:


其实, ,函数签名是相同的,无论你是否在值参数前面包含这个常量。

"In fact, to the compiler, the function signature is the same whether you include this const in front of a value parameter or not."

避免在函数声明中使用const传递值参数。

"Avoid const pass-by-value parameters in function declarations. Still make the parameter const in the same function's definition if it won't be modified."

第二段说,不要把参数const放在同一个函数的定义中。 const声明。我假设这是因为值参数的constness作为接口定义的一部分是无意义的。这是一个实现细节。

The second paragraph says to not put the const in the declaration. I assume this is because the constness of a value parameter is meaningless as part of a interface definition. It is an implementation detail.

基于这个建议,是否也建议指针参数的指针值? (对于引用参数,它是无意义的,因为您不能重新分配引用。)

Based on this recommendation, is it also recommended for the pointer values of pointer parameters? (It is meaningless on a reference parameter since you can't reassign a reference.)

// header
int func1(int* i);
int func2(int* i);

// cpp
int func1(int* i) {
    int x = 0;

    *i = 3; // compiles without error
    i = &x; // compiles without error

    return *i;
}
int func2(int* const i) {
    int x = 0;

    *i = 3; // compiles without error
    i = &x; // compile error

    return *i;
}

摘要:一些逻辑错误。这是最佳实践吗?你去极端的将const从头文件中删除吗?它是否与const指针值一样有用?为什么或为什么不?

Summary: Making value parameters is useful to catch some logic errors. Is it a best practice? Do you go to the extreme of leaving the const out of the header file? Is it just as useful to const pointer values? Why or why not?

一些引用

http://stackoverflow.com/questions/1554750/c-const-keyword-use-liberally
http://stackoverflow.com/questions/117293/use-of-const- for-function-parameters

const值参数有用的示例:

An example of when const value parameters are useful:

bool are_ints_equal(const int i, const int j) {
    if (i = j) { // without the consts this would compile without error
        return true;
    } else {
        return false;
    }
    // return i = j; // I know it can be shortened
}


推荐答案

p>我的看法:

这不是一个坏主意,但问题是微不足道,你的精力可能会更好地用于其他事情。

It's not a bad idea, but the issue is minor and your energy might be better spent on other things.

在你的问题中,你提供了一个很好的例子,当它可能会捕获错误,但偶尔你也最终做这样的事情:

In your question you provided a good example of when it might catch an error, but occasionally you also end up doing something like this:

void foo(const int count ...)
{
   int temp = count;  // can't modify count, so we need a copy of it
   ++temp;

   /* ... */
}

利弊是微小的。

这篇关于值参数的Const正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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