TheNitesWhoSay / RareCpp

Creating a simpler, more intuitive means of C++ reflection
MIT License
124 stars 6 forks source link

Iterate through fields and query types without an instance #95

Closed jsoulier closed 2 years ago

jsoulier commented 2 years ago

Hey, I am looking to iterate through the fields of a struct and query their sizes without needing an object instance. I'm sure that there is method provided but I don't see it.

Currently, I have a working function similar to the following.

template <typename T>
void func() {
    T t;
    T::Class::ForEachField(t, [&](auto &field, &auto value) {
        // sizeof(value)
    });
}

Is there anyway to do something like the following instead?

template <typename T>
void func() {
    T::Class::ForEachField([&](auto &field) {
        // query type sizes e.g. sizeof(field.Type)
    });
}

Thanks

TheNitesWhoSay commented 2 years ago

Sure can, example: https://godbolt.org/z/nvve6sxWd

jsoulier commented 2 years ago

Appreciate it, thanks.