Zac*_*Zac 9 c++ standards c++11
我试图理解为什么not_a_ref不是参考.我明白我可以把它作为参考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;
}
Run Code Online (Sandbox Code Playgroud)
从C++ 11草案,7.1.6.4(auto说明符)第6段:
根据函数调用(14.8.2.1),使用模板参数推导的规则确定推导出的变量d的类型.
从14.8.2.1(从函数调用中推导出模板参数)第3段:
如果P是引用类型,则P引用的类型用于类型推导.
因此,对于类型推导,仅忽略引用auto.
请注意此规则与此规则的不同之处decltype.
更新:请参阅下面的评论,因为我认为14.8.2.1第3段不适用.