Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why GCC allows catch by rvalue reference?

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;
}

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

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

like image 843
SwiftMango Avatar asked Feb 13 '23 17:02

SwiftMango


1 Answers

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) {
                    ^

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

like image 147
Shafik Yaghmour Avatar answered Feb 16 '23 08:02

Shafik Yaghmour