4

In the following example:

//Case 1
constexpr int doSomethingMore(int x)
{
    return x + 1;
}

//Case 2
constexpr int doSomething(int x)
{
    return ++x;
}


int main()
{}

Output:

prog.cpp: In function ‘constexpr int doSomething(int)’:
prog.cpp:12:1: error: expression ‘++ x’ is not a constant-expression

Why is Case 1 allowed but Case 2 is not allowed?

9
  • 5
    Because "expression ‘++ x’ is not a constant-expression"? I'm not sure what else you want to hear. Commented Jun 26, 2013 at 10:11
  • 1
    Currently it is waaaay limited what you can do in constexpr. But wait just a little, in C++14 most limitations will be gone (and you might even get them sooner in Clang and gcc). ++x is no-go for C++11. Commented Jun 26, 2013 at 10:16
  • 7
    Because is x+1 calculated and returned on the stack while ++x increments x and then returns x on the stack - thus x is altered and is not a constexpr. Commented Jun 26, 2013 at 10:16
  • 4
    @IngeHenriksen: The question still has merit IMO: the entire expression is free of side effects and can be calculated at compile time - it is a constexpr in spirit. Commented Jun 26, 2013 at 11:18
  • 1
    @peterchen, yes, and that's why it's allowed in C++14, but in C++11 constexpr functions must conform to a more functional programming style, i.e. avoiding state and mutable data, even local state. Commented Jun 26, 2013 at 11:56

3 Answers 3

7

Case 1 doesn't modify anything, case 2 modifies a variable. Seems pretty obvious to me!

Modifying a variable requires it to not be constant, you need to have mutable state and the expression ++x modifies that state. Since a constexpr function can be evaluated at compile-time there isn't really any "variable" there to modify, because no code is executing, because we're not at run-time yet.

As others have said, C++14 allows constexpr functions to modify their local variables, allowing more interesting things like for loops. There still isn't really a "variable" there, so the compiler is required to act as a simplified interpreter at compile-time and allow limited forms of local state to be manipulated at compile-time. That's quite a significant change from the far more limited C++11 rules.

Sign up to request clarification or add additional context in comments.

1 Comment

and even more interesting things like compile-time infinite loops :)
2

Your argument is indeed valid that by spirit/technicality of constexpr both x+1 and ++x are same. Where x is a local variable to the function. Hence there should be no error in any case.

This issue is now fixed with C++14. Here is the forked code and that compiles fine with C++14.

Comments

1

Constant expressions are defined in the last few pages of clause 5.

As a rough description, they are side-effect-free expressions that can be evaluated at compile-time (during translation). The rules surrounding them are created with this principle in mind.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.