从自动输入参数推导返回类型
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)
接受了……
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- 在 c++ 中将函数返回类型指定为模板参数
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 基于类型作为参数的字符串表示形式具有不同返回类型的函数
- 具有"templated"返回类型和参数的函数指针
- 如何创建两个具有相同名称和不同返回类型并基于布尔参数运行的函数
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- 如何定义依赖于参数包转换的函数的返回类型
- 无法推断返回类型的模板参数
- C++模板;作为模板参数传递的函数的自动推导返回类型
- 如何在模板参数中分离函数类型返回类型和参数
- 为什么函数返回类型中不允许参数推导?
- 模板化函数以从输入参数推断返回类型 stl-container
- 将 2D 数组作为函数参数,并返回 2D 数组作为函数的返回类型
- C++ 如何将参数绑定到具有相同返回类型的函数,并将它们全部存储在一个容器中
- 为什么编译器不能从返回类型中推断出模板参数?
- 具有不同模板参数的函数返回类型
- C++:返回类型为 T 的模板函数不带 T 类型参数,将无法编译