Skip to content

Conversation

@chaokunyang
Copy link
Collaborator

@chaokunyang chaokunyang commented Jan 23, 2026

Why?

The previous FORY_FIELD_INFO macro required defining field metadata outside the class/struct definition, which prevented it from accessing private fields. This limitation made it impossible to serialize classes with private members unless they were made public or friends were added.

What does this PR do?

This PR introduces a new FORY_STRUCT macro that must be used inside the class/struct definition as a replacement for FORY_FIELD_INFO. The key changes include:

  1. New FORY_STRUCT macro: Replaces FORY_FIELD_INFO and must be placed inside the struct/class body, enabling access to private fields via hidden friend functions
  2. Hidden friend function approach: Uses ADL (Argument-Dependent Lookup) to define ForyFieldInfo() as a friend function within the class, granting access to private members
  3. Backward compatibility: Keeps FORY_STRUCT_FIELDS for internal use and provides empty struct support via FORY_STRUCT_EMPTY
  4. Comprehensive migration: Updates all usages across:
    • Test files (encoder, serialization, field tests)
    • Documentation (C++ row format guide, xlang spec)
    • Examples (hello_row example)
    • README files

Migration pattern:

// Before:
struct MyClass {
  int private_field;
};
FORY_FIELD_INFO(MyClass, private_field);  // Outside class, no access to private

// After:
struct MyClass {
  int private_field;
  FORY_STRUCT(MyClass, private_field);  // Inside class, can access private
};

For external struct:

namespace thirdparty {
struct Foo {
  int32_t id;
  std::string name;
};

FORY_STRUCT_EXTERNAL(Foo, id, name);
} // namespace thirdparty

auto fory = Fory::builder().build();
fory->register_struct<thirdparty::Foo>(1);

To include base-class fields in a derived type, list FORY_BASE(Base) inside
FORY_STRUCT. The base must define its own FORY_STRUCT so its fields can be
referenced.

struct Base {
  int32_t id;
  FORY_STRUCT(Base, id);
};

struct Derived : Base {
  std::string name;
  FORY_STRUCT(Derived, FORY_BASE(Base), name);
};

Related issues

#2906

Does this PR introduce any user-facing change?

Yes, this introduces a breaking API change requiring users to migrate from FORY_FIELD_INFO to FORY_STRUCT.

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

No performance impact expected - this is a compile-time macro change that does not affect runtime serialization logic.

@chaokunyang chaokunyang changed the title feat(c++): support private fields of cpp class feat(c++): support private fields of c++ class Jan 23, 2026
@chaokunyang chaokunyang force-pushed the support_private_fields_of_cpp_class branch 4 times, most recently from dc1d81f to 27e78b6 Compare January 23, 2026 14:17
Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven’t taken a deep dive yet, but it looks fine based on the test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants