本文介绍了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补充说。该说

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

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

which is demonstrably not the case on GCC 4.9.

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

07-30 02:07