6 ms·
>In conclusion, at the time of this writing, you need to inspect the generated code of your compiler, if you want to be sure that something is really calculated
by andrepd 9y ago
>In conclusion, at the time of this writing, you need to inspect the generated code of your compiler, if you want to be sure that something is really calculated at compile time.
I don't get this. Why? Doesn't making a variable constexpr ensure it is a compile time value, or it will fail to compile?
- bilboa 9y agoApparently constexpr is like "inline". Just as the inline keyword doesn't actually guarantee that the compiler will inline a function, so constexpr doesn't require the compiler to precompute something, but rather just gives a hint to the compiler that you'd like it to if it can.
- grenoire 9y agoThere might be a case where you expected the compiler to constexpr something but it didn't. I can imagine cases where a heavy function getting called more than once, because you had expected it to be pre-computed.
- banachtarski 9y agoLiterally the second example shows a failing case for Clang 5 where GCC is able to statically compute it at compile time.
- andrepd 9y agoThat sounds like a bug in Clang?
- andrewprock 9y agoIt may not be a bug. The computation of Fibbonacci(10) requires a stack of ten frames. It may be that Clang has a limit on the depth of the stack for pre-compiled code.
- phaedrus 9y agoAt first I was going to say that it's similar to asm.js - that it enabled an optimization but also could fall back to just running the code. However in trying to find references to back up my answer, I found a different explanation of constexpr which changed my own understanding. https://stackoverflow.com/questions/38879475/why-cant-constexpr-just-be-the-default https://stackoverflow.com/questions/38879475/why-cant-conste... Quoting Ben Voigt: "What constexpr does is provide guarantees on what data-flow analysis a compliant compiler is required to do to detect1 compile-time-computable expressions, and also allow the programmer to express that intent so that they get a diagnostic if they accidentally do something that cannot be precomputed."
- mnarayan01 9y ago> Doesn't making a variable constexpr ensure it is a compile time value It ensures that the variable could be evaluated at compile time, where "could be evaluated at compile time" means "conforms to the rules set forth in the C++ standard". Whether or not it is computed at compile time is dependent on the compiler.
- mehrdadn 9y ago> It ensures that the variable could be evaluated at compile time Why couldn't a variable without constexpr be evaluated at compile-time? Nobody ever explains this.
- mikepurvis 9y agoThere's precedent in C++ for keywords that give the compiler hints it doesn't necessarily promise to act on: inline.
- mehrdadn 9y agoHappy to be corrected (EDIT: indeed, I stand corrected! see replies below), but I don't think the C++ spec says anything about 'inline' encouraging the compiler to inline the compiled code? 'auto' would've been an example, but that also got removed, because they realized it was useless. I don't get how constexpr is any different.
- mikepurvis 9y agoI don't know my way around the spec, but Wikipedia is pretty unequivocal: "... it serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call." https://en.wikipedia.org/wiki/Inline_function https://en.wikipedia.org/wiki/Inline_function
- 9y ago
- nightcracker 9y agoFor integral values you can use a little hack to ensure compile-time evaluation: namespace detail { template<class T, T val> constexpr T eval() { return val; } }; #define FORCE_COMPILE_TIME(x) detail::eval<decltype(x), x>()
- beeforpork 9y agoThis is exactly why I don't like C++: it is just too complicated, and it encourages to write complicated code, usually opaque boilerplate template hacks for what would seem a simple concept. This is difficult to read, review, understand, and hence error-prone. No, not only error-prone, but bug-encouraging. BTW, you can also enforce compile time evaluation like this: enum { aux1 = X }; use(aux1); But that's two lines, I know. And, of course, it is still a hack ('enum'? What?).
- ben-schaaf 9y agoInterestingly D actually used `enum` to define compile time values (as opposed to `alias` which is used for types). If you want a compile time value you can do this: enum aux1 = X;
- raverbashing 9y agoOh look another flag that has several caveats and corner cases and works only half the times you think it works, while increasing compiler complexity Here's a better idea, if you really need a compile time value in your program you calculate it yourself (or just run it at program startup and cache the value).
- weberc2 9y agoOr the compiler could behave intuitively, but that's hard in C++ due to all of the past decisions that need to be supported.