473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Templating composed functions

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 ^^
Jul 6 '08 #1
2 1121
On Jul 6, 12:29*am, alan <almkg...@gmail.comwrote:
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

Jul 6 '08 #2
On Jul 6, 4:23*pm, Greg Herlihy <gre...@mac.comwrote:
On Jul 6, 12:29*am, alan <almkg...@gmail.comwrote:
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!
Jul 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Leif K-Brooks | last post by:
I'm planning to start on a fairly large web application, most likely using mod_python. I recently wrote a fairly small (but real-world useful) web app with it, and all of those req.write()s made...
7
by: Ksenia Marasanova | last post by:
Hi, I am looking for fast, simple templating system that will allow me to do the following: - embed Python code (or some templating code) in the template - generate text output (not only...
2
by: lloyd christopher | last post by:
im developing a template based web application in perl and need some guidance. currently its all pretty simple stuff, we tell the designers place holders to use and then &username; or %USERNAME%...
1
by: Chris Ashley | last post by:
I have been looking into templating systems these past few days, but nothing seems to suit my purpose. I want to achieve complete separation of code and design in a templating system, so a designer...
18
by: pkassianidis | last post by:
Hello everybody, I am in the process of writing my very first web application in Python, and I need a way to generate dynamic HTML pages with data from a database. I have to say I am...
0
by: Chris Withers | last post by:
Hi All, I'm proud to announce that after almost a year's development, the first public release of Twiddler is available! Twiddler is a simple but flexible templating system for dynamically...
7
by: Paul | last post by:
I have been coding apps with PHP for several years. But I am getting more involved in larger and more complicated PHP applications and want to use best practices. I use Eclipse's PHP IDE. I...
5
by: jmDesktop | last post by:
Hi, I was using .net and it uses templates. I like that it did not mix the UI with logic. I saw there are many templating engines for php usage. Can you recommend one? I don' t know which to...
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.