Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Allowing a functor or a function to be stored in a class

Reply
Thread Tools

Allowing a functor or a function to be stored in a class

 
 
James Aguilar
Guest
Posts: n/a
 
      03-29-2005
In the C++ STL, there are various places where you can call a function that
takes a functor as a parameter. This functor may be either a function object or
a function pointer. For instance, if you have a vector<char> and call for_each
on it, both of the methods below would be valid:

#include <iostream>
#include <vector>
using namespace std;

struct MyFunctionObject
{
void operator ()(char a) {cout << a;};
};
void myFunction(char a) {cout << a;}

int main()
{
vector<char> v;
//fill it . . .
for_each(v.begin(), v.end(), (MyFunctionObject()));
for_each(v.begin(), v.end(), &myFunction);
return 0;
}

My question pertains to a similar issue: how do I cause the same to occur in a
class template? I would like my template to look like this:

template <typename CompT>
class WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

Can I actually do this in C++ without writing an inordinate amount of code? If
so, how?

- JFA1


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      03-29-2005
James Aguilar wrote:
>
> My question pertains to a similar issue: how do I cause the same to occur in a
> class template? I would like my template to look like this:
>
> template <typename CompT>
> class WhyWontThisWork
> {
> CompT m_comparator;
> public:
> WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}
> void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
> };
>
> Can I actually do this in C++ without writing an inordinate amount of code? If
> so, how?
>


This works just fine. What problem did you encounter?

struct Comp
{
bool operator()(char a, char b) const { return a == b; }
};

int main()
{
Comp comp;
WhyWontThisWork<Comp> cmp(comp);
cmp.doThis('a', 'b');
cmp.doThis('a', 'a');
return 0;
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-29-2005
James Aguilar wrote:
> In the C++ STL, there are various places where you can call a function that
> takes a functor as a parameter. This functor may be either a function object or
> a function pointer. For instance, if you have a vector<char> and call for_each
> on it, both of the methods below would be valid:
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> struct MyFunctionObject
> {
> void operator ()(char a) {cout << a;};
> };
> void myFunction(char a) {cout << a;}
>
> int main()
> {
> vector<char> v;
> //fill it . . .
> for_each(v.begin(), v.end(), (MyFunctionObject()));
> for_each(v.begin(), v.end(), &myFunction);
> return 0;
> }
>
> My question pertains to a similar issue: how do I cause the same to occur in a
> class template? I would like my template to look like this:
>
> template <typename CompT>
> class WhyWontThisWork
> {
> CompT m_comparator;
> public:
> WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}


I don't understand 'WhyWontThisWork' bit of it.

> void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
> };
>
> Can I actually do this in C++ without writing an inordinate amount of code?


What's inordinate?

I don't see you even attempting to use your template. How can we continue
the discussion without you even trying?

> If
> so, how?


What book are you reading that doesn't explain templates? What section of
the FAQ regarding questions about some code that doesn't work don't you
understand? Am I using overly long sentences?

V
 
Reply With Quote
 
James Aguilar
Guest
Posts: n/a
 
      03-29-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:IPk2e.58298$(E-Mail Removed)01.us.to .verio.net...
>
> I don't see you even attempting to use your template. How can we continue
> the discussion without you even trying?


OK.

//=== CODE ===
#include <iostream>

using namespace std;

