在C ++中是“const”后类型ID可以接受? [英] In C++ is "const" after type ID acceptable?

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

问题描述

我的同事在他所启发的问题上有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?

推荐答案

最重要的是一致性。如果没有任何编码指南,请选择一个并坚持使用。但是,如果你的团队已经有了事实上的标准,不要改变它!

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

这篇关于在C ++中是“const”后类型ID可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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