wrote:
> I think it would be possible to use SFINAE or something to detect
> whether a certain function is present or not. I would like to use one
> approach if it has the function (calling it for a result) and another
> when it doesn't exist (calling a substitute function).
Here is an example:
template < typename T >
class has_clone {
/*
stolen from Rani Sharoni, who attributes this to
Richard Smith and also Artem Livshits
With a fix to deal with const and non-const versions.
*/
template < typename S, S* ( S::* ) ( void ) >
struct dummy {};
template < typename S, S* ( S::* ) ( void ) const >
struct dummy_const {};
template < typename S >
static
yes_type check ( dummy< S, &S::clone > * );
template < typename S >
static
yes_type check ( dummy_const< S, &S::clone > * );
template < typename S >
static
no_type check ( ... );
public:
static bool const value = sizeof( check<T>(0) ) == sizeof( yes_type );
}; // has_clone
> I've thought about using an interface instead, but that requires all
> classes using it to implement that interface;
and that would be bad?
> moreover it won't ever be called virtually.
huh? I lost the reference of "it". If "it" still refers to "interface", then
this sentence seems to be false.
> I would like to have it work mostly "automatically".
Again, the reference of "it" seems to be dangling.
> My thoughts were along the lines of "pass the size of a function
> pointer of that function in the respective class, if it doesn't exist
> pass 0 instead".
How do you pass an unsigned int "in the respective class"? I am not
following.
> I can then select based on the sizeof whether it
> supports the function. The problem is, if it has the function, both
> templates match equally well. If it doesn't have it, the first is
> eliminated based on sfinae but the second still matches, so it works
> only if it doesn't exist.
You lost me completely. Could you illustrate your idea in code?
> Am I missing something?
I cannot tell.
> How can I make this work other than using an interface for matching?
If only I knew what "this" refers to ...
Best
Kai-Uwe Bux