此代码不能编译(英特尔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; };' –