Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function pointer to built in operators 1

Status
Not open for further replies.

NullTerminator

Programmer
Oct 13, 1998
293
0
0
US
In other words, can I take the address of < , <=, etc

Or would I have to do something with stl

&std::eek:perator<double> <(double, double)

Eveentually I want to set up a map like

typedef (*opPtr)(double, double);

std::map< const char*, opPtr > operatorMap;

operatorMap["LE"] = &opertor<=(double, double);

Jeb
\0
 
Predefined (built-in) operator is not a function so you can't take an address of (a pointer to) this artifact (not a function, not a var).

You can't overload these operators for basic types: overloaded operator function must have at least one formal parameter of class or enumeration type.
 
Thanks, I kind of thoought that was he case.

I also tried something like
Code:
#include <algorithm>
#include <map>

bool (*gt)(double,double) = std::greater<double>::operator();

std::map<const char*, bool (*)(double,double)> myMap;

myMap.insert(make_pair((const char*)"GT", gt);

but I couldn't get the syntax right after several tries
Any ideas?

tnx
Jeb
\0
 
What a problem? Make wrappers for doubles comparison:
Code:
bool gt(double x, double y) { return x > y; }
bool lt(double x, double y) { return x < y; }
// etc...
Now you have ordinal non-member functions, declare pointers to them:
Code:
typedef bool (*Cmpf)(double,double);
Make a proper map:
Code:
typedef map<const char*,Cmpf> Mapf;
...
Mapf m;
m.insert("gt",&gt);
m.insert("lt",&lt);
...
... m["gt"](1,2) // 1 > 2?
I don't think it's an effective (and safety) way to go (for anything). But what's your true problem behind the scene?
 
I'm sorry, must be
Code:
m.insert(Mapf::value_type("gt",&gt));
m.insert(Mapf::value_type("lt",&lt));
of course...
 
Thanks ArkM,
I implemented a very similar solution with the simpler wrappers.

Behind the scenes I have rules coming from a file like

"Parm1", "GT", Val2,
and
"Parm3", "NE", Val4,
and
"Parm8", "LE", Val9,
and
"ParmAA", "< ", ValZZ,
and
AdjustmentToApplyIfRulesPassed....

Based on The descriptor Parm.. I have to find the corresponding variable in my program and test it using the operator specified against the value provided in the rule.

If all the tests pass, apply the adjustments

ps In my example I should have included <functional> instead of <algorithm>

Tnx
Jeb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top