Using SFINAE to check for function existance

D

dascandy

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).

I've thought about using an interface instead, but that requires all
classes using it to implement that interface; moreover it won't ever
be called virtually. I would like to have it work mostly
"automatically".

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". 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.

Am I missing something? How can I make this work other than using an
interface for matching?
 
A

Alf P. Steinbach

* (e-mail address removed):
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).

I've thought about using an interface instead, but that requires all
classes using it to implement that interface; moreover it won't ever
be called virtually. I would like to have it work mostly
"automatically".

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". 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.

Am I missing something? How can I make this work other than using an
interface for matching?

As I recall the Boost library has functionality for determining whether
a member function exists or not.

Cheers, & hth.,

- Alf
 
K

Kai-Uwe Bux

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SFINAE 4
Selective check box selector 3
SFINAE not applying as expected. 1
iterator_traits and SFINAE 7
SFINAE and explicit instantiation 3
Tips for using Github??? 3
Trying to apply SFINAE 10
Repetitive Function 3

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top