I want to write C++ class (let type name will be Adapter), which accepts function pointers or functional objects (especially, lambda functions) in constructor and stores it's by value internally (as std::function for exampl). After the class been constructed, it represents functional object too and acts just as wrapper for internal functional object. But with following conditions:
- return type for wrapper-class (Adapter) is always predefined (let it be ReturnType);
- but internal functor may have no return value -- in this case some DefaultValue substituted;
- wrapper class always receives some ArgType as argument to operator();
- but internal function may receive ArgType as argument too, or may not accept arguments at all -- in latter case it just should be called without arguments.
First question -- it's possible at all with C++11, and second quastion how can I do it? I have no idea where should I start.
I need deduce (in template) return value of functional object? First pitfall, is that std::result_of doesn't support function pointers. And I have no idea how can I deduce argument type (it may be empty, no arguments, or ArgType) for functional object (I know how it is possible for function pointers, via templated structure, but how can I do it for functional object?).
I need something like this:
template <typename Functor?> class Adapter
{
private:
std::function<Void_or_ReturnType (Void_or_ArgType)> f;
public:
template <typename Functor?> Adapter(const Functor& f) { ??? }
ReturnType operator()(ArgType arg)
{
// one of the following four variants:
return f(arg);
f(arg); return DefaultValue;
return f();
f(); return DefaultValue;
}
};
May be I need to deduce Void_or_ReturnType and Void_or_ArgType types in template and write different template specialization for operator() for each of four cases. But how exactly can I do this?
Aucun commentaire:
Enregistrer un commentaire