Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using SFINAE to check for function existance

Reply
Thread Tools

Using SFINAE to check for function existance

 
 
dascandy@gmail.com
Guest
Posts: n/a
 
      10-23-2007
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?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-23-2007
* :
> 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-23-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check Existance of Bean in Struts google khan Java 1 11-30-2006 07:29 AM
Check existance of a querystring variable Imran Aziz ASP .Net 2 08-09-2005 10:51 AM
check Existance of a file on different machine Ajit ASP .Net 5 09-26-2003 10:59 AM
check Existance of a file on different machine Ajit ASP .Net 5 09-26-2003 10:59 AM
Run-time check for template function existance Patrick Kowalzick C++ 1 08-27-2003 02:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57