For the last 10 years, attempts are being made to add reflection to C++. The latest proposal is for c++23 and may or may not get in.
Unlike reflection in most languages, the plan for c++ reflection is compile time reflection. So at compile time, you can reflect over struct members, function and method parameters and properties, enumeration values and names, etc.
You can then do limited reification, injecting information about what you reflected over to generate other types and code.
While this is a bit strange, what it means is that programs that do not use reflection do not pay a run time cost for it. It also is incredibly powerful.
The simplest example is that you can use it to implement runtime reflection.
struct Member {
std::string_view name;
std::any_ref value;
};
struct Reflectable {
virtual std::span<Member> GetMembers() const = 0;
virtual std::span<Member> GetMembers() = 0;
};
template<class D>
struct ImplReflectable:Reflectable {
std::span<Member> GetMembers() const final;
std::span<Member> GetMembers() final;
};
template<class D>
std::span<Member> ImplReflectable<D>::GetMembers() const {
// compile time reflection code on D here
}
template<class D>
std::span<Member> ImplReflectable<D>::GetMembers() {
// compile time reflection code on D here
}
you write the above once, and suddenly you for any type you want reflectable, you can just do this:
struct Point : ImplReflectable<Point> {
int x, y;
};
and a reflection system is attached to Point.
The library that implements this runtime reflection can be as complex and powerful as you like. Each type would have to do a bit of work (like the above) to opt in, but doing so for a UI library (for example) isn't a serious problem. Types that don't opt in continue the C++ assumption of "don't pay for it if you aren't using it".
But that is only the start. One proposal, metaclasses, permits:
interface Reflectable {
std::span<Member> GetMembers() const;
std::span<Member> GetMembers();
};
you can have metaclasses, or functions that take types and return them. This allows you to define metaclasses of class, like "interface", written in-language. Now, interface is a bit of a toy, but you could write QObject or Reflectable or PolymorphicValueType or NetworkProtocol metaclasses that modify what your class definition means.
This may or may not get into c++23. It continues to get better, but it also continues to get pushed back. There are multiple compile time reflection implementations out there for most major C++ compilers you can try. The syntax is in flux, as there are symbol-operator based reflection libraries, reflexpr based operator reflection libraries, some where the reflected data is types, others where it is constexpr objects and consteval functions.