Templating composed functions

Discussion in 'C++' started by alan, Jul 6, 2008.

  1. alan

    alan Guest

    Hello all, in a project I have I have an abstract base type with some
    virtual functions:

    class Base{
    public:
    virtual Base* t1() =0;
    virtual Base* t2() =0;
    }

    I have a bunch of derived classes, much like the following:

    class Derived : public Base{
    private:
    Base* d1;
    Base* d2;
    public:
    virtual Base* t1(){
    return d1;
    }
    virtual Base* t2(){
    return d2;
    }
    Derived(Base* nd1, Base* nd2)
    : d1(nd1), d2(nd2){};
    }

    Now, I have to use t1() and t2() of an object in several different
    ways. For example, I need something like:

    void t1_one_way(Base* b){
    std::cout << "one way: " << std::hex
    << (size_t) b->t1() << std::endl;
    }

    void t2_one_way(Base* b){
    std::cout << "one way: " << std::hex
    << (size_t) b->t2() << std::endl;
    }

    Or I might have to use t1() and t2() in another way:

    void t1_another_way(Base* b){
    std::cout << "another way: " << std::dec
    << (size_t) b->t1() << std::endl;
    }

    void t2_another_way(Base* b){
    std::cout << "another way: " << std::dec
    << (size_t) b->t2() << std::endl;
    }

    This of course means quite a bit of code duplication. There are maybe
    3 or so ways, but there are about half a dozen t...() member functions
    (and there will probably be more in the future) with the same pattern.

    So I thought of using templates:

    typedef Base* _ff_type(Base*);

    template<_ff_type _ff>
    void one_way(Base* b){
    std::cout << "one way: " << std::hex
    << (size_t) _ff(b) << std::endl;
    }

    void another_way(Base* b){
    std::cout << "another way: " << std::dec
    << (size_t) _ff(b) << std::endl;
    }

    The above works but needs to use global functions for the proper call:

    inline Base* t1(Base* b){return b->t1();}
    inline Base* t2(Base* b){return b->t2();}
    ....
    int main(){
    Derived* foo = Derived(NULL, NULL);
    Derived* bar = Derived(foo,NULL);
    Derived* test = Derived(foo,bar);

    one_way<t1>(test);
    another_way<t1>(test);
    one_way<t2>(test);
    another_way<t2>(test);
    }

    Some questions:

    1. Is the above solution Standard C++ compliant? It compiles and
    appears to work correctly on g++ 4.2.3

    2. Is there a way so that I won't have to declare global t1() and
    t2()? Something like this is acceptable:

    one_way<Base::t1>(test)

    ....as long as I don't have to declare global t1() and t2() (because 1.
    they pollute the namespace, and 2.I might need to add several more
    member functions, and having to write global versions that effectively
    just reflect it gets irritating). I tried Base::t1 like the above but
    it says " invalid use of non-static member function". What should
    _ff_type be?

    Sincerely,
    AmkG ^^
    alan, Jul 6, 2008
    #1
    1. Advertising

  2. alan

    Greg Herlihy Guest

    On Jul 6, 12:29 am, alan <> wrote:
    > Hello all, in a project I have I have an abstract base type with some
    > virtual functions:
    >
    > Now, I have to use t1() and t2() of an object in several different
    > ways.
    > ...
    > This of course means quite a bit of code duplication.  There are maybe
    > 3 or so ways, but there are about half a dozen t...() member functions
    > (and there will probably be more in the future) with the same pattern.
    >
    > So I thought of using templates:
    >
    > typedef Base* _ff_type(Base*);
    >
    > template<_ff_type _ff>
    > void one_way(Base* b){
    >     std::cout << "one way: " << std::hex
    >         << (size_t) _ff(b) << std::endl;
    >
    > }
    >
    > void another_way(Base* b){
    >     std::cout << "another way: " << std::dec
    >         << (size_t) _ff(b) << std::endl;
    >
    > }
    >
    > The above works but needs to use global functions for the proper call:
    >
    > inline Base* t1(Base* b){return b->t1();}
    > inline Base* t2(Base* b){return b->t2();}
    > ...
    > int main(){
    >     Derived* foo = Derived(NULL, NULL);
    >     Derived* bar = Derived(foo,NULL);
    >     Derived* test = Derived(foo,bar);
    >
    >     one_way<t1>(test);
    >     another_way<t1>(test);
    >     one_way<t2>(test);
    >     another_way<t2>(test);
    >
    > }
    >
    > Some questions:
    >
    > 1.  Is the above solution Standard C++ compliant?  It compiles and
    > appears to work correctly on g++ 4.2.3


    I doubt that the main() routine above compiles. But otherwise, the
    function templates look OK to me.

    > 2.  Is there a way so that I won't have to declare global t1()  and
    > t2()?  Something like this is acceptable:
    >
    >   one_way<Base::t1>(test)


    Yes, instead of accepting a function type template parameter,
    one_way() and another_way() should accept a (Base class) member
    function pointer template parameter. For example:

    template <size_t (Base::*MF)()>
    size_t one_way( Base& b)
    {
    return (b.*MF)();
    }

    Now the compiler will accept your acceptable syntax:

    one_way<&Base::t1>(test)

    (don't forget the ampersand however).

    > ...as long as I don't have to declare global t1() and t2() (because 1.
    > they pollute the namespace, and 2.I might need to add several more
    > member functions, and having to write global versions that effectively
    > just reflect it gets irritating).  I tried Base::t1 like the above but
    > it says " invalid use of non-static member function".  What should
    > _ff_type be?


    See above.

    Greg
    Greg Herlihy, Jul 6, 2008
    #2
    1. Advertising

  3. alan

    alan Guest

    On Jul 6, 4:23 pm, Greg Herlihy <> wrote:
    > On Jul 6, 12:29 am, alan <> wrote:
    > > 2.  Is there a way so that I won't have to declare global t1()  and
    > > t2()?  Something like this is acceptable:

    >
    > >   one_way<Base::t1>(test)

    >
    > Yes, instead of accepting a function type template parameter,
    > one_way() and another_way() should accept a (Base class) member
    > function pointer template parameter. For example:
    >
    >      template <size_t (Base::*MF)()>
    >      size_t one_way( Base& b)
    >      {
    >          return (b.*MF)();
    >      }
    >
    > Now the compiler will accept your acceptable syntax:
    >
    >     one_way<&Base::t1>(test)
    >
    > (don't forget the ampersand however).
    >


    Thanks!
    alan, Jul 6, 2008
    #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. iksrazal
    Replies:
    2
    Views:
    303
    Marcin Grunwald
    Mar 8, 2005
  2. Francesc Guim Bernat

    Defining composed keys [XSD]

    Francesc Guim Bernat, Jul 29, 2003, in forum: XML
    Replies:
    0
    Views:
    463
    Francesc Guim Bernat
    Jul 29, 2003
  3. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,647
    Jonathan Bromley
    Jul 5, 2006
  4. the_jos
    Replies:
    5
    Views:
    420
    Joe Kesselman
    Nov 6, 2006
  5. Replies:
    6
    Views:
    334
Loading...

Share This Page