Templating composed functions

A

alan

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 ^^
 
G

Greg Herlihy

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
 
A

alan

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!
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top