google-test and static constexpr member

google-test and static constexpr member

本文关键字:member constexpr static and google-test      更新时间:2023-10-16

来自FAQ:

如果您的班级有静态数据成员:

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

您还需要在foo.cc的班级主体外定义它:

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

否则,您的代码为无效C ,并且可能以意外的方式破裂。特别是,在Google测试比较中使用它(Expect_eq等)将生成"未定义的参考"链接器错误。

如果我使用 static constexpr,而不是 static const,我是否仍然在foo.cc中有定义?

在C 11和C 14中,即使在constexpr的情况下,您也需要单独的foo定义。但是,对于constexpr情况,C 17中将不再需要单独的定义。