Why GCC allows catch by rvalue reference?

折月煮酒 提交于 2019-12-12 13:51:10

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/21977340/why-gcc-allows-catch-by-rvalue-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!