简体   繁体   English

自动类型归纳函数返回值

[英]auto type deduction of function return value

I am watching Scott Meyers's online video and have a different result with his presentation. 我正在观看Scott Meyers的在线视频,但他的演示结果却有所不同。 Here is the code: 这是代码:

auto LookupValue(int i) {
    static vector<int> values = {1, 2, 3, 4, 5};
    return values[i];
}

I got a waring for the return line with the information: 'Returning 'int&' from a function returning 'void''. 我对包含以下信息的返回行提出了警告:从返回“ void”的函数返回“ int&”。 Why the returning value deduce to void? 为什么返回值会导致无效?

Here is my testbed: 这是我的测试平台:

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

just a quick update: I test the return value as follows, and the compile DO set the return type with integral and should be returned by value, because of only the last line output with 1, the others are all 0. 只是快速更新:我按以下方式测试返回值,并且编译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;

Update conclusion: Finally, this is a bug of the parse component of CLion, and they promise to fix it in the next release. 更新结论:最后,这是CLion解析组件的错误,他们承诺在下一版本中对其进行修复。 Here is the bug report and feedback: 这是错误报告和反馈:

https://youtrack.jetbrains.com/issue/CPP-9906 https://youtrack.jetbrains.com/issue/CPP-9906

Why the returning value deduce to void? 为什么返回值会导致无效?

The compiler made a mistake. 编译器犯了一个错误。 It should deduce int, not void. 它应该推断出int而不是void。

... Or the example isn't complete. ...或示例不完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM