Problem with function selection in templates

Discussion in 'C++' started by Pär Buschka, Nov 6, 2006.

  1. Pär Buschka

    Pär Buschka Guest

    Hi there.

    I have some template functions that operate on different types of object. If
    the object has a certain memberfunction I want to call that function, and if
    not, I want to call a global function that operates on that object.

    Some pseudocode to show what I mean:

    template<class T> void Function(T& MyObject)
    {
    if exist MyObject.Process() then MyObject.Process();
    else GlobalProcess(MyObject);
    }

    is there a good way to do this without having to write a lot of overloaded
    functions for one of the cases?

    Thanks in advance
    Pär Buschka
     
    Pär Buschka, Nov 6, 2006
    #1
    1. Advertising

  2. Pär Buschka wrote:
    > I have some template functions that operate on different types of
    > object. If the object has a certain memberfunction I want to call
    > that function, and if not, I want to call a global function that
    > operates on that object.
    > Some pseudocode to show what I mean:
    >
    > template<class T> void Function(T& MyObject)
    > {
    > if exist MyObject.Process() then MyObject.Process();
    > else GlobalProcess(MyObject);
    > }
    >
    > is there a good way to do this without having to write a lot of
    > overloaded functions for one of the cases?


    template<class T, bool usemember> struct ObjectProcessor;

    template<class T> struct ObjectProcessor<T, true> {
    void operator()(T& MyObject) {
    MyObject.Process();
    }
    };

    template<class T> struct ObjectProcessor<T, false> {
    void operator()(T& MyObject) {
    GlobalProcess(MyObject);
    }
    };

    template<class T> void Function(T& MyObject)
    {
    ObjectProcessor<T, has_member< ??? > >()(MyObject);
    }

    Look up a suitable implementation of "has_member" on the Web.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 6, 2006
    #2
    1. Advertising

  3. Pär Buschka

    Pär Buschka Guest

    Thank you very much for the help, it seems to work perfect.

    A suitable has_member function can for instance be found at:
    http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.vc/2004-10/0007.html

    /Pär

    "Victor Bazarov" <> skrev i meddelandet
    news:eio2l3$np0$...
    > Pär Buschka wrote:
    >> I have some template functions that operate on different types of
    >> object. If the object has a certain memberfunction I want to call
    >> that function, and if not, I want to call a global function that
    >> operates on that object.
    >> Some pseudocode to show what I mean:
    >>
    >> template<class T> void Function(T& MyObject)
    >> {
    >> if exist MyObject.Process() then MyObject.Process();
    >> else GlobalProcess(MyObject);
    >> }
    >>
    >> is there a good way to do this without having to write a lot of
    >> overloaded functions for one of the cases?

    >
    > template<class T, bool usemember> struct ObjectProcessor;
    >
    > template<class T> struct ObjectProcessor<T, true> {
    > void operator()(T& MyObject) {
    > MyObject.Process();
    > }
    > };
    >
    > template<class T> struct ObjectProcessor<T, false> {
    > void operator()(T& MyObject) {
    > GlobalProcess(MyObject);
    > }
    > };
    >
    > template<class T> void Function(T& MyObject)
    > {
    > ObjectProcessor<T, has_member< ??? > >()(MyObject);
    > }
    >
    > Look up a suitable implementation of "has_member" on the Web.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    >
     
    Pär Buschka, Nov 7, 2006
    #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. Simon Niederberger
    Replies:
    2
    Views:
    16,529
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,472
    Andrew Crowe
    Sep 13, 2004
  3. JKop
    Replies:
    3
    Views:
    477
  4. BigMan
    Replies:
    1
    Views:
    438
  5. recover
    Replies:
    2
    Views:
    809
    recover
    Jul 25, 2006
Loading...

Share This Page