The body of constexpr function not a return-statement

前端 未结 2 1807
栀梦
栀梦 2021-02-01 08:56

In the following program, I have added an explicit return statement in func(), but the compiler gives me the following error:

m.cpp: In         


        
2条回答
  •  死守一世寂寞
    2021-02-01 09:02

    Prior to C++14, the body of a constexpr function must consist solely of a return statement: it cannot have any other statements inside it. This works in C++11 :

    constexpr int func (int x) 
    {
      return x < 0 ? -x : x;
    }
    

    In C++14 and up, what you wrote is legal, as are most other statements.

    Source.

提交回复
热议问题