Skip to content

Instantly share code, notes, and snippets.

@Cylix
Created October 19, 2020 05:46
Show Gist options
  • Select an option

  • Save Cylix/4d57b2f0894340daf675ef68b2c36ff6 to your computer and use it in GitHub Desktop.

Select an option

Save Cylix/4d57b2f0894340daf675ef68b2c36ff6 to your computer and use it in GitHub Desktop.
Reflection in C++14 - Member Function #2
//! our function base class, non-templated.
//! it does really nothing, except helping us to store functions in the same container
class function_base {
public:
virtual ~function_base(void) = default;
};
//! our function class, templated on return and parameters types
template <typename ReturnType, typename... Params>
class function;
template <typename ReturnType, typename... Params>
class function<ReturnType(Params...)> : public function_base {
public:
//! our constructor
//! we simply take an std::function as parameter and store it internally
//! in fact, our function class is just a wrapper of std::function which inherits from function_base, nothing more
function(const std::function<ReturnType(Params...)>& f) : f(f) {}
//! a callable operator to be able to call our function
ReturnType operator()(Params... params) {
return f(params...);
}
private:
std::function<ReturnType(Params...)> f;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment