c++ - Function as an argument of a constructor of std::function wrapper -
i writing monitor class synchronization problem , implement 'entry' class wrap std::function.
i implemented bit, used function traits, right able construct entry objects using prepared std::function object. attempts write constructor has plain function parameter failed compiler messages template argument deduction/substitution , <anonymous> parameter.
the program working curious how implement given constructor, code:
template <class f> struct functiontype; template <class r, class object, class... args> struct functiontype<r (object::*)(args...)> { typedef r return_type; }; template <class r, class object, class... args> struct functiontype<r (object::*)(args...) const> { typedef r return_type; }; template <class f> class entry { std::function<f> internalfunction; ... public: template <f> entry(const f& function){ // doesn't work. } template <f> entry(const std::function<f> function) : internalfunction(function) { } template<f, class... arguments> typename functiontype<f>::return_type operator()(arguments... arguments){ return internalfunction(arguments...); } };
a couple of things:
template<f>
doesn't make sense @ all. type of f
template parameter on class, use , remove altogether.
next, it's easier use trailing return type on operator()
function:
template<class... arguments> auto operator()(arguments... arguments) -> decltype(internalfunction(arguments...)) { return internalfunction(arguments...); }
(if have c++14 can use auto
).
here's fixed class
template <class f> class entry { std::function<f> internalfunction; public: entry(const f& function){ // doesn't work. } entry(const std::function<f> function) : internalfunction(function) { } template<class... arguments> auto operator()(arguments... arguments) -> decltype(internalfunction(arguments...)){ return internalfunction(arguments...); } };
Comments
Post a Comment