473,320 Members | 2,202 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,320 software developers and data experts.

STL map containing pointer_to_binary_function

Hi All,

I am beating my head against the wall trying to figure out how to make
this work.

I have a map which contains a string as the key and I want the data to
be a pointer_to_binary_function, but at the time of map definition, I
don't know the types. So really the map contains a templatized
pointer_to_binary_function. When I actually add to the map is where I
know the types of the binary function.

Adding to the map:

mMap.insert("test", ptr_fun(not_equal_to<int>));

Fails with: error: expected promary-expression before ')' token

I am at a loss here.

Thanks

Glenn

Aug 28 '07 #1
10 1768
Gl******@gmail.com wrote:
I am beating my head against the wall trying to figure out how to make
this work.

I have a map which contains a string as the key and I want the data to
be a pointer_to_binary_function, but at the time of map definition, I
don't know the types.
Then you don't have an object. There is no way around it, if you want
to define an object, you need to give it a concrete type.
So really the map contains a templatized
pointer_to_binary_function.
That's nonsense, sorry.
When I actually add to the map is where I
know the types of the binary function.
If you know it when you want to add, you should be able to know it
when you construct your map. It all happens while you're coding.
Adding to the map:

mMap.insert("test", ptr_fun(not_equal_to<int>));

Fails with: error: expected promary-expression before ')' token

I am at a loss here.
One like of code is *not enough* to understand how to get you back on
track. Please explain the problem you're trying to solve.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #2
Gl******@gmail.com wrote:
Hi All,

I am beating my head against the wall trying to figure out how to make
this work.

I have a map which contains a string as the key and I want the data to
be a pointer_to_binary_function, but at the time of map definition, I
don't know the types. So really the map contains a templatized
pointer_to_binary_function. When I actually add to the map is where I
know the types of the binary function.

Adding to the map:

mMap.insert("test", ptr_fun(not_equal_to<int>));
ptr_fun adapts a free function, but not_equal_to is template functor class
>
Fails with: error: expected promary-expression before ')' token

I am at a loss here.

Thanks

Glenn

--
Thanks
Barry
Aug 29 '07 #3
Thanks for the info.

Let me clearify my goal, I want a map to contain a string as the key
and a function pointer template as the data. Meaning, I want to insert
into the map the following things:

map["test"] = equal_to<string>; // or pointers to the
binary_function....
map["test1"] = equal_to<int>;
map["test2"] = less<double>;

etc...

I know the base template for all those functors is the binary_function
template.

Is it possible to have a map where the data is a binary_function
template?

Thanks

Glenn

On Aug 29, 2:58 am, Barry <dh...@126.comwrote:
Glenn...@gmail.com wrote:
Hi All,
I am beating my head against the wall trying to figure out how to make
this work.
I have a map which contains a string as the key and I want the data to
be a pointer_to_binary_function, but at the time of map definition, I
don't know the types. So really the map contains a templatized
pointer_to_binary_function. When I actually add to the map is where I
know the types of the binary function.
Adding to the map:
mMap.insert("test", ptr_fun(not_equal_to<int>));

ptr_fun adapts a free function, but not_equal_to is template functor class
Fails with: error: expected promary-expression before ')' token
I am at a loss here.
Thanks
Glenn

--
Thanks
Barry

Aug 29 '07 #4
Gl******@gmail.com wrote:
Thanks for the info.

Let me clearify my goal, I want a map to contain a string as the key
and a function pointer template as the data. Meaning, I want to insert
into the map the following things:

map["test"] = equal_to<string>; // or pointers to the
binary_function....
map["test1"] = equal_to<int>;
map["test2"] = less<double>;

etc...

I know the base template for all those functors is the binary_function
template.

Is it possible to have a map where the data is a binary_function
template?

Thanks
You can't do that, because

1.
std::binary_function<ArgumentType, boolare various types if
ArgumentType varies, then the pair type used for map is dependent on
ArgumentType.

2.
less<Tor greater<Tinherits from binary_function, which is not
virtual a class type, operator() is not virtual. so there's no
conversion between them (less<T<==binary_function<T, U>, greater<T>
<==binary_function), and the right operator() can't be called

--
Thanks
Barry
Aug 29 '07 #5
Gl******@gmail.com wrote:
Thanks for the info.

Let me clearify my goal, I want a map to contain a string as the key
and a function pointer template as the data. Meaning, I want to insert
into the map the following things:

map["test"] = equal_to<string>; // or pointers to the
binary_function....
map["test1"] = equal_to<int>;
map["test2"] = less<double>;

etc...

I know the base template for all those functors is the binary_function
template.

Is it possible to have a map where the data is a binary_function
template?
use boost::any
#include <map>
#include <string>
#include <functional>
#include <algorithm>
#include <iostream>

#include <boost/any.hpp>

