繁体   English   中英

从自动输入参数推导返回类型

[英]deducing return type from auto input argument

为什么以下内容无法编译? 编译器不能从输入中推断出返回类型吗?

不要完全遵循所显示的类型转换错误。

auto func(auto &x)
{
 return x[0];
}

int main()
{
  vector<int> v = { 1, 2, 3};
  (void)func(v);
}

$ g++ -std=c++1z auto.cc 
auto.cc: In instantiation of ‘auto func(auto:1&) [with auto:1 = std::vector<int>]’:
auto.cc:15:16:   required from here
auto.cc:8:14: error: could not convert ‘(& x)->std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(0ul)’ from ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’ to ‘std::vector<int>’
    return x[0];

auto作为函数参数类型不是标准的C ++ 尽管如此, g++ 7 (svn)接受了它, 并且您的代码得以编译

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM