noexcept取决于成员函数的noexcept

Rob*_*ahy 8 c++ gcc 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();
        }
};
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会说不

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

同理:

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

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 {
Run Code Online (Sandbox Code Playgroud)

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

小智 13

this由于核心语言问题1207,可以使用的规则发生了变化,实际上是出于另一个原因,但是也影响了noexcept表达式.

之前(在C++ 03之后,但是当C++ 11仍在编写时),this不允许在函数体外使用.该noexcept表达式是不是身体的一部分,所以this不能使用.

之后,this可以在之后的任意位置使用的CV限定符序列,和noexcept之后的表达出现在你的问题的代码清楚地说明.

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

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