此代碼不能編譯(英特爾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. 「操作員聲明」<<無功能
- 3. g ++「operator <<」聲明爲非函數「
- 4. 爲什麼`type(myField)`返回`<type'instance'>`而不是'<type'Field'>`?
- 5. 爲什麼operator <<不明確?
- 6. 錯誤:將'operator <<'聲明爲非函數|
- 7. const QList <int> warnings = QList <int>()<< 0;用gcc 4.7.2
- 8. 爲什麼我可以在類型聲明上使用「<:」,但不能使用「<:<」
- 9. 爲什麼boost :: is_same <int const&,boost :: add_const <int &> :: value等於false?
- 10. 汽車中心<div><div></div></div>
- 11. 返回向量<Foo>或shared_ptr <vector<Foo>>?
- 12. 常量<type>和富()與<type> FOO()
- 13. 爲什麼不使用<< EOF代替cat << EOF?
- 14. 爲什麼std :: pair <int, int>可以從const std :: pair <int, float>&?
- 15. 錯誤:操作符<<在操作符<<((const Args *)a) - > Args :: getArgL()
- 16. 爲什麼我的重載<<不返回任何數據?
- 17. <%$,<%@,<%=,<%#...這是怎麼回事?
- 18. 爲什麼不能將向量<bool>中的元素地址推回到向量<bool*>?
- 19. << <<超載
- 20. 呼應HTML回聲<<< HTML
- 21. THREE.Camera.prototype.lookAt</ <() -</ <是什麼意思?
- 22. 如何將shared_ptr <Foo<Derived>>轉換爲shared_ptr <Foo<Base>>?
- 23. Typescript Pick <> type failing:類型'「foo」'的參數不能分配給類型爲'Pick <Bar, "foo">'的參數'
- 24. 什麼意思的符號在java中「<<<」「<< =」「<<」
- 25. 容器<ImplementerOfIInterface>不是容器<IInterface>。爲什麼不?
- 26. 錯誤:</p> <p>錯誤:在const的返回類型
- 27. cmake的-D <var>:<type> = <value>:有什麼可以<type>呢?
- 28. 爲什麼在操作符<exists時定義lt lt?
- 29. operator <<:std :: cout << i <<(i << 1);
- 30. 爲什麼「const Eigen :: Matrix <>&」和「const Ref <Eigen :: Matrix <>>顯然不兼容?
試試C++ 14。這允許這一點。 –
「爲什麼功能X不存在」的一般答案是:「沒有人試圖實現它」,通常使用隱含* * – Caleth
或使用lambda,它允許它:'auto foo = [](){ const bool retVal = false;返回retVal; };' –