检查 C++ 成员是否为私有的机制

Con*_*man 38 c++ unit-testing private-members template-meta-programming

我正在为本科生编写单元测试,并希望将某些成员强制为公共或私有成员。我知道实际测试私有成员的方法,例如,#define private public或使用朋友类,但没有看到任何可以让我准确检查成员是否为私有的方法。

蛮力方法将尝试编译并解析输出错误,例如,寻找类似的东西error: 'foo' is a private member of 'Bar',但我希望有人会有更好的技巧!

cig*_*ien 34

如果要断言某个类型Bar没有名为 的公共成员foo,则可以编写以下测试:

template<typename T>
constexpr auto has_public_foo(T const &t) -> decltype(t.foo, true) 
{
    return true;
}

constexpr auto has_public_foo(...) 
{
    return false;
}

static_assert(not has_public_foo(Bar{}), "Public members are bad practice");
Run Code Online (Sandbox Code Playgroud)

这是一个演示

  • 请不要用“公共成员是不好的做法”来测试人们。他们从小就认为公众成员是邪恶的,然后当出现公众成员有用的情况时,他们仍然遵循你教给他们的教条。这就是最终每个成员都拥有 getter 和 setter 函数的方式 (26认同)
  • @PeterMortensen (1) 不,公共成员不是全局变量(您可能会想到静态成员)。(2) POD 类没有任何问题,即使 POD 类中也有辅助函数。(3) 教导人们“公共变量是邪恶的”,教导他们 POD 类是邪恶的。 (11认同)
  • @user253751这是合理的,但这应该是对OP问题的评论,不是吗?答案只是OP正在尝试做的事情的解决方案。 (5认同)
  • 是的,确实如此,但尚不清楚这些是否是 OP 的要求。 (2认同)
  • @cs95 getter 和 setter 都有问题。公共成员和具有普通 getter 和 setter 的成员之间没有区别。如果您依赖此类成员,您就是在对数据进行建模,而不是对行为进行建模。正确的面向对象类通常不需要 getter/setter,因为它提供了更高抽象级别的功能。例外情况很少见。 (2认同)

Sla*_*ica 16

您可以使用Clang 编译器的AST输出来验证某个成员是私有的还是公共的。例如,对于以下代码:

class test {
public:
    int pub;
private:
    int prv;
};
Run Code Online (Sandbox Code Playgroud)

运行此命令: clang -Xclang -ast-dump -fsyntax-only t.cpp

给出 AST 转储:

TranslationUnitDecl 0x55f6f550e3f8 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55f6f550e9b0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55f6f550e690 '__int128'
|-TypedefDecl 0x55f6f550ea20 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55f6f550e6b0 'unsigned __int128'
|-TypedefDecl 0x55f6f550ed68 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
| `-RecordType 0x55f6f550eb10 '__NSConstantString_tag'
|   `-CXXRecord 0x55f6f550ea78 '__NSConstantString_tag'
|-TypedefDecl 0x55f6f550ee00 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55f6f550edc0 'char *'
|   `-BuiltinType 0x55f6f550e490 'char'
|-TypedefDecl 0x55f6f5545bf8 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
| `-ConstantArrayType 0x55f6f5545ba0 '__va_list_tag [1]' 1
|   `-RecordType 0x55f6f550eef0 '__va_list_tag'
|     `-CXXRecord 0x55f6f550ee58 '__va_list_tag'
`-CXXRecordDecl 0x55f6f5545c50 <t.cpp:1:1, line:6:1> line:1:7 class test definition
  |-DefinitionData pass_in_registers trivially_copyable trivial literal
  | |-DefaultConstructor exists trivial needs_implicit
  | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveConstructor exists simple trivial needs_implicit
  | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
  | |-MoveAssignment exists simple trivial needs_implicit
  | `-Destructor simple irrelevant trivial needs_implicit
  |-CXXRecordDecl 0x55f6f5545d78 <col:1, col:7> col:7 implicit class test
  |-AccessSpecDecl 0x55f6f5545e10 <line:2:1, col:7> col:1 public
  |-FieldDecl 0x55f6f5545e50 <line:3:5, col:9> col:9 pub 'int'
  |-AccessSpecDecl 0x55f6f5545e98 <line:4:1, col:8> col:1 private
  `-FieldDecl 0x55f6f5545ed8 <line:5:5, col:9> col:9 prv 'int'
Run Code Online (Sandbox Code Playgroud)

这很容易通过脚本解析。或者,您可以使用 Clang AST 库创建LibASTMatcher以使用文档中所述的数据本身进行验证。

  • 谢谢你!这很棒并且工作得很好,但我选择独立于编译器。我可能会在未来的学期实施这一点。 (3认同)

Con*_*man 7

在我测试非常量对象时附加cigien 的正确答案,即T在对象的构造过程中进行了更改,因此将在运行时而不是编译时进行测试。这仅涉及删除const关键字:

// Check that color is a public member
template<typename T>
auto has_public_color(T &t) -> decltype(t.color, true)
{
    // Returns true if T has a public member named color
    return true;
}
auto has_public_color(...)
{
    return false;
}
Run Code Online (Sandbox Code Playgroud)

然后我只是插入到我的单元测试框架(Boost 单元测试)中:

BOOST_AUTO_TEST_CASE(test_cell_no_public_color)
{
    BOOST_TEST_MESSAGE("Testing that Cell has no public member of color");
    // Check that Cell has no public color
    BOOST_CHECK(not has_public_color(Cell{}));
}
Run Code Online (Sandbox Code Playgroud)

  • 我还执行相反的操作并检查颜色是否作为私有成员存在。他们刚刚学习如何定义类,我想强制执行一些抽象和封装原则,以便他们按照严格的方向编写程序。他们更专注于学习 C++ 语法而不是实际的程序设计。 (2认同)