-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing addresses to members #184
Comments
Looks quite interesting. Please provide a PR. |
Gave it some thought and I don't really know how to implement it. Structured bindings won't help here, since essentially:
If you are aware of maybe compiler specific tricks that can let me do that - please help me. |
Maybe something like this? Probably not the safest version, but it works :) template <typename Ts, typename Us>
void change_data_without_a_check(Ts& mystruct, const std::string_view column_name, const Us new_data) {
size_t column_id = 0;
constexpr auto field_names = pfr::names_as_array<Ts>();
for (size_t i = 0; i < field_names.size(); i++) {
if (field_names[i].compare(column_name) == 0) {
column_id = i;
}
}
size_t i = 0;
pfr::for_each_field(mystruct, [&](auto& f, auto meta) {
if (i == column_id) {
void* voidptr = reinterpret_cast<void*>(&(pfr::get<meta>(mystruct))); // this part gives me the mem address of each field (msvc - c++20)
(*(Us*)(voidptr)) = new_data; // ... and some "don't try this at home" assignments
}
i++;
});
}
struct S { char c; uint8_t u; };
int main(int argc, const char* argv[]) {
auto s2x = S('A');
uint8_t newvaluex = 100;
std::string mycolumnx = "c";
change_data_without_a_check(s2x, mycolumnx, newvaluex);
char newvaluex2 = 'A';
std::string mycolumnx2 = "u";
change_data_without_a_check(s2x, mycolumnx2, newvaluex2);
std::cout << s2x.c << " " << s2x.u << std::endl; } ``` |
As far as I understand, in the code above there is no point at which we have pointer of type |
Got it, I thought your main concern was getting the memory address of each member. |
Hey, I have some pybinded structs, that have a lot of repeating boilerplate:
I was thinking about using
boost::pfr
to automate this, but I don't think there is currently any way, sinceboost::pfr
doesn't have an interface to access member addresses i.e. something like this:Is there any interest in having such interface? And if so - I think I can make a PR. If you could give me some hints on possible implementation - that would be greatly appreciated.
Thanks!
The text was updated successfully, but these errors were encountered: