否除非依赖于成员函数的否

noexcept depend on noexcept of a member function

本文关键字:函数 成员 依赖于      更新时间:2023-10-16

考虑:

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) 中的错误?

由于

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

之前(在 C++03 之后,但当 C++11 仍在编写时),不允许在函数体之外使用thisnoexcept表达式不是身体的一部分,因此无法使用this

之后,this可以在 cv-qualifier-seq 之后的任何地方使用,之后会出现noexcept表达式,正如您问题中的代码清楚地说明的那样。

看起来这个问题的 GCC 实现不完整,只允许尾随函数返回类型的成员函数,但标准已经开放了更多。

我建议将此报告为错误(如果以前未报告过)。这已经在GCC bugzilla上报告为错误52869。

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