-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add reflection on attributes #13
Comments
There seems to be a significant pushback in the committee against the idea of reflection on attributes. The objection is that attributes today can be silently ignored by the compiler, and this feature is deemed extremely important for some reason. The suggestion of those who object is that these annotations need to use a keyword, rather than the attribute syntax. |
I dont understand why having attributes (today) silently ignored by the compiler make them unsuitable. Why not make them eventually silently ignored but mandatorily reflected? Why another keyword? That's questions for the committee... |
Hello everyone! I recently discovered an interesting way to describe custom attributes in C++17. I propose to consider it.
This short example shows that it is possible to implement it. At least - for one attribute :-) |
I would prefer a solution that does not require to use a wrapper type (eg The annotations (members do not need to have the same type/number of annotations): struct position
{
int distance;
float timestamp;
};
BOOST_DESCRIBE_STRUCT(position, (), (distance, timestamp))
BOOST_ANNOTATE_MEMBER(position, distance,
(unit, "meter"),
(doc, "The distance travelled from the start line"))
BOOST_ANNOTATE_MEMBER(position, timestamp,
(unit, "second"),
(doc, "The EPOCH timstamp since the beginning of the run")) And the usage:
https://godbolt.org/z/7PbobnMxW @pdimov Do you think something like this is worth adding to the library? |
@sdebionne Is there really a need for a separate
|
The rationale was mostly to keep annotation as an extension of Boost.Describe. But I like your suggestion too. I have a project that uses Annotation to generate a Json Schema from a described and annotated data structure. Here is the latest iteration of the annotation bits: If there is interest in this, I can move forward and make a PR with tests and doc... |
If i understand you right, your solution can be used like this:
This is perfect together with my suggestion and don't contradict it. |
It would be interesting for me, but i not a reviewer here:) |
Side note, the member annotations can also be functions: struct acquisition
{
int nb_frames = 1;
...
}
BOOST_ANNOTATE_MEMBER(acquisition, nb_frames,
(desc, "number of frames"),
(doc, "The number of frames to acquire (0 = continuous acquisition)"),
(validate, [](int nb_frames) { return nb_frames >= 0;})) Optional annotation can be handled pretty easily. Here we have a using Validate = BOOST_DESCRIBE_MAKE_NAME(validate);
template <typename L, typename T,
std::enable_if_t<boost::describe::has_annotation_by_name<L, Validate>::value, bool> = true>
bool validate(T const& t)
{
// With validation
auto validate = boost::describe::annotation_by_name_v<L, Validate>;
return validate(t);
}
template <typename L, typename T,
std::enable_if_t<!boost::describe::has_annotation_by_name<L, Validate>::value, bool> = false>
bool validate(T const& t)
{
// Without validation
return true;
} |
Following up discussion on cpplang, it would be interesting to have a way to extend member descriptors with custom information (think units, text description). User-defined attributes seems the right tool to express this custom information:
The emulation of the reflection would look something like:
We would have
describe_member_attributes<D>
returnsL<A1, A2, …, An>
, whereD
is a member descriptor.and
Ai
is an attribute descriptor of the formAs pointed out by @pdimov, there is a proposal about attribute reflection that introduces typed attributes.
The text was updated successfully, but these errors were encountered: