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

Mechanism to check if a C++ member is private

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

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

如果你想断言类型 Bar 没有名为 foo 的 public 成员,你可以编写以下测试:

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");

这是一个demo

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

class test {
public:
    int pub;
private:
    int prv;
};

运行 这个命令: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'

这很容易通过脚本解析。或者您可以使用 Clang AST 库创建 LibASTMatcher 来验证使用数据本身,如 documentation.

中所述

在我测试 non-constant 对象时附加 cigien's correct answer,即 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;
}

然后我就这样插入我的单元测试框架(提升单元测试):

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{}));
}