const 之前还是 const 之后? [英] Const before or const after?

查看:16
本文介绍了const 之前还是 const 之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;

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

The only thing that seems to matter is which side of the asterisk you put the const keyword. Personally I prefer to put const on 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 数据?在什么情况下,如果有的话,您更喜欢或需要其中一种?

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

所以在我出生之前很久就起草了编译器应该如何解释事物的标准时,这听起来像是一个武断的决定.由于 const 应用于关键字左侧的内容(默认情况下?)我猜他们认为添加 shortcuts" 来应用关键字和至少在通过解析 * 或 & 来更改声明之前,以其他方式类型限定符...

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 const is 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 & ...

C 中也是这种情况,然后我假设?

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

推荐答案

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

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

基本上,说明符中 const 的位置在星号之前的位置无关紧要,因为 C 语法是由 Kernighan 和 Ritchie 定义的.

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

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

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 const after * means the const qualifier is applied to a pointer declaration; encountering it prior to the * means the qualifier is applied to the data pointed to.

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

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

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

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

  • void * function1(void) 声明一个返回 void *,

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

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

void (* function2)(void) declares a function pointer to a function which returns void.

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

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

这篇关于const 之前还是 const 之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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