vendredi 14 juillet 2017

C++ : body of constexpr function ‘constexpr int func(int)’ not a return-statement

In the following program, I have added explicitly return statement in func(), but compiler gives following error.

m.cpp: In function ‘constexpr int func(int)’:
m.cpp:11:1: error: body of constexpr function ‘constexpr int func(int)’ not a return-statement
 }

My code is here:

#include <iostream>
using namespace std;

constexpr int func (int x);

constexpr int func (int x) 
{
    if (x<0)                
        x = -x;
    return x; // Explicitly added 
}

int main() 
{
    int ret = func(10);
    cout<<ret<<endl;
    return 0;
}

I have compiled my program in GCC compiler using following command.

g++ -std=c++11 m.cpp

Aucun commentaire:

Enregistrer un commentaire