Compile time or runtime? Compile time reflection would be completely painless and bloat-free.
replies(3):
I suppose C++'s template system might be able to generate JSON deserializers with static reflection as well
Swift got into this mess early in it's lifecycle and it's type checking is still more expensive than the rest of the compiler combined, and unpredictable on top of that.
It definitely can, and it will be faster and more type-safe than doing it at runtime. But if you do want to do it at runtime, well, it's possible to implement runtime reflection as a library on top of compile-time reflection, so someone will probably do that.
template<typename T>
T my_construct() { T result; return result; }
struct PolyReturn {
const int value;
operator int() const { return value; }
operator bool() const { return value > 0; }
};
https://cppinsights.io/s/f0b9976fedit: you get pseudo call-by-name as a bonus.