is there a way to test at compile time, whether the template parameter type implements some function (without forcing the type to provide a custom constant/typedef?)
imi's solution does not force inheritance of an interface and calling of a virtual function -- thus
it is more loosely coupled.
It is complex only in the sense that templates (and SFINAE for that matter) are extremely powerful
but the language syntax is not rich enough to ask such simple questions of a type as "do you have a method named X" or "do you have a virtual destructor".
The result is often necessarily obtuse syntax to accomplish these simple tasks, but the underlying implementation is not just simple -- it's non-existent: basically nothing imi wrote will generate any code whatsoever above the call to FooOrNot().
I also recall seeing another implementation of the question "are you ostreamable" which was
also obtuse and was the only use I've ever seen of implementing the comma operator.
darkestfright, if you meant inheritance from an abstract base class that has a pure virtual function "Foo", then everything jsmith said applies, although I wouldn't care much about the performance aspects if my application would be the standard-stock-IO-bound-database-application.. ;-)
However, I am in an inner loop of a time-critical function and a virtual call plus the dereferencing of the pointer to the struct would cost me like 10% performance..
If you had the non-virtual solution like this in mind:
Then the problem is, that now ToFooOrNotToFoo has to be aware of "HasFoo", whereas I want a generic library possibility where the library does not know anything about it's (template) arguments.
PS: Another disadvantage of this template thingie is, the error messages are uttermost crap. I'll have a look at Boost::concept libarary. Maybe they get compilers to generate better error messages.
While this concept does seem extremely strange to me, I'll admit that I am totally intrigued by it. I've always operated under the assumption that if I'm trying to use a function, and it isn't there..there should be an error, because my program won't work the way I intend it, and if something like this came up I could just specialize it myself. I've also never had to work in a time-critical environment, or environments where non-copy dereferencing is considered an expensive operation.
Thanks for exposing me to this concept, I will definitely look into it in depth in the near future. Stuff like this is why I got into Computer Science in the first place.