【问题标题】:Why GCC allows catch by rvalue reference?为什么 GCC 允许通过右值引用捕获?
【发布时间】:2014-02-24 01:34:38
【问题描述】:

标准规定通过右值引用捕获应该是非法的:Catch By Rvalue Reference,但我有以下代码:

#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)

【问题讨论】:

  • 也许它只是没有被 4.6.3 改变。请注意,GCC 直到最近才完成 C++11。 4.8 给出了适当的错误。
  • 那是一个相当古老的编译器。更新的版本拒绝了这个:ideone.com/0lzc0e
  • 看起来 4.7 接受它,但 4.8.2 不接受,当使用旧版本时,使用online compiler 会很有帮助。
  • @chris 4.8.1 是第一个提供完整 C++11 支持的 gcc 版本,而不是 4.8
  • @user2485710,我说得更笼统,因为 Coliru 使用的是 4.8 的任何版本(我认为是 4.8.1,但我不确定)。据我所知,4.8.0 比 4.8 更适合特定版本,除非 4.8 完全不同。

标签: c++ exception gcc c++11 g++


【解决方案1】:

gcc 4.8.1first C++11 feature complete 版本的 gcc。因此,在此之前的版本中看到不完整的 C++11 支持也就不足为奇了。我们可以看到4.8.2 rejects this 出现以下错误:

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

C++0x/C++11 Support in GCC 详细说明了哪个版本支持哪些主要功能。

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2013-08-22
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多