C ++引用 - 它们只是语法糖? [英] C++ references - are they just syntactic sugar?

查看:614
本文介绍了C ++引用 - 它们只是语法糖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是C ++引用只是语法糖,还是在某些情况下提供任何加速?

Is a C++ reference just syntactic sugar, or does it offer any speed ups in certain cases?

例如,一个call-by-pointer ,并且对于一个调用引用也是如此。基本机制看起来是相同的。

For example, a call-by-pointer involves a copy anyway, and that seems to be true about a call-by-reference as well. The underlying mechanism appears to be the same.

编辑:大约六个答案和许多评论后。我仍然认为参考只是合成糖。

After about six answers and many comments. I am still of the opinion references are just syntatic sugar. If people could answer in a straight yes or no, and if someone could do an accepted answer?

推荐答案

参考文献有更强的保证,而不是指针,所以编译器可以更积极地优化。我最近看到GCC内联多个嵌套调用通过函数引用完美,但没有一个通过函数指针(因为它不能证明指针总是指向同一个函数)。

References have stronger guarantees than pointers, so the compiler can optimize more aggressively. I've recently seen GCC inline multiple nested calls through function references perfectly, but not a single one through function pointers (because it couldn't prove that the pointer was always pointing at the same function).

如果引用结束存储在某处,它通常占用与指针相同的空间。这不是说,再次,它将像一个指针一样使用:如果它知道引用绑定到哪个对象,编译器可能会通过它。

If the reference ends up stored somewhere, it typically takes the same space as a pointer. That is not to say, again, that it will be used like a pointer : the compiler may well cut through it if it knows which object the reference was bound to.

这篇关于C ++引用 - 它们只是语法糖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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