473,769 Members | 5,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ty pe _ff>
void one_way(Base* b){
std::cout << "one way: " << std::hex
<< (size_t) _ff(b) << std::endl;
}

void another_way(Bas e* 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,NUL L);
Derived* test = Derived(foo,bar );

one_way<t1>(tes t);
another_way<t1> (test);
one_way<t2>(tes t);
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::t 1>(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 1142
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_ty pe _ff>
void one_way(Base* b){
* * std::cout << "one way: " << std::hex
* * * * << (size_t) _ff(b) << std::endl;

}

void another_way(Bas e* 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,NUL L);
* * Derived* test = Derived(foo,bar );

* * one_way<t1>(tes t);
* * another_way<t1> (test);
* * one_way<t2>(tes t);
* * 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::t 1>(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.com wrote:
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::t 1>(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
2095
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 for really ugly code. Would a templating engine solve that? Does anyone have any suggestions about which one to use?
7
1261
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 XML/HTML) I don't need a framework (already have it), but just simple templating. The syntax I had in mind is something like that:
2
1578
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% or <MYusername/> would be replaced by the value. basically what the perl cookbook uses in a few examples iirc. however more and more we have to deal with more complex data. variable length lists of data. say for example a list of new products...
1
1282
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 can edit HTML files without having to get involved with VS at all. http://www.codeproject.com/aspnet/page_templates.asp - this seems to be the best so far, if I can modify the code to use an external template file. Are there any other...
18
4974
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 overwhelmed by the plethora of different frameworks, templating engines, HTML generation tools etc that exist. After some thought I decided to leave the various frameworks aside for the
0
944
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 generating textual output. The key features are:
7
1623
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 doubt I am using it to its full extent but I want to learn more about, and compare and contrast, frameworks versus IDE versus templating systems and MOST IMPORTANTLY learn what is going to save me time. I know technically the IDE is the tool in...
5
2327
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 pursue. I don't need MVC at the moment, but I don't know that the templating engines require it either. Thank you.
10
1720
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 constructs with the HTML document itself. An opposite approach to this form of dynamic HTML production is called push-style templating, as coined by Terence Parr:
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.