【发布时间】:2017-05-29 10:00:28
【问题描述】:
我正在观看 Scott Meyers 的在线视频,但他的演示结果有所不同。代码如下:
auto LookupValue(int i) {
static vector<int> values = {1, 2, 3, 4, 5};
return values[i];
}
我收到一条关于返回行的警告,其中包含以下信息:'Returning 'int&' from a function return 'void''。 为什么返回值会推导出void?
这是我的测试平台:
fetag@MacgicBox ~$ clang --version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
只是一个快速更新: 我测试返回值如下,编译DO设置返回类型为整数,应该是按值返回,因为只有最后一行输出1,其他都是0。
cout << is_lvalue_reference<decltype(LookupValue(2))>::value << endl;
cout << is_rvalue_reference<decltype(LookupValue(2))>::value << endl;
cout << is_reference<decltype(LookupValue(2))>::value << endl;
cout << is_pointer<decltype(LookupValue(2))>::value << endl;
cout << is_void<decltype(LookupValue(2))>::value << endl;
cout << is_integral<decltype(LookupValue(2))>::value << endl;
更新结论: 最后,这是 CLion 解析组件的一个错误,他们承诺在下一个版本中修复它。这是错误报告和反馈:
【问题讨论】:
-
你用 C++11 还是 C++14 编译?
-
C++ 14. C++ 11 不支持函数的返回类型为自动。
-
这就是我问的原因,这可能只是一个奇怪的错误消息,因为在 C++11 中你必须使用带有 auto 的后缀返回类型。
-
与
clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp(clang 3.8) 和g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp(gcc 7.1) 我没有警告/错误。 -
尝试用 GCC 或其他编译器编译它怎么样?
标签: c++ c++14 auto type-deduction