473,699 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

external functionality with a functor object?

Hi everyone,

I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized functor
may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}
};
This was a good idea except for the fact that now I have no idea of how
the user is going to delacre an object of classA =/
Any ideas????

Sep 22 '06 #1
12 1960
aaragon wrote:
I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
Or, in one line instead of two:

return Functor()();

:-)
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized
functor may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}
How about making your 'operator()' a template?

struct Functor
{
template<class Tdouble operator()(T t_) {
return t_.x() + t_.y();
}
};
};
This was a good idea except for the fact that now I have no idea of
how the user is going to delacre an object of classA =/
Any ideas????
See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #2

aaragon wrote:
Hi everyone,

I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized functor
may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}
};
This was a good idea except for the fact that now I have no idea of how
the user is going to delacre an object of classA =/
Any ideas????
Not sure what you are trying to do by that Template Functor but this
should work.

struct eval
{
int x(){
return 1;
}
int y(){
return 2;
}

};

template <class Functor>
class ClassA
{
public:
eval tmp;
double evaluate(){
Functor functor_;
return functor_(tmp);
}
};
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}

};

int main(int argc, char* argv[])
{

ClassA<Functor< eval a;
a.evaluate();
}

Sep 22 '06 #3

Victor Bazarov wrote:
aaragon wrote:
I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();

Or, in one line instead of two:

return Functor()();

:-)
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized
functor may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}

How about making your 'operator()' a template?

struct Functor
{
template<class Tdouble operator()(T t_) {
return t_.x() + t_.y();
}
};
Yes! This works fine! =)
};
This was a good idea except for the fact that now I have no idea of
how the user is going to delacre an object of classA =/
Any ideas????

See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
However, I still have one problem. The user needs to implement the
entire functor and I don't think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???

Sep 22 '06 #4
aaragon wrote:
[..]
However, I still have one problem. The user needs to implement the
entire functor and I don't think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???
I don't understand the problem. Can you elaborate?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #5

Victor Bazarov wrote:
aaragon wrote:
[..]
However, I still have one problem. The user needs to implement the
entire functor and I don't think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???

I don't understand the problem. Can you elaborate?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sure. Well, the way you mentioned works just fine. But the idea is to
hide the implementation details for the user. Let's say, the user has
to write:

struct Functor
{
template<class T>
double operator()(T t_) {
return t_.x() + t_.y();
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
....
}

and for someone who does not have idea about C++, the functor struct is
just hard! So, I was thinking that it may be a good idea to do
something easier, like overriding the function because the user does
not have to deal with implementation details. If the user does not put
template<class Tthe functor will not work. Any ideas to accomplish
this?

Sep 22 '06 #6
aaragon wrote:
I'm trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
}
Did you need all this templates? A simpler way can be:

class ClassA {
public:
// ...
virtual double operator () ()= 0;
//...
double evaluate ()
{
return operator () ();
}
};
class UserClassA : public ClassA {
double operator () ()
{
return 2 + 2;
}
};

int main ()
{
UserClassA a;
a.evaluate ();
}

--
Salu2
Sep 22 '06 #7
aaragon wrote:
Victor Bazarov wrote:
>aaragon wrote:
>>[..]
However, I still have one problem. The user needs to implement the
entire functor and I don't think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???

I don't understand the problem. Can you elaborate?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Sure. Well, the way you mentioned works just fine. But the idea is
to hide the implementation details for the user. Let's say, the user
has to write:

