社區! 我想申請新的C++ 14的功能和錯誤時遇到意外,而我正在試圖通過爲const char []函數參數下面給出:C++ 14自動扣除錯誤:函數返回數組
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;
}
我不知道爲什麼,但編譯器的消息是該函數試圖返回一個數組(爲什麼會這樣做?)。如果我將函數的返回類型更改爲void它將被編譯。如果我傳遞另一種類型的參數(不是數組),它將被編譯。數組有什麼問題?
錯誤消息
../../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
什麼是'functor'?什麼是'f'?什麼是確切的問題/錯誤信息?你使用什麼版本的編譯器? – ildjarn 2015-02-07 09:29:38
@ ildjarn,哦,對不起。 * functor *是不必要的對象,我忘了消除它。我使用的編譯器是G ++ - 4.9。錯誤消息,我添加到問題。 – GamovCoder 2015-02-07 09:33:45
您不允許在函數參數的聲明中使用'auto'。 'decltype(auto)autofunc(const auto&a){...}'是無效的C++ 14。 – 2015-02-07 09:41:53