C++ 之前的常量还是之后的常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5503352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:18:27  来源:igfitidea点击:

Const before or const after?

c++csyntaxconst

提问by AJG85

To start you probably know that constcan be used to make either an object's data or a pointer not modifiable or both.

首先,您可能知道它const可用于使对象的数据或指针不可修改或两者兼而有之。

const Object* obj; // can't change data
Object* const obj; // can't change pointer
const Object* const obj; // can't change data or pointer

However you can also use the syntax:

但是,您也可以使用以下语法:

Object const *obj; // same as const Object* obj;

The only thing that seems to matter is which side of the asterisk you put the constkeyword. Personally I prefer to put conston the left of the type to specify it's data is not modifiable as I find it reads better in my left-to-right mindset but which syntax came first?

唯一似乎重要的是您将const关键字放在星号的哪一侧。就我个人而言,我更喜欢把它const放在类型的左边来指定它的数据是不可修改的,因为我发现它在我从左到右的心态中读起来更好,但哪种语法先出现?

More importantly why is there two correct ways of specifying constdata and in what situation would you prefer or need one over the other if any?

更重要的是,为什么有两种正确的const数据指定方式,以及在什么情况下您更喜欢或需要一种方式而不是另一种方式?

Edit:

编辑:

So it sounds like this was an arbitrary decision when the standard for how compilers should interpret things was drafted long before I was born. Since constis applied to what is to the left of the keyword (by default?) I guess they figured there was no harm in adding "shortcuts"to apply keywords and type qualifiers in other ways at least until such a time as the declaration changes by parsing a * or & ...

所以这听起来像是一个武断的决定,因为编译器应该如何解释事物的标准早在我出生之前就已经起草好了。由于const应用于关键字左侧的内容(默认情况下?)我猜他们认为添加“快捷方式”以其他方式应用关键字和类型限定符没有害处,至少直到声明更改为解析 * 或 & ...

This was the case in C as well then I'm assuming?

在 C 中也是如此,然后我假设?

采纳答案by Heath Hunnicutt

why is there two correct ways of specifying constdata and in what situation would you prefer or need one over the other if any?

为什么有两种指定const数据的正确方法,在什么情况下您更喜欢或需要一种方法(如果有)?

Essentially, the reason that the position of constwithin specifiers prior to an asterisk does not matter is that the C grammar was defined that way by Kernighan and Ritchie.

本质上,inconst内说明符在星号之前的位置无关紧要的原因是 C 语法是由 Kernighan 和 Ritchie 以这种方式定义的。

The reason they defined the grammar in this way was likely that their C compiler parsed input from left-to-right and finished processing each token as it consumed that. Consuming the *token changes the state of the current declaration to a pointer type. Encountering constafter *means the constqualifier is applied to a pointer declaration; encountering it prior to the *means the qualifier is applied to the data pointed to.

他们以这种方式定义语法的原因可能是他们的 C 编译器从左到右解析输入,并在每个标记使用它时完成处理。使用*令牌会将当前声明的状态更改为指针类型。遇到constafter*表示将const限定符应用于指针声明;在*将限定符应用于指向的数据的方法之前遇到它。

Because the semantic meaning does not change if the constqualifier appears before or after the type specifiers, it is accepted either way.

因为如果const限定符出现在类型说明符之前或之后,语义不会改变,所以无论哪种方式都可以接受。

A similar sort of case arises when declaring function pointers, where:

声明函数指针时会出现类似的情况,其中:

  • void * function1(void)declares a function which returns void *,

  • void (* function2)(void)declares a function pointerto a function which returns void.

  • void * function1(void)声明一个返回的函数void *

  • void (* function2)(void)声明一个指向函数的函数指针,该函数返回void.

Again the thing to notice is that the language syntax supports a left-to-right parser.

再次需要注意的是语言语法支持从左到右的解析器。

回答by horstforst

The rule is:

规则是:

const applies to the thing left of it. If there is nothing on the left then it applies to the thing right of it.

const 适用于它剩下的东西。如果左边没有任何东西,那么它适用于它右边的东西。

I prefer using const on the right of the thing to be const just because it is the "original" way const is defined.

我更喜欢在事物的右侧使用 const 来成为 const ,因为它是 const 定义的“原始”方式。

But I think this is a very subjective point of view.

但我认为这是一个非常主观的观点。

回答by Matt Davis

I prefer the second syntax. It helps me keep track of 'what' is constant by reading the type declaration from right to left:

我更喜欢第二种语法。它帮助我通过从右到左阅读类型声明来跟踪“什么”是常量:

Object * const obj;        // read right-to-left:  const pointer to Object
Object const * obj;        // read right-to-left:  pointer to const Object
Object const * const obj;  // read right-to-left:  const pointer to const Object

回答by Bo Persson

The order of the keywords in a declaration isn't all that fixed. There are many alternatives to "the one true order". Like this

声明中关键字的顺序并不是固定的。“一个真正的订单”有很多选择。像这样

int long const long unsigned volatile i = 0;

or should it be

或者应该是

volatile unsigned long long int const i = 0;

??

??

回答by James Kanze

The first rule is to use whichever format your local coding standards requires. After that: putting the constin front leads to no end of confusion when typedefs are involved, e.g.:

第一条规则是使用本地编码标准要求的任何格式。之后:const当涉及 typedef 时,将 the放在前面会导致混乱无止境,例如:

typedef int* IntPtr;
const IntPtr p1;   // same as int* const p1;

If your coding standard allows typedef's of pointers, then it really should insist on putting the const after the type. In every case but when applied to the type, const must follow what it applies to, so coherence also argues in favor of the const after. But local coding guidelines trump all of these; the difference isn't normally important enough to go back and change all of the existing code.

如果您的编码标准允许 typedef 的指针,那么它确实应该坚持将 const 放在类型之后。在任何情况下,但当应用于类型时,const 必须遵循它所适用的内容,因此一致性也支持后面的 const。但本地编码指南胜过所有这些;差异通常不足以返回并更改所有现有代码。

回答by Nick Westgate

There are historical reasons that either left or right is acceptable. Stroustrup had added const to C++ by 1983, but it didn't make it to C until C89/C90.

左或右都可以接受是有历史原因的。Stroustrup 于 1983 年将 const 添加到 C++ 中,但直到 C89/C90 才将其添加到 C。

In C++ there's a good reason to always use const on the right. You'll be consistent everywhere because const member functions mustbe declared this way:

在 C++ 中,有充分的理由始终在右侧使用 const。您将在任何地方保持一致,因为必须以这种方式声明const 成员函数:

int getInt() const;