9

So, I'm aware that in C++ static members can be initialized inside the class if they are a const literal type like the following

class test{
public:
        static constexpr int stc = 1;
private:
        int a = 0;
        int b = 0;
        int c = 0;
};

and the static constexpr variable stc can be used where the compiler can directly substitute the value of the member i.e

int main () {int array[test::stc];}  

However, if used in a context where the value cannot be directly substituted by the compiler:

int main() { const int &cs = test::stc; } 

then the compiler (clang) generates an error

c++ -std=c++11 -pedantic    t.cpp   -o t
Undefined symbols for architecture x86_64:
  "test::stc", referenced from:
      _main in t-a8ee2a.o
ld: symbol(s) not found for architecture x86_64

unless the static member is defined outside the class like so:

constexpr int test::stc;

Why is this the case?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
The Enigma
  • 137
  • 2
  • 6
  • Which compiler are you using, what compiler error are you getting, and what is the minimal but complete code which triggers that error? – Mário Feroldi Apr 30 '18 at 23:03
  • @MárioFeroldi updated post with more details – The Enigma Apr 30 '18 at 23:59
  • 1
    Not sure if this answers your question: [Why does a static data member need to be defined outside of the class?](https://stackoverflow.com/questions/18749071/why-does-a-static-data-member-need-to-be-defined-outside-of-the-class) – cpplearner May 01 '18 at 00:02
  • `const &cs...` gives an error: `error: ISO C++ forbids declaration of 'cs' with no type [-fpermissive]` – Joseph D. May 01 '18 at 00:39
  • Possible duplicate of [constexpr: definition and declaration for constexpr members](https://stackoverflow.com/questions/44721729/constexpr-definition-and-declaration-for-constexpr-members) – Passer By May 01 '18 at 01:19

3 Answers3

6

In

int main() { const int &cs = test::stc; } 

test::stc is odr-used while in

int main () {int array[test::stc];}  

it is not.

The following example from the C++11 Standard supports the above idea.

struct S { static const int x = 0; };
const int &f(const int &r);  
int n = b ? (1, S::x)    // S​::​x is not odr-used here
          : f(S::x);     // S​::​x is odr-used here, so a definition is required

Looking at it from practical point of view, cs will be an invalid reference unless test::stc has an address. array, on the other hand, needs just the value of test::stc, which can be evaluated at compile time. array does not need the address of test::stc to be a valid object.

An object that is odr-used must be defined exactly once in a program.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2
static constexpr int stc = 1; // declares the static var

constexpr int test::stc; // defines the static var

for more detailed explanation check link below

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

uzivanky
  • 93
  • 6
1

C++17 inline variables

In C++17 if you also mark the static member as inline, then I believe that you can odr-use it freely or have multiple definitions across compilation units, e.g.:

#include <iostream>

class MyClass {
    public:
        inline static constexpr int i = 42;
};


int main() {
    const int &cs = MyClass::i;
    std::cout << cs << std::endl;
    std::cout << &MyClass::i << std::endl;
}

More info at: How do inline variables work?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985