using namespace std;
using namespace boost;
struct FunctorDispatcher
{
void operator() (pair<string, boost::anyconst& p) const {
if (p.first == "less<int>")
{
less<intconst* pFnct
= boost::any_cast<less<int(&(p.second));
if ((*pFnct)(1, 2))
cout << "true" << endl;
}
else if (p.first == "equal_to<string>")
{
equal_to<stringconst* pFnct
= boost::any_cast<equal_to<std::string(&(p.second));
if ((*pFnct)("1", "1"))
cout << "true" << endl;
}
}
};

int main()
{

map<string, anyfunctorMap;

functorMap.insert(make_pair("less<int>", less<int>()));
functorMap.insert(make_pair("equal_to<string>", equal_to<string>()));

for_each(functorMap.begin(), functorMap.end(), FunctorDispatcher());
}

--
Thanks
Barry
Aug 29 '07 #6
Barry,

Thanks for the help. I have been working on a different method to do
this, but have hit the same results.

I have created a base class:

template <typename T>
class base {
public:
base(T eval) :
mValue(eval)
{};

virtual ~base() {};

bool run(const string &actualValue) {
runInternal(mValue, actualValue);
}

protected:
virtual bool runInternal(int v1, const string &v2) = 0;
virtual bool runInternal(const string &v1, const string &v2) = 0;

protected:
T mValue;
};
template <typename T>
class d1 : public base<T{
public:
d1(T eval) :
base<T>(eval)
{};

private:
bool runInternal(int v1, const string &v2) {
// convert string to int
return v1 == v2;
};

bool runInternal(const string &v1, const string &v2) {
return v1 == v2;
};
};

As I am sure you can see, I can not create a map<string, base *>
because base needs a type at that point. Can I somehow use a template
function in the class and not a complete template class?

Basically I need to have a map of functions, classes whatever that
test <, and == of a known value and actual value. They can be
strings, int, and doubles. Is this possible?

Thanks

Glenn

On Aug 29, 10:28 am, Barry <dhb2...@gmail.comwrote:
Glenn...@gmail.com wrote:
Thanks for the info.
Let me clearify my goal, I want a map to contain a string as the key
and a function pointer template as the data. Meaning, I want to insert
into the map the following things:
map["test"] = equal_to<string>; // or pointers to the
binary_function....
map["test1"] = equal_to<int>;
map["test2"] = less<double>;
etc...
I know the base template for all those functors is the binary_function
template.
Is it possible to have a map where the data is a binary_function
template?

use boost::any

#include <map>
#include <string>
#include <functional>
#include <algorithm>
#include <iostream>

#include <boost/any.hpp>

using namespace std;
using namespace boost;

struct FunctorDispatcher
{
void operator() (pair<string, boost::anyconst& p) const {
if (p.first == "less<int>")
{
less<intconst* pFnct
= boost::any_cast<less<int(&(p.second));
if ((*pFnct)(1, 2))
cout << "true" << endl;
}
else if (p.first == "equal_to<string>")
{
equal_to<stringconst* pFnct
= boost::any_cast<equal_to<std::string(&(p.second));
if ((*pFnct)("1", "1"))
cout << "true" << endl;
}
}

};

int main()
{

map<string, anyfunctorMap;

functorMap.insert(make_pair("less<int>", less<int>()));
functorMap.insert(make_pair("equal_to<string>", equal_to<string>()));

for_each(functorMap.begin(), functorMap.end(), FunctorDispatcher());

}

--
Thanks
Barry

Aug 29 '07 #7
Gl******@gmail.com wrote:
Barry,

Thanks for the help. I have been working on a different method to do
this, but have hit the same results.

I have created a base class:

template <typename T>
class base {
public:
base(T eval) :
mValue(eval)
{};

virtual ~base() {};

bool run(const string &actualValue) {
runInternal(mValue, actualValue);
}

protected:
virtual bool runInternal(int v1, const string &v2) = 0;
virtual bool runInternal(const string &v1, const string &v2) = 0;

protected:
T mValue;
};
template <typename T>
class d1 : public base<T{
public:
d1(T eval) :
base<T>(eval)
{};

private:
bool runInternal(int v1, const string &v2) {
// convert string to int
return v1 == v2;
};

bool runInternal(const string &v1, const string &v2) {
return v1 == v2;
};
};

As I am sure you can see, I can not create a map<string, base *>
because base needs a type at that point. Can I somehow use a template
function in the class and not a complete template class?

Basically I need to have a map of functions, classes whatever that
test <, and == of a known value and actual value. They can be
strings, int, and doubles. Is this possible?
So don't have
template <class T>
class Base;

instead have
class Base {
public:
virutal bool Compare(boost::any arg0, boost::any arg2) = 0;
};

template <class T, U>
class Child : public Base
{
// override Compare
// boost::any_cast to T and U with arg0 and arg1, then do the compare
};

the code can be like this:

#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#include <boost/any.hpp>
class Base {
public:
virtual bool Compare(boost::any const& arg0, boost::any const& arg1)
{
return false;
}

virtual bool Compare(boost::any const& arg)
{
return false;
}

virtual ~Base () { }
};

