Need help with project

Pages: 123
TheToaster wrote:
You could also use an enum here right?

Yes I could, but the constexpr's that I used are compile time constants too. Both methods guarantee that they are compile time constants, and I think that's the most important thing.
However, in the constexpr version, the compiler is not prohibited from creating an actual variable from it. While a variable declared “constexpr” can only be initialized with a compile-time constant expression, to my understanding the compiler is not prohibited from creating a real variable from it and converting expressions like “if(c == 5)" into runtime comparisons at the lowest optimization levels.

See the following: https://godbolt.org/z/Qqfeiq

If you look at this compiler output from four C++ compilers at zero optimization level, you notice all of them create a physical local stack variable for the "constexpr int" also. https://godbolt.org/z/Ap7TCs

This is at the lowest optimization level of course, but higher optimization levels don’t guarantee the removal of redundant local variables.

This is just something interesting I found out about the other day and wanted to see what you thought about it.
Last edited on
TheToaster, thanks for pointing that out!
Topic archived. No new replies allowed.
Pages: 123