我正在看Scott Meyer的在线视频,他的演示文稿有不同的结果。代码如下:
auto LookupValue(int i) {
static vector<int> values = {1, 2, 3, 4, 5};
return values[i];
}我得到了一个关于返回行的警告信息:‘return '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快速更新一下:我测试返回值如下所示,编译器确实用integral设置了返回值类型,并且应该按值返回,因为只有最后一行输出为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的解析组件的一个错误,他们承诺在下一个版本中修复它。以下是错误报告和反馈:
发布于 2017-05-29 18:10:22
为什么返回值会被推断为空?
编译器出了一个错误。它应该推导出int,而不是void。
..。或者示例不完整。
https://stackoverflow.com/questions/44239668
复制相似问题