template <typename CompT>
class Test
{
CompT m_comparator;
public:
Test(CompT cmpIn) : m_comparator(cmpIn) {}
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

class TestFunctionObject
{
public:
bool operator ()(char a, char b) {return a < b;}
};

bool testFunction(char a, char b)
{
return a < b;
}

int main()
{
Test a(&testFunction);
Test b((TestFunctionObject()));

a.doThis('a', 'b');
b.doThis('a', 'b');

return 0;
}
//=== CODE ===

For obvious reasons, this code does not compile. I didn't provide template
types for a or b in main. But that is really the question I am asking: is it
possible for me to have a class that can take a function pointer or a function
object in the constructor and treat them the same way (i.e. store them for later
use in another function)? If so, what is the type I need to use to replace
CompT?

- JFA1


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-29-2005
James Aguilar wrote:
> Test(CompT cmpIn) : m_comparator(cmpIn) {}
> If so, what is the type I need to use to replace
> CompT?
>


Since CompT is the type of the argument that you're going to pass to the
constructor, that's the type you should specify. That's also why the
standard library provides the template functions bind1st() and bind2nd()
in addition to the template classes binder1st and binder2nd.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-29-2005
James Aguilar wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:IPk2e.58298$(E-Mail Removed)01.us.to .verio.net...
>
>>I don't see you even attempting to use your template. How can we continue
>>the discussion without you even trying?

>
>
> OK.
>
> //=== CODE ===
> #include <iostream>
>
> using namespace std;
>
> template <typename CompT>
> class Test
> {
> CompT m_comparator;
> public:
> Test(CompT cmpIn) : m_comparator(cmpIn) {}
> void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
> };
>
> class TestFunctionObject
> {
> public:
> bool operator ()(char a, char b) {return a < b;}
> };
>
> bool testFunction(char a, char b)
> {
> return a < b;
> }
>
> int main()
> {
> Test a(&testFunction);
> Test b((TestFunctionObject()));
>
> a.doThis('a', 'b');
> b.doThis('a', 'b');
>
> return 0;
> }
> //=== CODE ===
>
> For obvious reasons, this code does not compile.


Well, DUH!

> I didn't provide template
> types for a or b in main. But that is really the question I am asking: is it
> possible for me to have a class that can take a function pointer or a function
> object in the constructor and treat them the same way (i.e. store them for later
> use in another function)? If so, what is the type I need to use to replace
> CompT?


Of course not. "Test" is a template. It needs an argument. The class
'TestFunctionObject' and the function 'testFunction' have different types.
You cannot expect your 'Test' template to become one and the same if you
instantiate it with two different types, now, can you?

You can always make your 'Test' class take a functor but to use, say,
functions with it you'd have to provide a separate object. Example:

struct myFunctor_base {
virtual void operator() const (char a, char b) = 0;
};

struct function_to_myFunctor_adapter : myFunctor_base {
void (*pfunc)(char,char);
function_to_myFunctor_adapter(void (*p)(char,char)) : pfunc(p) {}
void operator()(char a, char b) const {
return pfunc(a,b);
}
};

struct myFunctor : myFunctor_base {
void operator()(char,char) const; // implemented elsewhere
};

class Test {
myFunctor_base const& f;
public:
Test(myFunctor_base const& ff) : f(ff) {}
void doThis(char a, char b) { f(a,b); }
};

void myFunction(char a, char b);

int main() {
Test a = Test(myFunctor());
Test b = Test(function_to_myFunctor_adapter(myFunction));
a.doThis('a', 'b');
b.doThis('a', 'b');
}

Now, this probably constitutes (how did you put it?) inordinate amount
of code, but that's essentially how you can do it.

V
 
Reply With Quote
 
James Aguilar
Guest
Posts: n/a
 
      03-29-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:tql2e.58303$(E-Mail Removed)01.us.t o.verio.net...
>
> Now, this probably constitutes (how did you put it?) inordinate amount
> of code, but that's essentially how you can do it.


That's exactly how I put it, and yeah, it meets the criteria. I appreciate your
writing that code -- I probably wouldn't have if someone was asking me the same
question.

I take it that the solution to my problem is thus to choose one or the other
(function pointers or function objects) and stick with it, and not try to make
my stuff compatible with both?

- JFA1


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-29-2005
Victor Bazarov wrote:
>>
>> template <typename CompT>
>> class Test
>> {
>> CompT m_comparator;
>> public:
>> Test(CompT cmpIn) : m_comparator(cmpIn) {}
>> void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
>> };
>>
>>
>> bool testFunction(char a, char b)
>> {
>> return a < b;
>> }
>>

>
> You can always make your 'Test' class take a functor but to use, say,
> functions with it you'd have to provide a separate object.


It works as is, but you have to get the type of the template argument right:

Test<bool (*const)(char,char)> wrap(testFunction);

The reason that the standard library templates need wrapper classes for
function pointers is to provide the nested typedefs result_type and the
various argument_types. The TR1 function adaptors don't need these
typedefs, so, like Test, they can be used with raw function pointers.
(See my article in the upcoming May issue of CUJ).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-29-2005
Victor Bazarov wrote:
>
> Test a = Test(myFunctor());
> Test b = Test(function_to_myFunctor_adapter(myFunction));


Oh, I see, you're solving a different problem from the one I was
solving. The original template can be instantiated with various types;
if you need a single type that can hold different types of target
objects you need TR1's template class "function":

function<bool(char,char)> fp;
fp = TestFunctionObject();
fp = testFunction;

(Okay, you have to add "typedef bool result_type" to TestFunctionObject
to get this to compile with normal C++ compilers)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-30-2005
"Pete Becker" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
>>
>> Test a = Test(myFunctor());
>> Test b = Test(function_to_myFunctor_adapter(myFunction));

>
> Oh, I see, you're solving a different problem from the one I was solving.
> The original template can be instantiated with various types; if you need
> a single type that can hold different types of target objects you need
> TR1's template class "function":
>
> function<bool(char,char)> fp;
> fp = TestFunctionObject();
> fp = testFunction;


This is neat. I didn't know that. How many compilers support this?
Is 'function' template class part of 'std'? Which header to include?

> (Okay, you have to add "typedef bool result_type" to TestFunctionObject to
> get this to compile with normal C++ compilers)


"Normal"? What do you mean? Is it already supported?

Thanks.

V


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing functor or function to template class christof.warlich1@gmail.com C++ 14 07-19-2012 09:19 AM
Allowing a Stored Procedure Argument to be NULL Jonathan Wood ASP .Net 8 04-30-2008 08:18 PM
Performance of static member function versus functor chsalvia@gmail.com C++ 3 08-15-2007 12:36 PM
Functor and function pointers Amit C++ 8 03-04-2005 03:36 PM
functor object in template class Chandra Shekhar Kumar C++ 5 06-26-2003 04:55 AM



Advertisments