19

In the following program, I have added an explicit return statement in func(), but the compiler gives me the 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
 }

This is the code:

#include <iostream>
using namespace std;

constexpr int func (int x);

constexpr int func (int x) 
{
    if (x<0)                
        x = -x;
    return x; // An explicit return statement 
}

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

I have compiled program in a g++ compiler using the following command.

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

I have added return statement in function, then Why I got above error?

msc
  • 33,420
  • 29
  • 119
  • 214
  • 1
    You also don't need that declaration. A definition is also a declaration, and in this case, it harms readability IMO. – Rakete1111 Jul 14 '17 at 07:37
  • 3
    @tadman What? Of course `constexpr` function return values can depend on their arguments. What would be the point otherwise? – Angew is no longer proud of SO Jul 14 '17 at 07:37
  • 2
    And the more interesting use would be: const int a=func(10); so computing a const-expression based on constexpr. – Hans Olsson Jul 14 '17 at 07:38
  • @Angew How much code can you cram into a `constexpr` before the compiler refuses to compile-time evaluate it? – tadman Jul 14 '17 at 07:40
  • 3
    @tadman In C++11, a single `return` statement. Since C++14, the sky (and [dcl.constexpr] and Annex B :-) ) is the limit. Similar to how template metaprogramming works. – Angew is no longer proud of SO Jul 14 '17 at 09:19
  • @Angew Right you are. I was used to the C++11 behaviour where you couldn't really do much other than a `return` and the function was limited to being a sort of primitive macro. – tadman Jul 14 '17 at 16:17

2 Answers2

26

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Thomas
  • 174,939
  • 50
  • 355
  • 478
19

C++11's constexpr functions are more restrictive than that.

From cppreference:

the function body must be either deleted or defaulted or contain only the following:

  • null statements (plain semicolons)
  • static_assert declarations
  • typedef declarations and alias declarations that do not define classes or enumerations
  • using declarations
  • using directives
  • exactly one return statement.

So you can say this instead:

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

static_assert(func(42) == 42, "");
static_assert(func(-42) == 42, "");

int main() {}

Note that this restriction was lifted in C++14.

mpark
  • 7,574
  • 2
  • 16
  • 18