4

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)

Community
  • 1
  • 1
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Perhaps it just wasn't changed by 4.6.3. Do note that GCC wasn't C++11-complete until recently. 4.8 gives an appropriate error. – chris Feb 24 '14 at 01:35
  • That's quite an old compiler. A more recent version rejects this: http://ideone.com/0lzc0e – Mike Seymour Feb 24 '14 at 01:36
  • It looks like 4.7 accepts it but 4.8.2 does not, when using an older versions it can be helpful to use an [online compiler](http://stackoverflow.com/questions/3916000/online-c-compiler-and-evaluator). – Shafik Yaghmour Feb 24 '14 at 01:38
  • @chris `4.8.1` is the first gcc release to offer full C++11 support, not `4.8` – user2485710 Feb 24 '14 at 01:38
  • @user2485710, I was speaking more generally as in whatever version of 4.8 it is that Coliru uses (I think it's 4.8.1, but I'm not sure). As far as I'm aware, 4.8.0 is more suited for a specific version than 4.8, unless 4.8 is something different altogether. – chris Feb 24 '14 at 01:39
  • @texasbruce your implementation is incomplete, your compiler is unreliable as far as C++11 is concerned, this is code that doesn't compile on both `clang` and `gcc` using complete `C++11` implementations of the standard, for `gcc` you should use a version >= `4.8.1` – user2485710 Feb 24 '14 at 01:40
  • @chris `4.8` and `4.8.1` are 2 different versions, Coliru is probably wrong in labeling `4.8.1` as `4.8`. the `gcc` documentation clearly states that the first implementation of the `C++11` standard is the `4.8.1` – user2485710 Feb 24 '14 at 01:44
  • @user2485710 Is 4.8.1 complete? IIRC, their regex library is complete crap. They needed to have stuck a `static_assert` in the header to force error-on-compiler, because stuff like `"\\w+"` caused runtime errors and `.*` doesn't match any of my strings. That's WORSE than a compiler error, IMO. – KitsuneYMG Feb 24 '14 at 01:59
  • @chris It is `4.8.2` on Coliru you can just do a `g++ --version`. – Shafik Yaghmour Feb 24 '14 at 02:01
  • @KitsuneYMG, That's libstdc++. The compiler has complete support for the language. – chris Feb 24 '14 at 02:02
  • @ShafikYaghmour, I suppose I could. I normally just use Clang, but thanks for the heads up. – chris Feb 24 '14 at 02:03
  • @chris Gcc ships libstdc++ right? Then it's a flaw in the gcc package. That simple. If you cannot run a valid c++11 program under the package they call "complete" because it's outright missing stuff, then it's not complete. – KitsuneYMG Feb 24 '14 at 02:19
  • Tell that to the people advertising the compiler as complete. Anyway, Clang also uses libstdc++ by default, even though libc++ has full C++11 and C++14 support now. – chris Feb 24 '14 at 02:24

1 Answers1

8

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740