vendredi 6 mars 2015

C++11 variadic template function storage

I am currently reworking my callback system to C++11 variadic templates. Basically what I am trying to do is to store a function with a return value and any number of arguments.


Afterwards this function should be called and parameters are loaded from a boost::archive and return value should be written back to another archive.



class FuncStore {
public:
virtual ~FuncStore() {}
virtual void call(iarchive& in, oarchive& out) = 0;
protected:
template <class T> T read(iarchive& in) { T a; in >> a; return a; }
};

template<class Interface, class Return, class... Args>
class FuncStoreRetN : public FuncStore {
public:
typedef Return(Interface::*Function)(Args&&... args);
typedef Return ReturnType;
static const int ArgCount = sizeof...(Args);
private:
struct pass {
Return res;
pass(Interface* me, Function func, Args... args) {
res = me->func(args...);
}
};

Interface* mMe;
Function mFunc;
public:
FuncStoreRetN(Interface* me, Function func) : mMe(me), mFunc(func) {}
void call(iarchive& in, oarchive& out) {
pass c{mMe, mFunc, read<Args>(in)...};
out << c.res;
}
};


From my understanding pass c{mMe, mFunc, read<Args>(in)...}; should expand to pass c{mMe, mFunc, read<int>(in), read<string>(in), ...}; am I right?


But I cant compile with g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2



error: expected primary-expression before ‘>’ token
pass c{mMe, mFunc, read<Args>(in)...};
^


Any suggestions?


Aucun commentaire:

Enregistrer un commentaire