C ++ 14自动扣错误:函数返回一个数组 [英] C++14 auto deduction error: function returns an array

查看:223
本文介绍了C ++ 14自动扣错误:函数返回一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

社会!
我想申请新的C ++ 14的特性和意外遇到了错误,而我试图通过的为const char [] 的参数如下功能:

  decltype(自动)autofunc(const的汽车和放大器;一)
 {
     COUT<<的Hello World \\ n;
     COUT<< A<< ENDL; }
 汽车lambd = [](常量汽车和放大器;字){autofunc(性病::向前< decltype(字)>(字));};诠释的main()
{
 lambd(再见世界\\ n);
    返回0;
}

我不知道为什么,但compilier的消息是:函数试图返回一个数组(为什么会这么做?)。如果我改变函数的返回类型的无效的它会被编译。如果我通过另一种类型(而不是数组)的参数将被编译。使用数组的问题是什么?

错误消息

  ../../ untitled6 / main.cpp中:在实例化'<拉姆达(常量汽车:3及)GT; [自动:3 =的char [15]':
../../untitled6/main.cpp:74:25:从这里需要
../../untitled6/main.cpp:68:84:错误:调用没有匹配的函数autofunc(为const char [15])'
  汽车lambd = [](常量汽车和放大器;字){autofunc(性病::向前< decltype(字)>(字));};
                                                                                    ^
../../untitled6/main.cpp:68:84:注:候选人是:
../../untitled6/main.cpp:60:17:注意:模板和LT;一流汽车:2 - ; decltype(自动)autofunc(常量汽车:2及)
  decltype(自动)autofunc(const的汽车和放大器;一)
                 ^
../../untitled6/main.cpp:60:17:注:模板参数推导/替换失败:
../../untitled6/main.cpp:在模板&LT替代;一流汽车:2 - ; decltype(自动)autofunc(常量汽车:2及)自动:2 =的char [15]':
../../untitled6/main.cpp:68:84:从'&LT必需的;拉姆达(常量汽车:3及)GT; [自动:3 =的char [15]
../../untitled6/main.cpp:74:25:从这里需要
../../untitled6/main.cpp:60:17:错误:函数返回一个数组


解决方案

这是一个GCC的bug。

汽车不在函数参数的声明允许在C ++ 14。此语法是由概念精简版TS补充说。该 GCC 4.9发行说明


  

按第4.1.2节规定的G ++支持无约束的通用功能
  和N3889的§5.1.1:概念精简版规格。简单地说,汽车
  在任何函数的参数声明用作类型说明符
  声明符为了引入隐函数模板
  参数,类似于通用Lambda表达式。


N3889是概念的TS的早期工作草案。引用的部分,5.1.1,部分内容如下


  

一个泛型函数由具有函数声明表示汽车
  一个的概念名称的作为的类型说明符的一部分的其
  参数声明子句的。 [示例的:

 自动F(自动X); // 好
无效的排序(可排序和C); // OK(假设可排序的名字一个概念)


  
  

- 结束例如的]


  
  

在使用汽车概念名称的中
  参数声明子句的应作为PTED使用跨$ P $一个的类型参数的具有相同的约束和命名的概念。 [注意的:实现这一目标的确切机制是不确定的。 - 结束
  注
的] [的示例的:声明如下

通用功能

 自动F(自动X,常量经常和放大器; Y);


  
  

等同于下面的声明

 模板< typename的T1,T2定期GT&;
汽车F(T1 x,T2常量和放大器;);


  
  

- 结束例如的]


请注意,这个转变是不应该影响返回类型;它仍然汽车 - 这意味着它会被推断

由于这些规则, autofunc 的声明,应该是相当于

 模板<类T>
decltype(自动)autofunc(常量T&安培; A){/ * ... * /}

这将一直有效。相反,它得到了跨preTED为

 模板<类T>
ŧautofunc(常量T&安培; A){/ * ... * /}

导致一个错误,因为,所有的转发你正在做的, T 最后还是推导出数组类型。

