4 ms·
I thought constexpr implies const: https://godbolt.org/z/u3a1pq https://godbolt.org/z/u3a1pq That is what bothers me about constexpr. It conflates compile-tim
by haberman 7y ago
I thought constexpr implies const: https://godbolt.org/z/u3a1pq https://godbolt.org/z/u3a1pq
That is what bothers me about constexpr. It conflates compile-time evaluation with non-mutability. I want a keyword that guarantees an evaluation happens at compile-time, but doesn't by itself imply "const".
- bigcheesegs 7y agoYeah, on variable decls it does indeed mean const. It also means inline on functions and static data members (but not global variables).
- gumby 7y agoI'm not even sure what that would mean since the machinery to compute the value won't exist in the binary. You can get this behavior by initializing a variable with the value of another variable whose value is a constexpr. This is a case in which the order of initialization of global variables is not ambiguous! constexpr int foo_init_value = 2 + 4; int foo = foo_init_value; Actually not a bad idiom. But like you I do wish you could apply constexpr-like behavior to expressions rather than just statements: What you're If you could really apply really applied to expressions you could write something like int foo = _compute_at_compile_time(2 + 4); Of course the compiler can already do this degenerate example for you.
- bigcheesegs 7y agoThe first example is handled by constinit (or whatever we end up calling it) and the second is handled by consteval, although it has to be put in a function.
- Jasper_ 7y agoIt doesn't even guarantee compile-time evaluation; it only guarantees that it can be evaluated at compile time. There is no guarantee for the compiler to actually do so --and some compilers will barf on complex constexpr. Future editions of C++ add consteval to ensure that it is evaluated at compile time.