Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google-test and static constexpr member

Tags:

c++

googletest

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?

like image 488
Dmitry J Avatar asked Oct 15 '25 04:10

Dmitry J


1 Answers

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.

like image 183
adl Avatar answered Oct 16 '25 19:10

adl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!