加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么GCC允许通过右值参考捕获?

发布时间:2020-12-16 09:50:43 所属栏目:百科 来源:网络整理
导读:标准规定,通过右值引用捕获应该是非法的: Catch By Rvalue Reference,但我有以下代码: #include string#include iostreamusing namespace std;int main(){ try { throw string("test"); } catch (string s) { cout s endl; } return 0;} 使用-Wall选项成功
标准规定,通过右值引用捕获应该是非法的: 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是gcc的 first C++11 feature complete版本.因此,在此之前的版本中看到不完整的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详细说明了哪个版本支持哪些主要功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读