const-correctness的解释是什么? [英] What is the definition of const-correctness?
问题描述
我认为常量正确性的概念定义得很好,但是当我与其他人讨论时,我们对它的含义似乎有不同的看法。有人说这是关于在尽可能多的位置使用 const注释的程序。其他人则是在且仅当使用const注释的地方没有违反constness的情况(即,编译器会为您检查的属性)时,才将程序定义为const正确。
I thought the concept of "const-correctness" was pretty well defined, but when I talked to other people about it, it seemed we had different ideas of what it means. Some people say it's about a program having the "const" annotations in as many places as possible. Others define a program as const-correct if and only if there are no violations of constness where the const annotation is used (ie. it's a property that the compiler checks for you).
所以我想知道以下哪个函数是const正确的:
So I'd like to know, which of these functions are const correct:
struct Person {
string getName1() const { return _name; }
string getName2() { return _name; }
string getName3() const { _name = "John"; return _name; }
string getName4() { _name = "John"; return _name; }
string _name;
};
我在互联网上搜索过定义,但找不到真正的答案,我也怀疑可能会发生 citogenesis 的情况。那么有人可以为定义提供任何可靠的引用吗?
I've searched on the Internet for definitions but couldn't really find definite answer, and I also have suspicions that there might be a case of citogenesis at play. So could anyone provide any solid citation for a definition?
推荐答案
在我看来,常量正确性的意思是:
In my view "const correctness" means:
所有不打算修改的内容都应标记为
const
。
这意味着编译器可以在您输入错误时告诉您。 (在某些情况下,它可能还会对优化产生影响,但这更多是次要的收益,取决于许多其他因素。)
This means that the compiler can tell you when you make a mistake. (It also possibly has implications for optimisations in certain conditions, but that's more of a secondary benefit and depends on a lot of other factors)
很有用:
-
成员函数可以标记为
const
,如下所示:你的例子。这意味着您从该函数内部访问成员变量就像变量本身是const
。 (这是您显示的示例,尽管getName3()
不起作用)
Member functions can be marked
const
, as in your examples. This means that your accesses to member variables from within that function will be as though the variables themselves wereconst
. (This is the example you've shown, althoughgetName3()
won't work)
免费变量,局部变量和成员变量本身也可以标记为 const
-初始化后就不能更改。示例-局部变量:
"Free" variables, local and member variables themselves may also be marked const
- once initialised they may not be changed. Example - local variable:
int f(int i) {
const int square = i*i;
// do something here that uses, but doesn't change square
return square;
}
或自由变量:
extern const double PI; // set somewhere else but not changed.
或成员变量:
class foo {
const int state; // can't be changed once constructed
foo(int s) : state(i) {}
};
函数自变量也可以标记为 const
,该定义仍可以匹配非const声明:
Function arguments can also be marked const
, the definition can match a non-const declaration still:
void f(int i);
void f(const int i) {
// the fact that i is const is an implementation detail of f here
}
作为旁注,在某些情况下, const
引用的正确性是必需的:
As a side note const
correctness for references is required in some cases:
void g(const std::string& s);
void f(std::string& s);
这两个可以在更多地方使用:
Of these two one can be used in more places:
g("hi"); // Fine
f("hi"); // Not fine - you can't bind a temporary to a reference
当然,如果您 meant 更改 s
然后通过临时变量还是没有任何意义的。
Of course if you meant to change s
then passing a temporary in makes little sense anyway.
这篇关于const-correctness的解释是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!