标准声明通过右值引用捕获应该是非法的:Catch By Rvalue Reference ,但我有以下代码:
#include <string>
#include <iostream>
using namespace std;
int main(){
try {
throw string("test");
} catch (string && s) {
cout << s << endl;
}
return 0;
}
使用 -Wall
选项,它可以在没有任何警告的情况下成功编译。这是怎么发生的?
我正在使用 gcc 版本 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)
最佳答案
gcc 4.8.1
是 first C++11 feature complete gcc
版本。因此,在之前的版本中看到不完整的 C++11 支持也就不足为奇了。我们可以看到 4.8.2 rejects this出现以下错误:
error: cannot declare catch parameter to be of rvalue reference type 'std::string&& {aka std::basic_string<char>&&}'
} catch (string && s) {
^
C++0x/C++11 Support in GCC详细说明哪个版本支持哪些主要功能。
关于c++ - 为什么 GCC 允许通过右值引用捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977340/