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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With