constexpr question

Discussion of chess software programming and technical issues.

Moderator: Ras

Rein Halbersma
Posts: 749
Joined: Tue May 22, 2007 11:13 am

Re: constexpr question

Post by Rein Halbersma »

Rein Halbersma wrote: Wed Sep 14, 2022 10:39 pm But this wasn't the OP's question. Simply initializing a constexpr variable inside foo() and then returning that, will simplify the program at -O0 already. https://gcc.godbolt.org/z/d8YzshsTW
For the macro-lovers, it's possible to define an immediately invoked lambda:

Code: Select all

#define FORCE_CONSTEXPR(expr) [&]() \
    { constexpr auto x = (expr); return x; }()
and write

Code: Select all

if (x & FORCE_CONSTEXPR((Rank6 | Rank7 | Rank8)))
Sesse
Posts: 300
Joined: Mon Apr 30, 2018 11:51 pm

Re: constexpr question

Post by Sesse »

Rein Halbersma wrote: Wed Sep 14, 2022 10:39 pm But this wasn't the OP's question.
No, but neither was OP asking about the C++ standard. When people start talking about “the Standard guarantees” this or that, don't be surprised if those guarantees don't hold. :-)

In practice, every sane compiler in optimizing mode (you don't care about performance on -O0, right?) will constant-fold all the simple cases, constexpr or not.