noexcept取决于成员函数的noexcept [英] noexcept depend on noexcept of a member function

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

问题描述

请考虑:

  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
}
};

GCC说不:

  test.cpp:27:43:error:无法调用成员函数'int test :: impl()const'with 
out object
int get()const noexcept (impl())){

同样:

  test.cpp:27:38:error:无效的使用'this'在顶层
int get()const noexcept(noexcept(this-& ))){

  test.cpp:31:58:error:无效使用不完全类型'类测试'
int get()const noexcept(noexcept(std :: declval< test>
类测试{



这是根据标准的预期行为还是GCC(4.8.0)中的错误?

open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207\">核心语言问题1207 ,实际上是因为另一个原因,但影响 noexcept 表达式。



之前(在C ++ 03之后,但是当C ++ 11仍在写时), / code>不允许在函数体外使用。 noexcept 表达式不是正文的一部分,因此不能使用 this



之后,可以在 cv-qualifier-seq 之后的任何位置使用



看起来像这个问题的GCC实现是不完整的,而且没有noexcept 只允许成员函数在尾部函数返回类型,但是标准已经打开了更多。 我建议将此报告为一个错误(如果以前没有报告)。这已经在GCC bugzilla上报告为 bug 52869



无论如何,clang接受C ++ 11模式中的代码。


Consider:

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();
        }
};

GCC says no:

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

Similarly:

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

and

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 {

Is this intended behaviour as per the standard, or a bug in GCC (4.8.0)?

解决方案

The rules for where this can be used changed as a result of core language issue 1207, actually for another reason but in a way that also affects noexcept expressions.

Before (after C++03, but when C++11 was still being written), this was not allowed to be used outside a function body. The noexcept expression is not part of the body, so this could not be used.

After, this can be used anywhere after the cv-qualifier-seq, and noexcept expressions appear after that, as the code in your question clearly illustrates.

It looks like the GCC implementation of this issue is incomplete, and only allows member functions in trailing function return types, but the standard has opened up more than that. I recommend reporting this as a bug (if it has not previously been reported). This has already been reported on GCC bugzilla as bug 52869.

For whatever it's worth, clang accepts the code in C++11 mode.

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

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