Hi, I'm asking for a template trick to detect if a class has a specific member function of a given signature.
The problem is similar to the one cited here http://www.gotw.ca/gotw/071.htm but not the same: in the item of Sutter's book he answered to the question that a class C MUST PROVIDE a member function with a particular signature, else the program won't compile. In my problem I need to do something if a class has that function, else do "something else".
A similar problem was faced by boost::serialization but I don't like the solution they adopted: a template function that invokes by default a free function (that you have to define) with a particular signature unless you define a particular member function (in their case "serialize" that takes 2 parameters of a given type) with a particular signature, else a compile error will happens. That is to implement both intrusive and non-intrusive serialization.
I don't like that solution for two reasons: 1) to be non intrusive you must override the global "serialize" function that is in boost::serialization namespace, so you have IN YOUR CLIENT CODE to open namespace boost and namespace serialization!! And a second, practical reason, is because the stack to resolve that mess was 10 to 12 function invocation... and I'm a game developer.
I need to define a custom behavior for classes that has not that member function, and my entities are inside different namespaces (and I don't want to override a global function defined in one namespace while I'm in another one)
Can you give me an hint to solve this puzzle?
EDIT: @Chris Jester-Young I know well Koenig lookup. In fact I was surprised of what they did in documentation (http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/index.html)
why open the boost::serialization namespace?
but you are not answering to my question: I don't want to do the same thing of boost::serialization. Maybe I decide to do "nothing" if the class has not that function with that signature. in boost::serialization if you don't have that member function OR if you don't override global "serialize" function... compile error! I don't want this.
EDIT: @Tom Leys I'm sorry that's not what I expected as answer. What you suggest me is not what I want. If you read the link to the gotw site (old site of Herb Sutter) you'll discover that your solution is both intrusive (and I don't want) and it doesn't solve the problem with the signature and it doesn't solve the fact that I can accept classes that don't have that member function... It's a bit more tricky.