noexcept depend on noexcept of a member function

与世无争的帅哥 提交于 2019-11-30 04:02:27

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/18878029/noexcept-depend-on-noexcept-of-a-member-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!