Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

19
  • 2
    @AndrewLazarus, you can't cast away const from a const object, only from a const X* which points to an X. But that's not the point; the point is that automatic objects cannot have static addresses. As I said, constexpr ceases to be meaningful once the compilation is finished, so there is nothing to cast away (and quite possibly nothing at all, because the object is not even guaranteed to exist at runtime.) Commented Dec 13, 2012 at 23:54
  • 50
    I kind of feel like not only is this answer incredibly confusing but also self contradictory. For example you say that you almost always want static and constexpr but explain that they are orthogonal and independent, doing different things. You then mention a reason to NOT combine the two since it would ignore ODR-usage (which seems useful). Oh and I still don't see why static should be used with constexpr since static is for runtime stuff. You never explained why static with constexpr is important. Commented Oct 1, 2014 at 22:08
  • 4
    @void.pointer: You're right about the last paragraph. I changed the intro. I thought I had explained the importance of static constexpr (it prevents the constant array from having to be recreated on every function call), but I tweaked some words which might make it clearer. Thanks. Commented Oct 2, 2014 at 3:31
  • 20
    Might also be useful to mention compile time constants vs runtime constants. In other words, if a constexpr constant variable is only used in compile-time contexts and never needed at runtime, then static makes no sense, since by the point you get to the runtime, the value has been effectively "inlined". However, if constexpr is used in runtime contexts (in other words, the constexpr would need to be converted to const implicitly, and available with a physical address for runtime code) it will want static to ensure ODR compliance, etc. That is my understanding, at least. Commented Jun 11, 2015 at 17:54
  • 10
    An example for my last comment: static constexpr int foo = 100;. There is no reason why the compiler couldn't substitute usage of foo everywhere for literal 100, unless code were doing something like &foo. So static on foo has no usefulness in this case since foo doesn't exist at runtime. Again all up to the compiler. Commented Jun 11, 2015 at 17:56