不确定是否为 noexcept 取决于成员函数是否为 noexcept。

8

请考虑以下内容:

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

1个回答

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

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

之后,在cv-qualifier-seq之后的任何地方都可以使用thisnoexcept表达式紧随其后,正如您问题中的代码所示。

看起来,GCC对此问题的实现不完整,并且仅允许在尾随函数返回类型中使用成员函数,但标准已经开放了更多内容。这已经在GCC bugzilla上报告为bug 52869

无论价值如何,clang在C++11模式下接受此代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接