1

While having a polemic discussion with a colleague regarding the use of static for data declared within h/hpp files whenever you choose to put the data in there, I was surprised to discover that:

const float a[100] = {1};

declared in a h/hpp does NOT throw a linker error if the header file is included by different translation units. At first I thought that it's because a is not used in either of them or because of some linker optimization, but even if I use a and build the debug configuration, still no linker error. I don't know why but I was sure that omitting static would throw a linker error in this scenario (I'm arguing it's always good practice to use static for data defined in headers and it's even better practice instead to not define big data (e.g. arrays) in headers in the first place, to avoid memory waste).

Am I remembering this wrong?

Zuzu Corneliu
  • 1,594
  • 2
  • 15
  • 27
  • Also see "const is implicitly static" and can be inlined in C++17: https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c – Den-Jason Sep 22 '20 at 08:33
  • 1
    There are **two** languages called C and C++ that are **distinct** languages, and within implementations there is some leeway to how this is handled... Which language do you want to ask about, and which implementation?! – Antti Haapala -- Слава Україні Sep 22 '20 at 09:54
  • @AnttiHaapala I thought it was clear from the title I was asking about both. I expected the behavior to be the same regarding this, it seems like my C background mislead me – Zuzu Corneliu Sep 22 '20 at 13:24

0 Answers0