r/cpp • u/Ok_Statistician_781 • 14d ago
Toggleable Annotations for C++26 Reflection
https://github.com/RyanJK5/rjk-flagHey all, I put together a very simple reflection utility that lets you make toggleable annotations, similarly to explicit(bool) or noexcept(bool).
Try it on Compiler Explorer!
inline constexpr rjk::flag serializable{};
inline constexpr rjk::flag skip_field{};
template <typename T>
struct [[ =serializable ]] MyType {
int x;
int y;
[[ =skip_field(std::is_pointer_v<T>) ]]
T data;
};
// serializable is applied unconditionally
static_assert(rjk::is_flag_set(^^MyType<int>, serializable));
// skip_field is applied conditionally
static_assert(not rjk::is_flag_set(^^MyType<int>::data, skip_field));
static_assert(rjk::is_flag_set(^^MyType<int*>::data, skip_field));
24
Upvotes
4
u/zerhud 14d ago
It seems auto& […p] = foo; can to be used . Next we can skip pointers inside serialize method with if constexpr. What is benefits from attributes?