为什么需要引用 const 来避免复制参数
根据 Wikipedia,函数调用不会复制 参数对
:const
类型的引用
void f_slow(BigObject x);
void f_fast(const BigObject& x);
f_slow(y); // slow, copies y to parameter x
f_fast(y); // fast, gives direct read-only access to y
为什么引用需要是 const
?非 const 引用不会完成相同的任务吗:
void f_should_be_fast(BigObject& x);
According to Wikipedia, function calls don't copy parameters which are are references
to a const
type:
void f_slow(BigObject x);
void f_fast(const BigObject& x);
f_slow(y); // slow, copies y to parameter x
f_fast(y); // fast, gives direct read-only access to y
Why does the reference need to be const
? Wouldn't a non-const
reference accomplish the same:
void f_should_be_fast(BigObject& x);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,任何类型的参考都可以。使其成为 const 使其更加灵活,因为它可以接受 const 变量或临时变量作为参数,并记录(并强制)您不修改参数的意图。
Yes, any kind of reference will do. Making it
const
makes it more flexible as it can accept const variables or temporaries as parameters, and documents (and enforces) your intent not to modify the parameter.const
确实不会影响速度。它只是阻止函数修改原始对象,使其类似于按值传递语义(但显然不完全相同)。请注意,这也可能值得阅读:想要速度吗?按值传递
The
const
doesn't affect the speed, really. It just prevents the function from modifying the original object, making it similar to pass-by-value semantics (but not identical, obviously).Note that it may also be worth reading this: Want Speed? Pass by Value
对于非 const 的引用,您无法将 const 对象(或 const 对象的引用)传递到函数中,这将导致 const 正确性更差。但这不会影响执行速度。
With a reference to non-const you can't pass a
const
object (or a reference to const object) into the function and this will lead to worse const correctness. It won't affect execution speed however.const
关键字表示函数/方法中的参数值不会更改。这可能会带来更好的性能,因为编译器可以通过调整代码来提高性能,尽管这是不被允许的。The
const
keyword signals that the parameter value will not be changed in the function/method. This might result in a better performance, because the compiler can adjust the code with some performance improvements, though this is not granted.是的,它会完成相同的任务,但是 const 是向编译器发出的一个信号,如果对象永远不会被修改,它可以进行优化。
Yes it would accomplish the same, but the
const
is a signal to the compiler that can make optimizations if an object is never going to be modified.在这种情况下,引用只是不可修改,因为它是用const限定的。通过不限定它const,它只是一个可修改的引用。唯一关心的是引用参数是否可修改。 AFAIK,速度与 const 资格无关。
Reference in this case is just non-modifiable because of qualifying it with const. By not qualifying it const, it is just a modifiable reference. The only concern is the whether the reference argument is modifiable or not. Speed has nothing to do with const qualification, AFAIK.
使用 const 引用可以为您提供Const 正确性.
简而言之:
这意味着引用引用的类型不能在函数内部修改。同时,您不会产生按值传递的开销。
它可以帮助诚实的程序员避免犯诚实的错误和错误。提供编译器执行任何优化(完全)。
Using a const reference provides you Const correctness.
In a nutshell:
It means that the type to which the reference refers cannot be modified inside the function & at the same time you do not incur the overhead of pass by value.
It helps honest programmers from making honest mistakes & provides the compiler to perform any optimization(it at all) in can perform any.