473,386 Members | 1,602 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,386 software developers and data experts.

Pass code as template parameter

Hi,

I need to write many instances like the following struct

struct Function191 : public Function {
double operator()(double x) {
return cos(x) - x;
}
friend std::ostream& operator<< (std::ostream& os, const Function191&
f) {
return os << "cos(x) - x";
}
};

Is there a way to generalize this, such that I can write a generic struct,
which takes a parameter which is both interpreted as code for
operator() and as string for operator<<? I was hoping it would be possible
with templates for example:
Func<"cos(x) - x"f1;
Func<"exp(x) - log(x)"f2;

Is this possible?

Best,
Edward
Nov 6 '08 #1
2 1907
On Nov 6, 1:37*pm, "Edward Jensen" <edw...@jensen.invalidwrote:
Hi,

I need to write many instances like the following struct

struct Function191 : public Function {
* * *double operator()(double x) {
* * * * *return cos(x) - x;
* * *}
* * *friend std::ostream& operator<< (std::ostream& os, const Function191&
f) {
* * * * *return os << "cos(x) - x";
* * *}

};

Is there a way to generalize this, such that I can write a generic struct,
which takes a parameter which is both interpreted as code for
operator() and as string for operator<<? I was hoping it would be possible
with templates for example:
Func<"cos(x) - x"f1;
Func<"exp(x) - log(x)"f2;
The preprocessor has two features:

- ## characters concatenate
- # character stringifies

First: macros are known to be messy.

Here is an example

#include <iostream>

struct Function
{};

#define DEFINE_FUNCTION(N,
code) \
struct Function##N : public Function
{ \
double operator()(double x)
{ \
return
(code); \
}
\
friend std::ostream& operator<< (std::ostream& os, const
Function##N&) { \
return os <<
#code; \
}
\
}

DEFINE_FUNCTION(191, x + 1);
DEFINE_FUNCTION(192, x / 2);

int main()
{
Function191 f191;
f191(5);
std::cout << f191 <<'\n';

Function192 f192;
f192(6);
std::cout << f192 << '\n';
}

Ali
Nov 6 '08 #2
"Edward Jensen" <ed****@jensen.invalidkirjutas:
>
Is there a way to generalize this, such that I can write a generic
struct, which takes a parameter which is both interpreted as code for
operator() and as string for operator<<?
This seems like a task for a preprocessor macro. Introspective support in
C++ is almost non-existant, so one cannot do this kind of things in the
language itself.

hth
Paavo


Nov 6 '08 #3

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

Similar topics

0
by: Mikael Petterson | last post by:
Hi, I am trying to produce the following in javacode; public void action<An actionName>(String value1, int value 2....,Coordinator c) throws Exception; The arguments to the actionXXX could...
1
by: Biff Malone | last post by:
I'm creating an element at run time with the following code <xsl:variable name="myElement"> <xsl:element name="abc"> <xsl:value-of select="." /> </xsl:element> </xsl:variable> ...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: darrel | last post by:
I'm still struggling to figure out the big-picture concept of how to go about building a web site menu from an xml file. The XML file contains the full site structure (the site map, so-to-speak)....
0
by: Padgett | last post by:
Hi There, I'm trying to do an XML / XLS transform but am having trouble retrieving my data. The trouble seems to be with the parameter - if I hard code the id in the template SQL, everything...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
8
by: Leo jay | last post by:
dear all, i want to write log on disk file when macro NEED_DO_LOG is defined. otherwise, do nothing. so i wrote code like this: #ifdef NEED_DO_LOG fstream fsLogFile (...); #else CDummyStream...
1
Dormilich
by: Dormilich | last post by:
Hi, I'm running into a problem with the <xsl:processing-instruction> element (Sablotron). purpose: I generate a xhtml fragment from a xml file via xslt. This is printed (echo) to the output...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.