c – noexcept取决于成员函数的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();
        }
};

海湾合作委员会说不

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

解决方法

可以使用的规则因 core language issue 1207而改变,实际上是出于另一个原因,但其方式也影响了noexcept表达式.

之前(在C 03之后,但是当C 11仍然被写入时),不允许在函数体外使用它. noexcept表达式不是正文的一部分,因此无法使用.

之后,这可以在cv-qualifier-seq之后的任何地方使用,之后会出现noexcept表达式,因为问题中的代码清楚地说明了这一点.

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

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

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...