这是具有讽刺意味的​​特别GCC以来4.9的自己的笔记(同上)说,


  //下面的两个函数声明是等价的
汽车增量(自动X){返回X ++; }
模板< typename的T>
汽车增量(T X){返回X ++; }


这是 demonstrably不是这样的GCC 4.9 的。

community! I'm trying to apply new C++14 features and unexpectedly encountered on error while I was attempting to pass the const char[] argument to the function given below:

 decltype(auto) autofunc( const auto& a)
 {
     cout<<"Hello World\n";
     cout<<a<<endl;

 }


 auto lambd = [](const auto& word){  autofunc(std::forward< decltype(word) > (word));};

int main()
{


 lambd("Goodbye World\n");


    return 0;
}

I don't know why,but the compilier's message is that function tries to return an array (why would it do this?). If I change function's return type to void It'll be compiled. If I pass an argument of another type(not array) It will be compiled. What is the problem with arrays?

Error message

../../untitled6/main.cpp: In instantiation of '<lambda(const auto:3&)> [with auto:3 = char [15]]':
../../untitled6/main.cpp:74:25:   required from here
../../untitled6/main.cpp:68:84: error: no matching function for call to 'autofunc(const char [15])'
  auto lambd = [](const auto& word){  autofunc(std::forward< decltype(word) > (word));};
                                                                                    ^
../../untitled6/main.cpp:68:84: note: candidate is:
../../untitled6/main.cpp:60:17: note: template<class auto:2> decltype(auto) autofunc(const auto:2&)
  decltype(auto) autofunc( const auto& a)
                 ^
../../untitled6/main.cpp:60:17: note:   template argument deduction/substitution failed:
../../untitled6/main.cpp: In substitution of 'template<class auto:2> decltype(auto) autofunc(const auto:2&) [with auto:2 = char [15]]':
../../untitled6/main.cpp:68:84:   required from '<lambda(const auto:3&)> [with auto:3 = char [15]]'
../../untitled6/main.cpp:74:25:   required from here
../../untitled6/main.cpp:60:17: error: function returning an array

解决方案

This is a GCC bug.

auto is not allowed in function parameter declarations in C++14. This syntax is added by the Concepts Lite TS. The GCC 4.9 release notes say that

G++ supports unconstrained generic functions as specified by §4.1.2 and §5.1.1 of N3889: Concepts Lite Specification. Briefly, auto may be used as a type-specifier in a parameter declaration of any function declarator in order to introduce an implicit function template parameter, akin to generic lambdas.

N3889 is an early working draft of the concepts TS. The cited section, 5.1.1, reads in part

A generic function is denoted by function declarator having auto or a concept-name as part of the type-specifier in its parameter-declaration-clause. [Example:

auto f(auto x); // Ok
void sort(Sortable& c); // Ok (assuming Sortable names a concept)

end example ]

The use of auto or a concept-name in the parameter-declaration-clause shall be interpreted as the use of a type-parameter having the same constraints and the named concept. [ Note: The exact mechanism for achieving this is unspecified. — end note ] [Example: The generic function declared below

auto f(auto x, const Regular& y);

Is equivalent to the following declaration

template<typename T1, Regular T2>
auto f(T1 x, const T2&);

end example ]

Note that this transformation is not supposed to affect the return type; it remains auto - meaning that it will be deduced.

Given these rules, autofunc's declaration should have been equivalent to

template<class T>
decltype(auto) autofunc( const T& a) { /* ... */ }

which would have been valid. Instead, it got interpreted as

template<class T>
T autofunc( const T& a) { /* ... */ }

causing an error since, with all the forwarding you are doing, T ends up getting deduced as an array type.

This is particular ironic since GCC 4.9's own notes (cited above) say that

// the following two function declarations are equivalent
auto incr(auto x) { return x++; }
template <typename T>
auto incr(T x) { return x++; }

which is demonstrably not the case on GCC 4.9.

这篇关于C ++ 14自动扣错误:函数返回一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