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*

10
  • I'm not familiar with the function scope problem. I see that g++ behaves as you state, but MSVC accepts that allegedly invalid code. g++ says that i isn't constant, but allows its use as array size (and not a VLA). That is inconsistent. g++ accepts the code with static added. Are you sure that you're not reporting a compiler quirk (bug) as a rule of the language? If it is a rule, what is it? Commented Feb 8, 2017 at 8:28
  • @Cheersandhth.-Alf I think RSahu is right one cannot supply address that is runtime dependent to constexpr pointer. Imagine the situation when the foo is called recursively. ri should get different value at each recursion level, but how many recursion levels are there? It may depend on runtime value... Commented Feb 8, 2017 at 8:33
  • @W.F.: I would not offhand think that argument would hold, because there's no sense in giving i a different adress determined at run time, but with a value known at compile time. That's just idiocy. If there is such a rule in the current language definition, then that's clearly a defect in the standard. So I'm interested in what the (alleged) rule is. Commented Feb 8, 2017 at 8:36
  • 1
    @RSahu: I've searched through the C++ 14 standard and perused google results such as (stackoverflow.com/a/38853356/464581), and it seems as you're right about the formal: a constexpr object needs not have static storage duration, and probably a local non-static one is required to behave as-if it's of automatic storage duration. This supports (1) constexpr objects that are not really constant because they have mutable parts, (2) per translation-unit optimization where an non-ODR used non-static constexpr object is removed, and (3) just wasting programmers' time. A defect. Commented Feb 8, 2017 at 9:41
  • 2
    Thanks for noting this, I learned something new today. I thought that kind of nonsense only started with C++14 and bloomed with C++17. Non-constant constepxr objects. Grumble, grumble... Argh! Commented Feb 8, 2017 at 9:43