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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With