C++ const 引用参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035795/
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
const reference parameters
提问by Evan Teran
Is there a difference between the following declarations?
以下声明之间有区别吗?
void somefunc(const Person &p);
void somefunc(Person const &p);
回答by Evan Teran
there is no difference. const
binds to the type to its left...unless it is the first part of the declaration in which case it binds to the right.
有没有什么区别。const
绑定到它左边的类型......除非它是声明的第一部分,在这种情况下它绑定到右边。
See: https://isocpp.org/wiki/faq/const-correctness#const-ref-alt
请参阅:https: //isocpp.org/wiki/faq/const-correctness#const-ref-alt
Personally, I find that const T &x
reads better. According to this, Bjarne also prefers to put the const
first. Specifically because the keyword was originally going to be called readonly
and readonly int x
reads better :-P.
就我个人而言,我觉得const T &x
读起来更好。根据此,Bjarne的也喜欢放const
第一。特别是因为该关键字最初将被调用readonly
并且readonly int x
读取效果更好:-P。
回答by AraK
Yes there is! the first one is more readable :)
就在这里!第一个更具可读性:)
回答by abelenky
See this http://www.parashift.com/c++-faq-lite/const-correctness.html
看到这个http://www.parashift.com/c++-faq-lite/const-correctness.html
(See new link instead : https://isocpp.org/wiki/faq/const-correctness#const-ref-alt)
(请参阅新链接:https: //isocpp.org/wiki/faq/const-correctness#const-ref-alt)
Specifically 18.6 to 18.8.
特别是 18.6 到 18.8。
[18.6] What does "const Fred& x" mean?
It means x aliases a Fred object, but x can't be used to change that Fred object.
[18.7] Does "Fred& const x" make any sense?
No, it is nonsense.
[18.8] What does "Fred const& x" mean?
Fred const& x is functionally equivalent to const Fred& x. However, the real question is which should be used.
[18.6] “const Fred&x”是什么意思?
这意味着 x 是 Fred 对象的别名,但 x 不能用于更改该 Fred 对象。
[18.7] "Fred& const x" 有意义吗?
不,这是胡说八道。
[18.8] "Fred const& x" 是什么意思?
Fred const& x 在功能上等同于 const Fred& x。然而,真正的问题是应该使用哪个。
Read the rest of the article for more info. But essentially, it says they are equivalent, and you can use either one as you see fit. However, you should pick one and stick with it, to avoid future confusion.
阅读本文的其余部分以获取更多信息。但本质上,它表示它们是等效的,您可以根据需要使用任何一种。但是,您应该选择一个并坚持下去,以避免将来出现混乱。
回答by abelenky
It really is a matter of taste.
这真的是一个品味问题。
If read from right to left, "Person const & x
" reads "x is a reference to a constant Person."
如果从右到左阅读,“ Person const & x
”读作“x 是对常量 Person 的引用”。
This sounds better than "const Person & x
", which would be "x is a reference to a Person, which is constant."
这听起来比“ const Person & x
”更好,后者是“x 是对 Person 的引用,它是常量。”
So if one is familiar with the right-to-left reading direction of variable declarations, one perhaps would prefer the first one.
因此,如果您熟悉变量声明的从右到左阅读方向,您可能会更喜欢第一个。
回答by Cort Ammon
Both are the same.
两者都是一样的。
const Person & x is currently the most popular, because English puts adjectives before the nouns, and that makes it "more English like."
const Person & x 目前最流行,因为英语将形容词放在名词之前,这使得它“更像英语”。
The alternative form, Person const & x, was suggested as a less idiomatic form. Their argument was that that form can always be parsed from right to left, "x is a reference to a const Person." The argument was that clarity of parsing was more important than idiomatic structure.
替代形式 Person const & x 被建议为一种不太惯用的形式。他们的论点是这种形式总是可以从右到左解析,“x 是对 const Person 的引用。” 争论的焦点是解析的清晰度比惯用结构更重要。
Unfortunately for those people, their form doesn't handle everything. Once you start seeing arrays and function pointers and other exotic forms, the rules start to break down. char *str[10] cannot be written in right to left form because the array has to be on the right side. void (*signal(int, void (*fp)(int)))(int) makes the right to left people cringe in pain.
不幸的是,对于这些人来说,他们的形式并不能处理一切。一旦你开始看到数组和函数指针以及其他奇异的形式,规则就开始崩溃了。char *str[10] 不能以从右到左的形式写入,因为数组必须在右侧。void (*signal(int, void (*fp)(int)))(int) 使从右到左的人在痛苦中畏缩。
Only rule has ever cropped up to make sense of all types, the Clockwise Spiral rule: http://c-faq.com/decl/spiral.anderson.html
唯一出现的规则可以理解所有类型,顺时针螺旋规则:http: //c-faq.com/decl/spiral.anderson.html
Usually, once someone sees the the clockwise spiral, all hopes of a a clear parsing rule goes out the window. Most people then accept that const Person& x is probably the best way to go!
通常,一旦有人看到顺时针螺旋,所有对清晰解析规则的希望都破灭了。大多数人然后接受 const Person& x 可能是最好的方法!