如果我有一个函数:
Foo& Bar()
{
return /// do something to create a non-temp Foo here and return a reference to it
}
为什么会出现这种情况:
auto x = Bar(); /// probably calls copy ctor - haven't checked
不同于这个吗?
auto &x = Bar(); /// actually get a reference here
(实际上,我预期第二个版本会得到一个引用的引用,这没有多少意义。)
如果我明确指定了x
的类型为值或引用,我会得到我期望的结果(当然)。不过,我希望auto
编译成Bar()
的返回类型,而在这种情况下,它是一个引用。
这里是否存在Foo
和Foo&
之间的隐式转换?
(接受规范参考,尽管我已经厌倦了阅读委员会说话。)
(第二次使用时间机器将默认按引用传递C++。使用#pragma compatibility
触发器编译C代码。ARGH。)