Overriding a [[noreturn]] virtual function

后端 未结 3 1867
小蘑菇
小蘑菇 2021-01-07 19:26

The [[noreturn]] attribute can be applied to functions that are not meant to return. For example:

[[noreturn]] void will_throw() { throw std::r         


        
3条回答
  •  不思量自难忘°
    2021-01-07 20:10

    In practice, neither g++, clang nor MSVC consider the [[noreturn]] attribute as inherited

    #include 
    
    struct B {
    public:
      [[noreturn]] virtual void f() { std::cout << "B\n"; throw 0; }
    };
    
    struct D : public B {
      void f() override { std::cout << "D\n"; }
    };
    
    int main() 
    {
        try { B{}.f(); } catch(...) {}
        D{}.f();
    
        B* d = new D{};
        d->f();
    }
    

    which prints out "B", "D" and "D" for all three compilers.

提交回复
热议问题