Skip to content

Instantly share code, notes, and snippets.

@Cylix
Created October 19, 2020 05:52
Show Gist options
  • Save Cylix/5081764fb21c5b42f42262281211a1df to your computer and use it in GitHub Desktop.
Save Cylix/5081764fb21c5b42f42262281211a1df to your computer and use it in GitHub Desktop.
Reflection in C++14 - Member Function #5
//! register specific member function
template <typename ReturnType, typename... Params>
void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)>& f) {
//! wrap the instanciation of a new object of type Type and the call to the function f in a C++11 lambda
auto f_wrapper = [f](Params... params) -> ReturnType {
return (Type().*f.second)(params...);
};
//! store function information
functions.push_back({
f.first, //! function name
std::make_shared<function<ReturnType(Params...)>>(f_wrapper) //! function object
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment