1

It is more of the rhetorical question (and a rant). Pre-11 every time I had to make a library which exhibited static const char* const (as in static const char* const class_name = "ClassA";) as class members, I knew the library could no longer be header-only – I had to provide a .cpp file with a definition of this variable and its value.

So instead, I had to turn it into the static function name(), returning the pointer.

Then C++11 came, and now I can have static constexpr char[] as my member – and I can even give it a value in my header! But I still have to provide the definition… So I am not excited at all.

Why would that be the case? If constexpr can be evaluated by compiler at compile time, why do I need a definition of it? Why does it have to have linkage at all?

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • @Andy, sure thing. Will edit. – SergeyA Jan 28 '16 at 16:36
  • Possible duplicate: http://stackoverflow.com/questions/28530583/strange-behavior-with-constexpr-static-member-variable. – R Sahu Jan 28 '16 at 16:42
  • Duplicate: http://stackoverflow.com/q/23428684 – Kerrek SB Jan 28 '16 at 16:43
  • 1
    You can totally have a `static constexpr const char *` member in header-only. And if you actually need an *array* rather than a string, well, you probably want it to have linkage. – 5gon12eder Jan 28 '16 at 16:46
  • Don't agree with duplicates. Those questions ask 'how', I ask 'why'. – SergeyA Jan 28 '16 at 16:46
  • @AndyG, not true. See either of the links suggested as possible duplicates. – R Sahu Jan 28 '16 at 16:51
  • @Andy, it does: http://coliru.stacked-crooked.com/a/dc81a7ece8637724 – SergeyA Jan 28 '16 at 16:51
  • Well, you can have that member but you cannot ODR-use it which makes it pretty useless. But as I've commented before, if you only want a string, use a pointer rather than an array. – 5gon12eder Jan 28 '16 at 16:53
  • @RSahu and SergeyA: Today I learned something. Thank you :-) – AndyG Jan 28 '16 at 16:54
  • 2
    I think this is an interesting question. Those other questions people have linked to show the standard requires it, but I too would like to understand *why* the standard requires it (and why the linker can't just auto-resolve it for `constexpr` types). Were there some particular problems the committee was trying to avoid when requiring this? Is it just an oversight? – Cornstalks Jan 28 '16 at 16:59
  • Are you talking about `char[]` specifically or about any `static constexpr` members? Because the rules are the same for `int`s or any other type. You cannot ODR-use a `static constexpr int = 42;` without a definition in a source file either. Thanks to constant propagation, a plain old `int`, unlike an array, can be useful in non-ODR-uses as well, however. – 5gon12eder Jan 28 '16 at 17:32

1 Answers1

0
  • Your are talking of static constexpr, so you have to deal with static keyword
  • Static imposes that you declare it in outside the header because of the one definition rule. This rule means that you cannot declare the same variable more thhan once. If you could declare it in the header and you included the header in two places, you would declare the variable twice.

  • However, as you can do it compile time, you can save a lot of startup time for your program (remember the fibonacci example)

Bottom line: you have the boiring part of the syntax of static variable, but you can save some runtime with it

More details here for the static part here:

Why does constexpr static member (of type class) require a definition?

Gabriel
  • 3,564
  • 1
  • 27
  • 49