Dmitry J
Dmitry J

Reputation: 927

google-test and static constexpr member

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?

Upvotes: 4

Views: 1496

Answers (1)

adl
adl

Reputation: 16034

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.

Upvotes: 4

Related Questions