struct Functor
{
template<class T>
double operator()(T t_) {
return t_.x() + t_.y();
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
...
}

and for someone who does not have idea about C++, the functor struct
is just hard! So, I was thinking that it may be a good idea to do
something easier, like overriding the function because the user does
not have to deal with implementation details. If the user does not
put template<class Tthe functor will not work. Any ideas to
accomplish this?
So, what is the user supposed to write to accomplish what they need?
If the 'Functor' above is hard (and yes, member templates are not the
simplest thing to understand, I give you that), what would be "easy"
for somebody "who does not have idea about C++"? If they don't have
a clue, why would they be writing those functors?

Let's suppose you want to make their life easier. You write some kind
of base class

class BaseFunctor { /* not sure what goes here yet */ };

, right? OK, how the user will make use of it? They would have to
know to derive from it, correct? So

class UserFunctor : public Functor { /* something */ };

and then to achieve the goal they would need to know how to override
a virtual function and not to change the signature... It seems that
they still would need to know a lot.

Of course, another approach is to hide the scary template stuff into
a macro, like this:

#define FUNCTOR_METHOD( type, arg) \
template<class Tdouble operator()(T arg)

and then ask your user to do

struct Functor {
FUNCTOR_METHOD( double, t_) {
return t_.x() + t_.y();
}
};

Is that better?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '06 #8

Victor Bazarov wrote:
aaragon wrote:
Victor Bazarov wrote:
aaragon wrote:
[..]
However, I still have one problem. The user needs to implement the
entire functor and I don't think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???

I don't understand the problem. Can you elaborate?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sure. Well, the way you mentioned works just fine. But the idea is
to hide the implementation details for the user. Let's say, the user
has to write:

struct Functor
{
template<class T>
double operator()(T t_) {
return t_.x() + t_.y();
}
};

int main(int argc, char *argv[])
{
ClassA<Functora ;
a.evaluate();
...
}

and for someone who does not have idea about C++, the functor struct
is just hard! So, I was thinking that it may be a good idea to do
something easier, like overriding the function because the user does
not have to deal with implementation details. If the user does not
put template<class Tthe functor will not work. Any ideas to
accomplish this?

So, what is the user supposed to write to accomplish what they need?
If the 'Functor' above is hard (and yes, member templates are not the
simplest thing to understand, I give you that), what would be "easy"
for somebody "who does not have idea about C++"? If they don't have
a clue, why would they be writing those functors?

Let's suppose you want to make their life easier. You write some kind
of base class

class BaseFunctor { /* not sure what goes here yet */ };

, right? OK, how the user will make use of it? They would have to
know to derive from it, correct? So

class UserFunctor : public Functor { /* something */ };

and then to achieve the goal they would need to know how to override
a virtual function and not to change the signature... It seems that
they still would need to know a lot.

Of course, another approach is to hide the scary template stuff into
a macro, like this:

#define FUNCTOR_METHOD( type, arg) \
template<class Tdouble operator()(T arg)

and then ask your user to do

struct Functor {
FUNCTOR_METHOD( double, t_) {
return t_.x() + t_.y();
}
};

Is that better?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
=) That's a good idea! (altough I don't like to deal with macros for
some reason). Another would be to pass just a function instead of an
object, right? Maybe that's also an easy way to implement this. What
do you think?

Sep 23 '06 #9
aaragon wrote:
Victor Bazarov wrote:
>[..]
Of course, another approach is to hide the scary template stuff into
a macro, like this:

#define FUNCTOR_METHOD( type, arg) \
template<class Tdouble operator()(T arg)

and then ask your user to do

struct Functor {
FUNCTOR_METHOD( double, t_) {
return t_.x() + t_.y();
}
};

Is that better?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

=) That's a good idea! (altough I don't like to deal with macros for
some reason). Another would be to pass just a function instead of an
object, right? Maybe that's also an easy way to implement this. What
do you think?
Yes, functions are essentially functors (in C++ terms) since when named
by their name they can be called using the function call operator.

You still haven't explained how your users that aren't very sophisticated
(by your own opinion) will deal with those things. What do you expect
them to be able to do? What level do you think they need to be at to be
able to use your library? I am not saying you should require them to
implement type traits with a bunch of typedefs and static members to
successfully utilize your library, but FCOL, if they don't know C++ (or
most of it), maybe you should concentrate on Visual Basic mechanism for
them to use?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 24 '06 #10

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

Similar topics

6
5924
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
3
2161
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can instantiate in the following ways PQueue<int> pq1; PQueue<int, Functor> pq2; // where Functor is a name of user-defined class I also added another constructor to accept a function pointer so that
4
2901
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
2
2514
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
2
20145
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating the Functor object. The code is as follows: #include <iostream> using namespace std;
5
2527
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's a multi-threaded application, naturally I want the call back functor to be created on a per thread basis. Suppose my thread is so defined template <typename servlet_type>
1
2808
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I incorporate the same logic within my simulation, the class that implements the functor logic has problems compiling. I get the following errors: -- Building myTest.cpp --
2
2287
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling ----nextSibling ---->0 | |
11
2279
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
9173
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6533
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
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4375
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
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.