此代碼不能編譯(英特爾ICC 15.0.3),即返回在這種情況下鍵入的是bool
。爲什麼auto不能用作函數聲明的返回類型?這個功能很好地支持DRY principle。爲什麼汽車不能用作與錯誤訊息<code>explicit type is missing ("int" assumed)</code></p> <pre><code>auto foo(){ const bool retVal = false; return retVal; } </code></pre> <p>它可以很容易地看到返回類型爲函數聲明
0
A
回答
4
1
您可以可以使用尾隨返回類型在C++ 11,如果你想這樣做的現代的方式:
auto foo() -> bool{
const bool retVal = false;
return retVal;
}
但我不得不承認,我更喜歡使用bool foo(){..}
作爲pre-C++ 14。
相關問題
- 1. 爲什麼「testScores [」dave「]」的類型是「可選<Array<Int>> .Type」而不是「<Array<Int>> .Type」
- 2. 爲什麼boost :: is_same <int const&,boost :: add_const <int &> :: value等於false?
- 3. 汽車中心<div><div></div></div>
- 4. 返回向量<Foo>或shared_ptr <vector<Foo>>?
- 5. 爲什麼不使用<< EOF代替cat << EOF?
- 6. 常量<type>和富()與<type> FOO()
- 7. <%$,<%@,<%=,<%#...這是怎麼回事?
- 8. 爲什麼不能將向量<bool>中的元素地址推回到向量<bool*>?
- 9. << <<超載
- 10. 什麼意思的符號在java中「<<<」「<< =」「<<」
- 11. Typescript Pick <> type failing:類型'「foo」'的參數不能分配給類型爲'Pick <Bar, "foo">'的參數'
- 12. 容器<ImplementerOfIInterface>不是容器<IInterface>。爲什麼不?
- 13. 錯誤:</p> <p>錯誤:在const的返回類型
- 14. operator <<:std :: cout << i <<(i << 1);
- 15. cmake的-D <var>:<type> = <value>:有什麼可以<type>呢?
- 16. cout << 2 [「abc」] << endl;爲什麼它工作?它的語法是什麼?
- 17. 操作<<爲QString的
- 18. 使用operator <<與const對象
- 19. Collections.sort()聲明:爲什麼<? super T>而不是<T>
- 20. <<做什麼?
- 21. 的Rails搞亂我的代碼以「<<<<<<< HEAD」
- 22. -2 <1 = false。爲什麼?
- 23. new ArrayList <Set<?>>() - 爲什麼它可以?
- 24. vector <pair <int,unordered_set <int> >>爲對
- 25. 如何將表達式<Func <T, bool>>轉換爲表達式<Func <Type, bool>>?
- 26. 不能從'cli :: array <Type> ^'轉換爲'cli :: array <Type> ^'
- 27. 爲什麼我不能寫Nullable <Nullable <int>>?
- 28. Php <<<是什麼意思?
- 29. 什麼是PHP中的「<<< SQL」?
- 30. 在bash中,<<<用於什麼?
試試C++ 14。這允許這一點。 –
「爲什麼功能X不存在」的一般答案是:「沒有人試圖實現它」,通常使用隱含* * – Caleth
或使用lambda,它允許它:'auto foo = [](){ const bool retVal = false;返回retVal; };' –