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

标签 c++ exception gcc c++11 g++

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

相关文章:

macos - native C/C++ 程序中的独立 Rmath.h [2]

c++ - 链接到多线程调试 DLL 库时出现链接错误

c++ - 使用 foreach 循环 C++ 更新 std::list 中的每个值

c++ - 成功包含文件,现在处理语义问题

c++ - C++ 中的编译问题 (CodeBlocks 13.12)

c - 错误: unused variable (struct)

c++ - 显式实例化失败的原因是什么?

haskell - 如何使用没有 SomeException 作为参数类型的处理程序

C# AutoMapperMappingException KeyNotFoundException 异常

c++ - 处理异常时内存泄漏