program do calculation at compile time or at run time ? see this example https://pastebin.com/yAh9eyDN
Well for that simple code, if you don't disable optimization, the value stored in x would be 200. Assuming you don't do optimization, and you add constexpr specifier to the function, yes the calculation will be done at compile time. x would be 200
what kind of optimization ?
compilers usually do optimization by default, you can enable, disable these using compiler flags
What compiler do you normally use? msvc? gcc, clang?
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
ah i forgot to mention, constexpr doesn't gurantee that the expression will be evaluated during compile time :D
constexpr guaranties compile time calculation. Non-constexpr may or may not be optimized away, unless you disable optimization, and then it will, well, not optimized
consteval is the only guarantee
Обсуждают сегодня