Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Reflection for C++26. Parent comment
Serialization
#include <print> template<typename T> void serialize(const T& obj, std::ostream& os) { for_each(reflect(T), [&](auto member) { std::print("{}: {}\n", member.name(), member.get(obj)); }); }
class Person { public: Person(const std::string& name, int age) : name(name), age(age) {} std::string getName() const { return name; } void setName(const std::string& name) { this->name = name; } int getAge() const { return age; } void setAge(int age) { this->age = age; } private: std::string name; int age; REFLECT_PROPERTIES( (name, "Name of the person"), (age, "Age of the person") ) }; int main() { Person person("Alice", 30); auto properties = reflect::getProperties<Person>(); for (const auto& prop : properties) { std::print("Property: {} ({})\n", prop.name, prop.description); auto value = reflect::get(person, prop.name); std::print("Value: {}\n", value); if (prop.name == "age") { reflect::set(person, prop.name, 31); } } std::print("Updated age: {}\n", person.getAge()); return 0; }
template<typename T> void printTypeInfo() { constexpr auto info = reflect(T); std::print("Type name: {}\n", info.name()); std::print("Member count: {}\n", info.members().size()); }
template<typename T> void printAllMembers(const T& obj) { for_each(reflect(T), [&](auto member) { std::print("{}: {}\n", member.name(), member.get(obj)); }); }
There are no comments on this comment yet.
Serialization
Simplified property systems Simplified template metaprogramming Generic algorithm for printing all members