gpt4 book ai didi

c++ - 为什么 GCC 允许通过右值引用捕获?

转载 作者:太空狗 更新时间:2023-10-29 20:13:22 26 4
gpt4 key购买 nike

标准声明通过右值引用捕获应该是非法的: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.1first 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com