析构函数和noexcept [英] Destructors and noexcept

查看:553
本文介绍了析构函数和noexcept的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑与析构函数和 noexcept 。我的理解是,在C ++ 11中,任何析构函数,包括用户定义,隐式的 noexcept(true),即使我们 throw 从它。并且必须明确指定 noexcept(false)是他们希望它由于某种原因。

I am a little bit confused with destructors and noexcept. My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true), even if we throw from it. And one has to specify explicitly noexcept(false) is they want it to be that way for some reason.

我看到的恰恰相反 - 与GCC 4.7.2,用户定义析构函数,无论类和析构函数是多么原始,隐式 noexcept(false)。我在这里缺少什么?是否有一些隐藏的用户定义的析构函数?

What I'm seeing is that quite the opposite - with GCC 4.7.2, the user-defined destructor, no matter how primitive the class and destructor are, is implicitly noexcept(false). What am I missing here? Is there some hidden gotcha with user-defined destructors?

推荐答案

这是一个已知的错误(信用OP的找到错误报告),它似乎已经修复在GCC 4.8.0 。例如,下面的静态断言将在GCC 4.7.2上启动,但不在GCC 4.8.0上启动:

This is a known bug (credits to the OP for finding the bug report), and it seems it has been fixed in GCC 4.8.0. For instance, the static assertion below will fire on GCC 4.7.2, but not on GCC 4.8.0:

struct X
{
    ~X() { };
};

int main()
{
    X x;

    // This will not fire even in GCC 4.7.2 if the destructor is
    // explicitly marked as noexcept(true)
    static_assert(noexcept(x.~X()), "Ouch!");
}

这篇关于析构函数和noexcept的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