为什么 GCC 允许通过右值引用进行捕获?

2023-12-31

该标准规定通过右值引用捕获应该是非法的:按右值引用捕获 https://stackoverflow.com/a/3860676/1270003,但我有以下代码:

#include <string>
#include <iostream>

using namespace std;

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

它成功编译,没有任何警告-Wall选项。这是怎么发生的?

我在用gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)


gcc 4.8.1第一个 C++11 功能完成 http://isocpp.org/blog/2013/05/gcc-4.8.1-released-c11-feature-complete的版本gcc。所以看到不完整也就不足为奇了C++11在此之前的版本中支持。我们可以看到4.8.2 拒绝此 http://coliru.stacked-crooked.com/a/36b3d6a7fab40d73出现以下错误:

error: cannot declare catch parameter to be of rvalue reference type 'std::string&& {aka std::basic_string<char>&&}'
 } catch (string && s) {
                    ^

The GCC 中的 C++0x/C++11 支持 http://gcc.gnu.org/projects/cxx0x.html详细说明哪个版本支持哪些主要功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 GCC 允许通过右值引用进行捕获? 的相关文章

随机推荐