Alok Save
Alok Save

Reputation: 206646

Why is addition allowed in a constexpr function, but increment isn't?

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?

Upvotes: 4

Views: 637

Answers (3)

iammilind
iammilind

Reputation: 70094

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.

Upvotes: 2

Jonathan Wakely
Jonathan Wakely

Reputation: 171471

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.

Upvotes: 7

Andrew Tomazos
Andrew Tomazos

Reputation: 68738

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.

Upvotes: 1

Related Questions