我想了解为什么not_a_ref
不是一个参考。我明白,我可以通过auto &
将其作为参考。我在标准中挖了一段时间,但是迷了路,无法弄清楚这种行为的定义。为什么auto的初始值设定项是引用时不是引用?
例子:
#include <vector>
#include <iostream>
#include <type_traits>
std::vector<int> stuff;
std::vector<int>& get_stuff()
{
return stuff;
}
int main()
{
auto not_a_ref = get_stuff();
if(std::is_reference<decltype(not_a_ref)>::value)
std::cout << "is_reference true" << std::endl;
else
std::cout << "is_reference false" << std::endl;
if(¬_a_ref != &stuff)
std::cout << "definately not a reference" << std::endl;
return 0;
}
如果它按照您认为的那样工作,那将会很烦人。这会让'auto'变得不那么有用,因为添加一个引用很容易,但删除一个就很麻烦。 –
我绝不会暗示它应该是任何其他方式,而只是试图了解标准中如何规定此行为。它是我个人目标的一部分,能够通过阅读标准来了解这类事情 - 但我陷入了困境。 – Zac
[这里是论文。](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf)我不喜欢看标准,所以我只是使用Google搜索。第一页有一个函数的例子,返回一个对float的引用,并且类型被推断为float。 –