You can do that using metadata available at compile time. However, we must do it manually:
template<typename Class, typename T>
struct Property {
constexpr Property(T Class::*aMember, const char* aName) : member{aMember}, name{aName} {}
using Type = T;
T Class::*member;
const char* name;
};
template<typename Class, typename T>
constexpr auto makeProperty(T Class::*member, const char* name) {
return Property<Class, T>{member, name};
}
Now we have a class that can hold our desired metadata. Here's how to use it:
struct Dog {
constexpr static auto properties = std::make_tuple(
makeProperty(&Dog::barkType, "barkType"),
makeProperty(&Dog::color, "color")
);
private:
std::string barkType;
std::string color;
};
Now we can do iteration on it by recursion:
template<std::size_t iteration, typename T, typename U>
void accessGetByString(T&& object, std::string name, U&& access) {
// get the property
constexpr auto property = std::get<iteration>(std::decay_t<T>::properties);
if (name == property.name) {
std::forward<U>(access)(std::forward<T>(object).*(property.member));
}
}
template<std::size_t iteration, typename T, typename U>
std::enable_if_t<(iteration > 0)>
getByStringIteration(T&& object, std::string name, U&& access) {
accessGetByString<iteration>(std::forward<T>(object), name, std::forward<U>(access));
// next iteration
getByStringIteration<iteration - 1>(std::forward<T>(object), name, std::forward<U>(access));
}
template<std::size_t iteration, typename T, typename U>
std::enable_if_t<(iteration == 0)>
getByStringIteration(T&& object, std::string name, U&& access) {
accessGetByString<iteration>(std::forward<T>(object), name, std::forward<U>(access));
}
template<typename T, typename U>
void getByString(T&& object, std::string name, U&& access) {
getByStringIteration<std::tuple_size<decltype(std::decay_t<T>::properties)>::value - 1>(
std::forward<T>(object),
name,
std::forward<U>(access)
);
}
Then finally, you can use this tool like:
struct MyAccess {
void operator()(int i) { cout << "got int " << i << endl; }
void operator()(double f) { cout << "got double " << f << endl; }
void operator()(std::string s) { cout << "got string " << s << endl; }
}
Dog myDog;
getByString(myDog, "color", MyAccess{});
This for sure could by simplified with overloaded lambda. To know more about overloaded lambda, see this blog post
The original code was taken from that answer: C++ JSON Serialization
There's a proposal to make things like this easier.
This is covered by P0255r0