const int * p与int const * p-类型之后的const是否可以接受? [英] const int *p vs. int const *p - Is const after the type acceptable?

查看:132
本文介绍了const int * p与int const * p-类型之后的const是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事在他启发的问题上是0分,共2分( 1 2 ),所以我想我应该给他有机会赶上.

My co-worker is 0 for 2 on questions he has inspired (1, 2), so I thought I'd give him a chance to catch up.

我们最近的分歧是关于在声明中放置"const"的样式问题.

Our latest disagreement is over the style issue of where to put "const" on declarations.

他认为它应该在类型的前面或指针之后.理由是,这通常是其他所有人所做的,其他样式容易引起混淆.因此,指向常量int的指针和指向int的常量指针分别是:

He is of the opinion that it should go either in front of the type, or after the pointer. The reasoning is that this is what is typically done by everyone else, and other styles are liable to be confusing. Thus a pointer to a constant int, and a constant pointer to int would be respectively:

const int *i;
      int * const i;

但是,我还是很困惑.我需要一致且易于理解的规则,而我能理解"const"的唯一方法是将它修改后的内容放在" "之后.有一个例外,它可以将它放在最终类型的前面,但这是一个例外,因此,如果我不使用它,对我来说更容易.

However, I'm confused anyway. I need rules that are consistent and easy to understand, and the only way I can make sense of "const" is that it goes after the thing it is modifying. There's an exception that allows it to go in front of the final type, but that's an exception, so it's easier on me if I don't use it.

因此,指向常量int的指针和指向int的常量指针分别是:

Thus a pointer to a constant int, and a constant pointer to int would be respectively:

int const * i;
int * const i;

作为一项附加好处,以这种方式进行操作使更深层次的间接理解变得更容易理解.例如,指向int的常量指针的指针显然应该是:

As an added benefit, doing things this way makes deeper levels of indirection easier to understand. For example, a pointer to a constant pointer to int would clearly be:

int * const * i;

我的争论是,如果有人只是以自己的方式学习它,那么弄清楚以上内容的工作原理将很容易.

My contention is that if someone just learns it his way, they'll have little trouble figuring out what the above works out to.

这里的最终问题是,他认为将const放在int之后是如此丑陋,而且对可读性有害,因此应在样式指南中予以禁止.当然,我认为该指南应该建议按照我的方式进行操作,但是无论哪种方式,我们都不应该禁止一种方法.

The ultimate issue here is that he thinks that putting const after int is so unspeakably ugly, and so harmful to readability that it should be banned in the style guide. Of course, I think if anything the guide should suggest doing it my way, but either way we shouldn't be banning one approach.

修改: 我得到了很多很好的答案,但没有一个能真正直接解决我的最后一段(最终问题").许多人争辩说要保持一致性,但是在这种情况下是否是如此可取,以至于禁止以另一种方式做到这一点是一个好主意,而不是只是劝阻它?

I've gotten a lot of good answers, but none really directly address my last paragraph ("The ultimate issue"). A lot of people argue for consistency, but is that so desirable in this case that it is a good idea to ban the other way of doing it, rather that just discouraging it?

推荐答案

最重要的是 consistency .如果没有任何编码指南,则选择其中一个并坚持下去.但是,如果您的团队已经有了事实上的标准,请不要更改它!

The most important thing is consistency. If there aren't any coding guidelines for this, then pick one and stick with it. But, if your team already has a de facto standard, don't change it!

也就是说,我认为到目前为止更常见的是

That said, I think by far the more common is

const int* i;
int* const j;

因为大多数人都写

const int n;

代替

int const n;

旁注-读取指针const ness的一种简单方法是从右侧开始读取声明.

A side note -- an easy way to read pointer constness is to read the declaration starting at the right.

const int* i; // pointer to an int that is const
int* const j; // constant pointer to a (non-const) int
int const* aLessPopularWay; // pointer to a const int

这篇关于const int * p与int const * p-类型之后的const是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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