<br><font size=2 color=#800080 face="sans-serif">> Chris Fairles" <chris.fairles@gmail.com> on Fri, 20 Jul 2007 18:03:18 -0400</font> <br><tt><font size=2>> How can you determine (compile-time) that a class has a serialize function?<br> > <br> > struct foo {<br> > template <typename Archive><br> > void serialize (Archive &, const unsigned int);<br> > };<br> > <br> > "&foo::serialize" isn't a type and I can't figure out a way to make it<br> > a type (besides a typedef in the class) and also a constant<br> > expression.<br> > <br> > Tried to play around with this as well but theres no substitution<br> > failure here and static functions can't be constant expressions<br> > (commonnnn c++0x!!)<br> > <br> > struct serialize {<br> > template <typename T><br> > static void value(T const&) { void(T::*f)(archive::xml_oarchive &,<br> > const unsigned int) = &T::template serialize<archive::xml_oarchive>;<br> > (void)f; }<br> > };<br> > <br> > Basically, in the code below I want to see "GoodGood" printed out and<br> > the only thing I can change is Q.<br> > <br> > struct A{<br> > void f(int,int){}<br> > };<br> > struct B{};<br> > <br> > template <class T,class F=void><br> > struct Q{enum{value=false};};<br> > <br> > template<class T> struct Q<T,void(T::serialize)(int,int)>{enum{value=true};};<br> > <br> > int main(){<br> > if(Q<A>::value) { cout << "Good";}<br> > if(!Q<B>::value){ cout << "Good";}<br> > }<br> > <br> > 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><iostream></font> <br><font size=2 color=blue>#include</font><font size=2> </font><font size=2 color=#a11f12><iomanip></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> </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> <</font><font size=2 color=blue>class</font><font size=2> T></font> <br><font size=2 color=blue>struct</font><font size=2> HasSerializeMember</font> <br><font size=2>{</font> <br><font size=2> </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> &, </font><font size=2 color=blue>unsigned</font><font size=2>);</font> <br> <br><font size=2> </font><font size=2 color=blue>template</font><font size=2> <SerializationFun></font> <br><font size=2> </font><font size=2 color=blue>struct</font><font size=2> A {};</font> <br> <br><font size=2> </font><font size=2 color=blue>template</font><font size=2> <</font><font size=2 color=blue>class</font><font size=2> Q></font> <br><font size=2> </font><font size=2 color=blue>static</font><font size=2> yes_type Test(A<&Q::serialize>*);</font> <br> <br><font size=2> </font><font size=2 color=blue>template</font><font size=2> <</font><font size=2 color=blue>class</font><font size=2> Q></font> <br><font size=2> </font><font size=2 color=blue>static</font><font size=2> no_type Test(...);</font> <br> <br><font size=2> </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> </font><font size=2 color=blue>sizeof</font><font size=2>(Test<T>(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> </font><font size=2 color=blue>template</font><font size=2> <</font><font size=2 color=blue>class</font><font size=2> Stream></font> <br><font size=2> </font><font size=2 color=blue>void</font><font size=2> serialize(Stream & 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> </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> </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> cout</font> <br><font size=2> << boolalpha</font> <br><font size=2> << HasSerializeMember<A>::value << </font><font size=2 color=#a11f12>'\n'</font><font size=2> </font><font size=2 color=#008000>// true</font> <br><font size=2> << HasSerializeMember<B>::value << </font><font size=2 color=#a11f12>'\n'</font><font size=2> </font><font size=2 color=#008000>// false</font> <br><font size=2> << HasSerializeMember<C>::value << 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>