template <class T, class U>
class Child1 : public Base
{
public:
Child1(U arg1) : arg1_(arg1) {}

protected:
virtual bool Compare(boost::any const& arg0)
{
if (T const* pT = boost::any_cast<T>(&arg0))
{
if ((*pT) == arg1_) {
std::cout << "true" << std::endl;
return true;
}
}

return false;

}
private:
U arg1_;
};
template <class T, class U>
class Child2 : public Base
{
protected:
virtual bool Compare(boost::any const& arg0, boost::any const& arg1)
{
T const* pT = boost::any_cast<T>(&arg0);
U const* pU = boost::any_cast<U>(&arg1);

if (pT && pU)
{
if ((*pT) == (*pU)) {
std::cout << "true" << std::endl;
return true;
}
}

return false;
}
};

bool operator== (std::string const& lhs, int rhs)
{
std::ostringstream oss;
oss << rhs;
return lhs == oss.str();
}

bool operator== (int lhs, std::string const& rhs)
{
std::ostringstream oss;
oss << lhs;
return rhs == oss.str();
}

int main()
{
std::map<std::string, Base*Map;
Map["test1"] = new Child1<int, int>(10);
Map["test2"] = new Child1<int, std::string>("100");
Map["test3"] = new Child1<std::string, int>(100);

Map["test1"]->Compare(10);
Map["test2"]->Compare(100);
Map["test3"]->Compare(std::string("100"));

Map["test4"] = new Child2<int, int>();
Map["test5"] = new Child2<int, std::string>();
Map["test6"] = new Child2<std::string, int>();

Map["test4"]->Compare(10, 10);
Map["test5"]->Compare(100, std::string("100"));
Map["test6"]->Compare(std::string("100"), 100);

for (std::map<std::string, Base*>::iterator it = Map.begin();
it != Map.end(); ++it)
{
delete it->second;
}
}
Aug 30 '07 #8
Gl******@gmail.com schrieb:
Basically I need to have a map of functions, classes whatever that
test <, and == of a known value and actual value. They can be
strings, int, and doubles. Is this possible?
Take a look at boost.variant and boost.function:

http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/function.html

With boost.variant you can define visitors that compare two variant
instances. A variant can store a set of types like:

typedef boost::variant<std::string, int, doublemyVariant;

--
Thomas
http://www.netmeister.org/news/learn2quote.html
The pedants here will shout "int main(void)" but I'll just whisper it.
--Bob Wightman in comp.lang.c
Sep 3 '07 #9
Thomas J. Gritzan wrote:
Gl******@gmail.com schrieb:
>Basically I need to have a map of functions, classes whatever that
test <, and == of a known value and actual value. They can be
strings, int, and doubles. Is this possible?

Take a look at boost.variant and boost.function:

http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/function.html

With boost.variant you can define visitors that compare two variant
instances. A variant can store a set of types like:

typedef boost::variant<std::string, int, doublemyVariant;
With Boost.Variant, the operator() argument can only be one

It it right?

So it can't deal the Compare(Type1 arg0, Type2 arg1) case

--
Thanks
Barry
Sep 3 '07 #10
Barry schrieb:
Thomas J. Gritzan wrote:
>Gl******@gmail.com schrieb:
>>Basically I need to have a map of functions, classes whatever that
test <, and == of a known value and actual value. They can be
strings, int, and doubles. Is this possible?

Take a look at boost.variant and boost.function:

http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/function.html

With boost.variant you can define visitors that compare two variant
instances. A variant can store a set of types like:

typedef boost::variant<std::string, int, doublemyVariant;
With Boost.Variant, the operator() argument can only be one

It it right?

So it can't deal the Compare(Type1 arg0, Type2 arg1) case
You can have binary visitors:
http://www.boost.org/doc/html/varian...ary-visitation

You can even use a template, so that you don't have to write a function for
every possible combination.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
To iterate is human, to recurse divine.
-L. Peter Deutsch
Sep 3 '07 #11

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

Similar topics

2
by: Good Enchiladas | last post by:
While building on a class library for an object model, I get the above error message. The steps to recreate the problem are as follows: 1. Build a RootLevel.dll containing only this code: ...
0
by: Skeeve | last post by:
Hi there, I'm trying to create a containing box (relative position), and within it, I'm trying to line that box with four (top, right, left, bottom) thin boxes that contain a repeated background...
2
by: Skeeve | last post by:
Hi there, I'm trying to create a containing box (relative position), and within it, I'm trying to line that box with four (top, right, left, bottom) thin boxes that contain a repeated background...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
26
by: Bill Norton | last post by:
If you wrap one division around another like this: <div id="wrapper"> <div id="child">...</div> </div> ....the wrapper division will be considered the containing block of the child division...
4
by: Ivor Somerset | last post by:
Dear CSS community, The code below shows my problem. I have a containing DIV box into which I place floating boxes. As the background-color shows, the size of the containing box is not extended...
1
by: Mohd Al Junaibi | last post by:
Hi all... I've got a group of tables and I wanted to first see which tables contained a fields containing the "Date/Time" data type. And I was successful in doing so...here's the query: (...
17
by: Umesh | last post by:
Can anyone do it? ARMY1987- what say?
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.