Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction template parameter deduction
    primarykey
    data
    text
    <p>I am writing the small C++ message-based communication framework with using of the Reactor pattern. The problem which I have met is the serialization of the application (user-defined) messages. To prevent API from redudant information I make an assumption about the serialization function. There is an Archive class which holds the serialized form of the message, however it is the template, so the user could choose the binary form of its. The assumption is that for each message there will be only one serialization function available (defined), so the type deduction of the binary type could be cleary deduced from the function signature. Let's see the code:</p> <pre><code>template &lt;typename T&gt; struct Archive { T t; }; // struct Archive template &lt;typename Message, typename T&gt; void serialize(const Message&amp; msg, Archive&lt;T&gt;* const ar); struct Signal { void* payload; }; template &lt;typename T&gt; struct Wrapper { Signal* pack() { Signal* s = new Signal; archive(&amp;serialize, &amp;s-&gt;payload); return s; } template &lt;typename P&gt; void archive(void(*f)(const T&amp;, Archive&lt;P&gt;* const), void** payload) { Archive&lt;P&gt; ar; f(t, &amp;ar); P* p = new P; *p = ar.t; *payload = p; } T t; }; struct TestMsg { int i; }; template &lt;&gt; void serialize(const TestMsg&amp; msg, Archive&lt;int&gt;* const ar) { ar-&gt;t = msg.i; } int main() { Wrapper&lt;TestMsg&gt; msg; msg.pack(); return 0; } </code></pre> <p>Compiler claims that it cannot deduce the P type. Is there any other way (without traits) to help compiler with such deduction?</p> <p>Kind Regard, Gracjan</p> <p>EDIT(14-05-2013 15:42): According to the request in comment I attach the Traits solution:</p> <pre><code>/****** Library part *******/ template &lt;typename T&gt; struct Archive { T t; }; // struct Archive template &lt;typename T&gt; struct MessageTrait {}; template &lt;typename Message, typename T&gt; void serialize(const Message&amp; msg, Archive&lt;T&gt;* const ar); struct Signal { void* payload; }; template &lt;typename T&gt; struct Wrapper { Signal* pack() { typedef Archive&lt;typename MessageTrait&lt;T&gt;::ArchType&gt; ArchiveType; Signal* s = new Signal; ArchiveType ar; serialize(t, &amp;ar); return s; } T t; }; /****** Application part ******/ struct TestMsg { int i; }; template&lt;&gt; struct MessageTrait&lt;TestMsg&gt; { typedef int ArchType; }; template &lt;&gt; void serialize(const TestMsg&amp; msg, Archive&lt;int&gt;* const ar) { ar-&gt;t = msg.i; } int main() { Wrapper&lt;TestMsg&gt; msg; msg.pack(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload