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 10 1713 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 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
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
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 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
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
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;
}
}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: Umesh |
last post by:
Can anyone do it? ARMY1987- what say?
|
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 ...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |