-1

enter image description here

The constexpr is not evaluated in compile-time as seen in the assembly(CALL instruction), why?

(Using most recent gcc that comes with codeblocks (g++ 4.7.1) with -std=c++11)

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
Oded
  • 72
  • 1
  • 8

3 Answers3

2

Your getOdd() isn't constexpr and the compiler is certainly not required to propagate constant expressions through non-constexpr functions. Also, did you compile with optimization enabled? Without optimizations compilers tend to do no otimizations at all.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
2

constexpr doesn't guarantee compile-time evaluation. It only guarantees that the constexpr, when fed compile-time constant inputs itself resolves to a compile-time constant.

In this case, the compiler chose not to evaluate getOdd(7), even though it could have. (Note, getOdd itself isn't constexpr even though isEven is.)

The compiler might choose to optimize and inline, for example, you increase your optimization level. But that has little to do with constexpr.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • Yea, like Dietmar wrote, even when I declare getOdd as constexpr too its still not optimizing. When I raised my optimization level it did. Thanks :) – Oded Nov 22 '13 at 20:31
0

g++ -O3 will evaluate the expression at compile-time.

Note by the way that a simpler implementation of getOdd would be to return t | 1.

Rémi
  • 3,705
  • 1
  • 28
  • 39
  • Thanks, good answer but not very detailed as Joe Z's. And the t | 1, I knew that, but I wanted to test the constexpr mechanism. – Oded Nov 22 '13 at 20:34