c++ - Function template parameter deduction -


i writing small c++ message-based communication framework using of reactor pattern. problem have met serialization of application (user-defined) messages. prevent api redudant information make assumption serialization function. there archive class holds serialized form of message, template, user choose binary form of its. assumption each message there 1 serialization function available (defined), type deduction of binary type cleary deduced function signature. let's see code:

template <typename t> struct archive {   t t; };  // struct archive  template <typename message, typename t> void serialize(const message& msg, archive<t>* const ar);  struct signal {   void* payload; };  template <typename t> struct wrapper {   signal* pack() {     signal* s = new signal;     archive(&serialize, &s->payload);     return s;   }    template <typename p>   void archive(void(*f)(const t&, archive<p>* const), void** payload) {     archive<p> ar;     f(t, &ar);     p* p = new p;     *p = ar.t;     *payload = p;   }    t t; };  struct testmsg {   int i; };  template <> void serialize(const testmsg& msg, archive<int>* const ar) {   ar->t = msg.i; }  int main() {   wrapper<testmsg> msg;   msg.pack();   return 0; } 

compiler claims cannot deduce p type. there other way (without traits) compiler such deduction?

kind regard, gracjan

edit(14-05-2013 15:42): according request in comment attach traits solution:

/****** library part *******/ template <typename t> struct archive {   t t; };  // struct archive  template <typename t> struct messagetrait {};  template <typename message, typename t> void serialize(const message& msg, archive<t>* const ar);  struct signal {   void* payload; };  template <typename t> struct wrapper {   signal* pack() {     typedef archive<typename messagetrait<t>::archtype> archivetype;     signal* s = new signal;     archivetype ar;     serialize(t, &ar);     return s;   }    t t; };  /****** application part ******/ struct testmsg {   int i; };  template<> struct messagetrait<testmsg> {   typedef int archtype; };  template <> void serialize(const testmsg& msg, archive<int>* const ar) {   ar->t = msg.i; }  int main() {   wrapper<testmsg> msg;   msg.pack();   return 0; } 

template <typename message, typename t> void serialize(const message& msg, archive<t>* const ar);  template <typenane t> signal* wrapper<t>::pack() {     signal* s = new signal;     archive(&serialize, &s->payload);     return s; } 

in above code, serialize name of template, , used in place of function represents complete set of overloads result possible specializations of template (i.e. every possible substitution of template arguments). @ same time archive template can potentially take function subset comply minimal restriction second argument instantiation of archive template.

the problem here not template cannot deduce argument, there infinite types match requirements, problem open ended. brings next question, need template?

in general bad idea specialize function templates, serialize template overload? can reduce generality of problem situation deducible or not there 1 or 2 candidates? have feeling opening many degrees of liberty might or not need , getting bit fact anything can match.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -