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).

live demo


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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -