3

From the FAQ:

If your class has a static data member:

// foo.h

class Foo {
    ...
    static const int kBar = 100;
};

You also need to define it outside of the class body in foo.cc:

const int Foo::kBar; // No initializer here.

Otherwise your code is invalid C++, and may break in unexpected ways. In particular, using it in Google Test comparison assertions (EXPECT_EQ, etc) will generate an "undefined reference" linker error.

If instead of static const I use static constexpr, should I still have definition in foo.cc or not?

Jonas
  • 6,915
  • 8
  • 35
  • 53
Dmitry J
  • 867
  • 7
  • 20

1 Answers1

3

In C++11 and C++14, you need a separate definition of foo if is it odr-used, even in the case of constexpr. However for the constexpr case, the separate definition will not be required anymore in C++17.

Community
  • 1
  • 1
adl
  • 15,627
  • 6
  • 51
  • 65