C++:const 引用,类型说明符之前与之后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3694630/
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
C++: const reference, before vs after type-specifier
提问by eisbaw
What is the difference between the arguments in:
以下参数之间的区别是什么:
int foo1(const Fred &arg) {
...
}
and
和
int foo2(Fred const &arg) {
...
}
? I don't see this case covered in the parashift FAQ.
? 我在 parashift 常见问题解答中没有看到这种情况。
采纳答案by Sean Fausett
No differenceas const is read right-to-left with respect to the &, so both represent a reference to an immutable Fred instance.
与 & 从右到左读取 const没有区别,因此两者都表示对不可变 Fred 实例的引用。
Fred& const
would mean the reference itself is immutable, which is redundant; when dealing with const pointersboth Fred const*
and Fred* const
are valid but different.
Fred& const
将意味着引用本身是不可变的,这是多余的;处理const 指针时, Fred const*
和Fred* const
都有效但不同。
It's a matter of style, but I prefer using const
as a suffix since it can be applied consistently including const member functions.
这是一个风格问题,但我更喜欢使用const
作为后缀,因为它可以一致地应用,包括const member functions。
回答by jamesdlin
Behavior
行为
There is no semantic difference between const T&
and T const&
; the language treats them as the same type. (The same thing applies to const T*
and T const*
.)
const T&
和之间没有语义差异T const&
;该语言将它们视为同一类型。(同样的事情适用于const T*
和T const*
。)
As a matter of style
作为风格问题
Regarding which you should prefer stylistically, however, I'll dissent from a lot of the other answers and prefer const T&
(and const T*
):
但是,关于您应该在风格上更喜欢哪个,我会反对许多其他答案,并且更喜欢const T&
(和const T*
):
const T&
is the style used in Stroustrup's The C++ Programming Languagebook.const T&
is the style used in the C++ standard itself.const T*
is the style used in K&R's The C Programming Languagebook.const T*
is the style used in the C standard.- Due to the above factors, I think
const T&
/const T*
have way more inertia thanT const&
/T const*
.const T&
/const T*
empirically seem way more common to me thanT const&
/T const*
in all of the C++ and C code that I've seen. I think following common practices is more readable than dogmatically adhering to right-to-left parsing rules. - With
T const*
, it seems easier to misplace the*
asT* const
(especially if people aren't as accustomed to it). In contrast,const* T
is not legal syntax.
const T&
是 Stroustrup 的The C++ Programming Language一书中使用的风格。const T&
是 C++ 标准本身使用的样式。const T*
是 K&R 的The C Programming Language一书中使用的风格。const T*
是 C 标准中使用的样式。- 由于上述因素,我认为
const T&
/const T*
比T const&
/具有更大的惯性T const*
。const T&
/在我所见过的所有 C++ 和 C 代码中,/const T*
对我来说似乎比T const&
/更常见T const*
。我认为遵循常见的做法比教条地坚持从右到左的解析规则更具可读性。 - 使用
T const*
,将*
as放错位置似乎更容易T* const
(特别是如果人们不习惯它)。相比之下,const* T
不是合法的语法。
What about the right-to-left parsing rule?
那么从右到左的解析规则呢?
Regarding the whole right-to-left parsing argument that people seem to love to use: as I mentioned in a comment to another answer, const T&
reads fine right-to-left too. It's a reference to a T constant. "T" and "constant" each can work as an adjective or a noun. (Additionally, reading T const*
right-to-left can be ambiguous since it could be incorrectly interpreted as "pointer constant toT" instead of as "pointer toconstant T".)
关于人们似乎喜欢使用的整个从右到左的解析论点:正如我在对另一个答案的评论中提到的,const T&
从右到左也很好读。它是对 T 常量的引用。“T”和“常数”都可以用作形容词或名词。(此外,读取T const*
从右到左可以是不明确的,因为它可能被错误地解释为“指针常数到T”,而不是称为“指针以恒定T”)。
回答by Chubsdad
Though they are one and the same, to retain consistency with the RIGHT-LEFTrule about parsing C and C++ declarations, it is better to write Fred const &arg
尽管它们是一回事,但为了与有关解析 C 和 C++ 声明的RIGHT-LEFT规则保持一致,最好编写Fred const &arg
Also refer thisfor developing more understanding about declarations, qualifiers and declarators.
另请参阅此内容以加深对声明、限定符和声明符的理解。
回答by Default
Both work, and hereis the explanation from the man who wrote it.
To quote him:
两者都有效,这是编写它的人的解释。
引用他的话:
Why? When I invented "const" (initially named "readonly" and had a corresponding "writeonly"), I allowed it to go before or after the type because I could do so without ambiguity.
为什么?当我发明“const”(最初命名为“readonly”并有一个相应的“writeonly”)时,我允许它在类型之前或之后,因为我可以这样做而不会产生歧义。
回答by Prasoon Saurav
No difference, both are syntactically and semantically same.
没有区别,两者在语法和语义上都是相同的。
回答by Nirocfz
回答by Cedric H.
References doesn't work the same way as pointers: for pointers you can have 'const pointers' (type * const p
) and 'pointer to const' (const type * p
or type const * p
).
引用与指针的工作方式不同:对于指针,您可以使用“常量指针”(type * const p
)和“指向常量的指针”(const type * p
或type const * p
)。
But you don't have this for references: a reference will always refer to the same object; in that sense you can consider that 'references' are 'const references' (the same way you can have 'const pointers').
但是你没有这个用于引用:一个引用总是指向同一个对象;从这个意义上说,您可以认为“引用”是“常量引用”(与拥有“常量指针”的方式相同)。
Therefore something like 'type & const ref' is not legal. You can only have 'reference to type' (type &ref
) and 'reference to constant type' (const type &ref
or type const &ref
; both are exactly equivalent).
因此,像 'type & const ref' 这样的东西是不合法的。您只能拥有“对类型的引用” ( type &ref
) 和“对常量类型的引用”(const type &ref
或type const &ref
; 两者完全等效)。
One last thing: even if const type
sounds more correct in English, writing type const
allows a more systematic understanding of declarations "right to left" : int const & ref
can be read has 'ref is a reference to a constant int'. Or more complicated example: int const * const & ref
, ref is a reference to a constant pointer to a constant int.
最后一件事:即使const type
在英语中听起来更正确,写作也type const
可以更系统地理解“从右到左”的声明:int const & ref
可以阅读 has 'ref is a reference to a constant int'。或者更复杂的例子:int const * const & ref
, ref 是一个指向常量 int 的常量指针的引用。
Conclusion: in your question, both are exactly equivalent.
结论:在您的问题中,两者完全相同。