Home > Blockchain >  Why is that noexcept specifier doesn't work in complete class context but default argument and
Why is that noexcept specifier doesn't work in complete class context but default argument and

Time:10-09

I'm learning C using the books listed here. In particular, I read about complete-class context and came to know that it includes function-body, default argument, noexcept-specifier etc. Now, to further clear my understanding of the topic, I wrote the following program where #1 and #2 works but #3 fails. I don't know why #3 fails because I read that all the three(function body, default argument and noexcept specifier) are included in the complete-class context.

struct A {
   constexpr static bool func() 
   { 
       return true; 
   }
   //--------------vvvvvv------->works as expected   #1
   void f(bool V1 = func())
   { 
      bool V2 = func(); //works as expected          #2
   }
   //-----------------vvvvvv---->DOESN'T WORK?       #3
   void g()  noexcept(func()) 
   {
      ;
   }

};

A complete-class context of a class is a

  • function body
  • default argument
  • noexcept specifier

As you can see the third point says "noexcept specifier" so i expected #3 to works as well but it doesn't.

So my question is why #3 doesn't work unlike #1 and #2? Demo

GCC gives the error with #3:

 error: 'static constexpr bool A::func()' called in a constant expression before its definition is complete
   20 |    void g()  noexcept(func())

Clang gives:

error: noexcept specifier argument is not a constant expression
   void g()  noexcept(func()) 
                      ^~~~~~
<source>:20:23: note: undefined function 'func' cannot be used in a constant expression
<source>:10:26: note: declared here
   constexpr static bool func() 

MSVC gives:

 error C2131: expression did not evaluate to a constant
<source>(20): note: failure was caused by call of undefined function or one not declared 'constexpr'
<source>(20): note: see usage of 'A::func'

CodePudding user response:

The program is well-formed and all the three compilers are wrong in rejecting the code as noexcept-specifier is included in complete-class context as quoted in the question.

Here are the respective bug reports:

GCC rejects use of static constexpr member function in noexcept complete-class context

Clang rejects use of static constexpr member function in noexcept complete-class context

MSVC rejects use of static constexpr member function in noexcept complete-class context

CodePudding user response:

Because A::func() is incomplete in this context, try to place g outside class as not A member

  • Related