C++ Logo

sg7

Advanced search

[isocpp-sg7] Customized metadata for reflection

From: ykiko <ykikoykikoykiko_at_[hidden]>
Date: Sun, 12 May 2024 23:24:32 +0800
I think P2996 lacks an important feature, that is, customized metadata for
reflection. As we all know, we can serialize and deserialize struct
automatically through static reflection, but sometimes, for some fields
(maybe private), we want to skip it. It is a common demand, But
unfortunately, there is no way to allow users to attach metadata to fields
in P2996 currently.

In other languages, customized metadata is widely used. For example, we can
mark a field *[JsonIgnore] *in *C#*
<https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm> and
*#[serde(skip_deserializing)] *in *Rust*
<https://serde.rs/field-attrs.html#skip> to skip fields for serialization.
Besides, although C++ does not have reflection now, some frameworks
implement reflection on their own through code generation. They all support
customized metadata. For example, *UPROPERTY
<https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/GameplayArchitecture/Properties/>
*in
UE and *Q_PROPERTY <https://doc.qt.io/qt-6/properties.html> *in QT. Clang
and GCC also support an attribute to expose metadata in AST, which is
[[clang::annotate("")]]; some code generation tools use it to customize
metadata.

So, in my opinion, we cannot write a serialization library based on P2996
that is as convenient as those in other languages without a way to allow
users to customize metadata. I suggest adding a new attribute
*[[annotate(constant-expression,
...)]] *to allow users to attach metadata to some entries. And, we can use
reflection to get the annotations of entries.

This topic may be related to the proposal *Attributes Introspection. *And
the idea is at an early stage, any suggestion is welcome.

ykiko.

Received on 2024-05-12 15:24:46