Why does GCC catch the rvalue link?

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

#include <string>
#include <iostream>

using namespace std;

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

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

I use gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)

+3
source share
1 answer

gcc 4.8.1was the first version of C ++ 11 version gcc . Therefore, it is not surprising to see incomplete support for C ++ 11 in the version before. 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) {
                    ^

Support for C ++ 0x / C ++ 11 in GCC , details on what basic functions were supported in this version.

+7
source

All Articles