google-测试和静态constexpr成员

google-test and static constexpr member

来自FAQ

如果您的 class 有一个静态数据成员:

// foo.h

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

您还需要在 foo.cc 中的 class 主体之外定义它:

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

否则你的代码是无效的 C++,并且可能会以意想不到的方式中断。特别是,在 Google 测试比较断言(EXPECT_EQ 等)中使用它会产生 "undefined reference" 链接器错误。

如果我使用 static constexpr 而不是 static const,我是否还应该在 foo.cc 中定义?

在 C++11 和 C++14 中,如果是 odr-used, even in the case of constexpr. However for the constexpr case, the separate definition ,则需要单独定义 foo