Noexcept depends on noexcept member function

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 does not respond:

 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 the intended behavior according to the standard or a bug in GCC (4.8.0)?

+6
source share
1 answer

The rules for using this can be changed as a result of a problem with the 1207 base language , for another reason, but in some way that also affects noexcept expressions.

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

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

It seems that the GCC implementation of this problem is incomplete and only allows member functions to return return function types, but the standard has discovered more than that. I recommend reporting this as an error (if it has not previously been reported). This is reported in GCC bugzilla as error 52869 .

Whatever the cost, clang accepts code in C ++ 11 mode.

+12
source

Source: https://habr.com/ru/post/954088/


All Articles