Using SFINAE to check for function existance

Discussion in 'C++' started by dascandy@gmail.com, Oct 23, 2007.

  1. Guest

    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?
     
    , Oct 23, 2007
    #1
    1. Advertising

  2. * :
    > 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?
     
    Alf P. Steinbach, Oct 23, 2007
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Oct 23, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajit
    Replies:
    5
    Views:
    1,039
    Nicolas
    Sep 26, 2003
  2. Imran Aziz

    Check existance of a querystring variable

    Imran Aziz, Aug 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    30,568
    Imran Aziz
    Aug 9, 2005
  3. Patrick Kowalzick
    Replies:
    1
    Views:
    327
    Chris Theis
    Aug 27, 2003
  4. google khan

    Check Existance of Bean in Struts

    google khan, Nov 30, 2006, in forum: Java
    Replies:
    1
    Views:
    780
    Manish Pandit
    Nov 30, 2006
  5. Dan
    Replies:
    15
    Views:
    639
    Patrice
    Jul 9, 2007
Loading...

Share This Page