<br><font size=2 color=#800080 face="sans-serif">&gt; Chris Fairles&quot;
&lt;chris.fairles@gmail.com&gt; on Fri, 20 Jul 2007 18:03:18 -0400</font>
<br><tt><font size=2>&gt; How can you determine (compile-time) that a class
has a serialize function?<br>
&gt; <br>
&gt; struct foo {<br>
&gt; template &lt;typename Archive&gt;<br>
&gt; void serialize (Archive &amp;, const unsigned int);<br>
&gt; };<br>
&gt; <br>
&gt; &quot;&amp;foo::serialize&quot; isn't a type and I can't figure out
a way to make it<br>
&gt; a type (besides a typedef in the class) and also a constant<br>
&gt; expression.<br>
&gt; <br>
&gt; Tried to play around with this as well but theres no substitution<br>
&gt; failure here and static functions can't be constant expressions<br>
&gt; (commonnnn c++0x!!)<br>
&gt; <br>
&gt; struct serialize {<br>
&gt; &nbsp; &nbsp; template &lt;typename T&gt;<br>
&gt; &nbsp; &nbsp; static void value(T const&amp;) { void(T::*f)(archive::xml_oarchive
&amp;,<br>
&gt; const unsigned int) = &amp;T::template serialize&lt;archive::xml_oarchive&gt;;<br>
&gt; (void)f; }<br>
&gt; };<br>
&gt; <br>
&gt; Basically, in the code below I want to see &quot;GoodGood&quot; printed
out and<br>
&gt; the only thing I can change is Q.<br>
&gt; <br>
&gt; struct A{<br>
&gt; &nbsp; void f(int,int){}<br>
&gt; };<br>
&gt; struct B{};<br>
&gt; <br>
&gt; template &lt;class T,class F=void&gt;<br>
&gt; struct Q{enum{value=false};};<br>
&gt; <br>
&gt; template&lt;class T&gt; struct Q&lt;T,void(T::serialize)(int,int)&gt;{enum{value=true};};<br>
&gt; <br>
&gt; int main(){<br>
&gt; &nbsp; if(Q&lt;A&gt;::value) { cout &lt;&lt; &quot;Good&quot;;}<br>
&gt; &nbsp; if(!Q&lt;B&gt;::value){ cout &lt;&lt; &quot;Good&quot;;}<br>
&gt; }<br>
&gt; <br>
&gt; Help/hints/remotely possible?</font></tt>
<br>
<br><font size=2 face="sans-serif">You can try something like this.</font>
<br>
<br><font size=2 color=blue>#include</font><font size=2> </font><font size=2 color=#a11f12>&lt;iostream&gt;</font>
<br><font size=2 color=blue>#include</font><font size=2> </font><font size=2 color=#a11f12>&lt;iomanip&gt;</font>
<br>
<br><font size=2 color=blue>typedef</font><font size=2> </font><font size=2 color=blue>char</font><font size=2>
yes_type;</font>
<br>
<br><font size=2 color=blue>struct</font><font size=2> no_type</font>
<br><font size=2>{</font>
<br><font size=2>&nbsp; &nbsp;</font><font size=2 color=blue>char</font><font size=2>
padding[8];</font>
<br><font size=2>};</font>
<br>
<br><font size=2 color=blue>template</font><font size=2> &lt;</font><font size=2 color=blue>class</font><font size=2>
T&gt;</font>
<br><font size=2 color=blue>struct</font><font size=2> HasSerializeMember</font>
<br><font size=2>{</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>typedef</font><font size=2>
</font><font size=2 color=blue>void</font><font size=2> (T::*SerializationFun)(</font><font size=2 color=blue>int</font><font size=2>
&amp;, </font><font size=2 color=blue>unsigned</font><font size=2>);</font>
<br>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>template</font><font size=2>
&lt;SerializationFun&gt;</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>struct</font><font size=2>
A {};</font>
<br>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>template</font><font size=2>
&lt;</font><font size=2 color=blue>class</font><font size=2> Q&gt;</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>static</font><font size=2>
yes_type Test(A&lt;&amp;Q::serialize&gt;*);</font>
<br>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>template</font><font size=2>
&lt;</font><font size=2 color=blue>class</font><font size=2> Q&gt;</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>static</font><font size=2>
no_type Test(...);</font>
<br>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>static</font><font size=2>
</font><font size=2 color=blue>const</font><font size=2> </font><font size=2 color=blue>bool</font><font size=2>
value =</font>
<br><font size=2>&nbsp; &nbsp; &nbsp; &nbsp; </font><font size=2 color=blue>sizeof</font><font size=2>(Test&lt;T&gt;(0))
== </font><font size=2 color=blue>sizeof</font><font size=2>(yes_type);</font>
<br><font size=2>};</font>
<br>
<br><font size=2 color=blue>struct</font><font size=2> A</font>
<br><font size=2>{</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>template</font><font size=2>
&lt;</font><font size=2 color=blue>class</font><font size=2> Stream&gt;</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>void</font><font size=2>
serialize(Stream &amp; strm, </font><font size=2 color=blue>unsigned</font><font size=2>
version) {}</font>
<br><font size=2>};</font>
<br>
<br><font size=2 color=blue>struct</font><font size=2> B</font>
<br><font size=2>{</font>
<br><font size=2>};</font>
<br>
<br><font size=2 color=blue>struct</font><font size=2> C</font>
<br><font size=2>{</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>void</font><font size=2>
serialize() {}</font>
<br><font size=2>};</font>
<br>
<br><font size=2 color=blue>int</font><font size=2> main()</font>
<br><font size=2>{</font>
<br><font size=2>&nbsp; &nbsp; </font><font size=2 color=blue>using</font><font size=2>
</font><font size=2 color=blue>namespace</font><font size=2> std;</font>
<br><font size=2>&nbsp; &nbsp; cout</font>
<br><font size=2>&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; boolalpha</font>
<br><font size=2>&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; HasSerializeMember&lt;A&gt;::value
&lt;&lt; </font><font size=2 color=#a11f12>'\n'</font><font size=2> &nbsp;</font><font size=2 color=#008000>//
true</font>
<br><font size=2>&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; HasSerializeMember&lt;B&gt;::value
&lt;&lt; </font><font size=2 color=#a11f12>'\n'</font><font size=2> &nbsp;</font><font size=2 color=#008000>//
false</font>
<br><font size=2>&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt; HasSerializeMember&lt;C&gt;::value
&lt;&lt; endl; </font><font size=2 color=#008000>// false</font>
<br><font size=2>}</font>
<br>
<br><font size=2 face="sans-serif">Regards,<br>
Roman Perepelitsa<br>
<br>
Deutsche Bank Moscow<br>
+7 (495) 660-74-08<br>
</font>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">---<br>
<br>
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.<br>
<br>
Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.</span><br>