加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – noexcept取决于成员函数的noexcept

发布时间:2020-12-16 05:52:47 所属栏目:百科 来源:网络整理
导读:考虑: class test { private: int n; int impl () const noexcept { return n; } public: test () = delete; test (int n) noexcept : n(n) { } int get () const noexcept(noexcept(impl())) { return impl(); }}; 海湾合作委员会说不 test.cpp:27:43: err
考虑:
class test {
    private:
        int n;

        int impl () const noexcept {
            return n;
        }

    public:
        test () = delete;
        test (int n) noexcept : n(n) {    }

        int get () const noexcept(noexcept(impl())) {
            return impl();
        }
};

海湾合作委员会说不

test.cpp:27:43: error: cannot call member function 'int test::impl() const' with
out object
   int get () const noexcept(noexcept(impl())) {

同理:

test.cpp:27:38: error: invalid use of 'this' at top level
   int get () const noexcept(noexcept(this->impl())) {

test.cpp:31:58: error: invalid use of incomplete type 'class test'
   int get () const noexcept(noexcept(std::declval<test>().impl())) {
                                                          ^
test.cpp:8:7: error: forward declaration of 'class test'
 class test {

这是符合标准的预期行为,还是GCC(4.8.0)中的错误?

解决方法

可以使用的规则因 core language issue 1207而改变,实际上是出于另一个原因,但其方式也影响了noexcept表达式.

之前(在C 03之后,但是当C 11仍然被写入时),不允许在函数体外使用它. noexcept表达式不是正文的一部分,因此无法使用.

之后,这可以在cv-qualifier-seq之后的任何地方使用,之后会出现noexcept表达式,因为问题中的代码清楚地说明了这一点.

看起来这个问题的GCC实现是不完整的,并且只允许成员函数在尾随函数返回类型,但标准已经打开了更多.我建议将此报告为错误(如果之前未报告过).这已经在GCC bugzilla报道为bug 52869.

无论它的价值如何,clang都接受C 11模式的代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读