为什么GCC允许catch通过右值引用?

为什么GCC允许catch通过右值引用?

问题描述:

标准规定,通过右值引用捕获应该是非法的: Catch By Rvalue参考,但我有以下代码:

The standard states that catching by rvalue reference should be illegal: Catch By Rvalue Reference, but I have the follwing code:

#include <string>
#include <iostream>

using namespace std;

int main(){
    try {
        throw string("test");
    } catch (string && s) {
        cout << s << endl;
    }
    return 0;
}

它成功编译没有任何警告与 -Wall 选项。

It successfully compiles without any warning with -Wall option. How does this happen?

我使用 gcc版本4.6.3 20120306(Red Hat 4.6.3-2)(GCC)

gcc 4.8.1 href =http://isocpp.org/blog/2013/05/gcc-4.8.1-released-c11-feature-complete>第一个C ++ 11功能完成版本 gcc 。因此,在之前的版本中看到不完整的 C ++ 11 支持并不奇怪。我们可以看到 4.8.2拒绝此,并显示以下错误:

gcc 4.8.1 was the first C++11 feature complete version of gcc. So it is not surprising to see incomplete C++11 support in a version before that. We can see that 4.8.2 rejects this with the following error:

error: cannot declare catch parameter to be of rvalue reference type 'std::string&& {aka std::basic_string<char>&&}'
 } catch (string && s) {
                    ^

GCC中的 C ++ 0x / C ++ 11支持详细说明哪个版本支持哪些主要功能。

The C++0x/C++11 Support in GCC details which major features were supported in which version.