Jump to content
  • Advertisement
Sign in to follow this  
kingos

c++ functors as arguments to functions

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Having a few problems with passing std::functors to functions as parameters. I have reduced my code down to the following simple example:

#ifndef MYTEST_H
#define MYTEST_H

#include <list>
#include <algorithm>
#include <functional>


class MyObject
{
    int a;
};

class MyStuff
{
private:
    std::list<MyObject *> myList;

public:
    void Add (MyObject * object, std::unary_function<MyObject *, bool> & function);
};

#endif


and then the cpp file

#include "mytest.h"

using namespace std;

void MyStuff::Add
(
    MyObject * object,
    unary_function<MyObject *, bool> & function
)
{
    list<MyObject *>::iterator i = find_if (myList.begin(), myList.end(), function);

    myList.insert(i, object);
}



This code doesn't compile on vc7.1 or gcc 3.3. c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\algorithm(64) : error C2064: term does not evaluate to a function taking 1 arguments c:\MPLTest\Test2\mytest.cpp(6) : see reference to function template instantiation '_InIt std::find_if<std::list<_Ty>::iterator,std::unary_function<_Arg,_Result>>(_InIt,_InIt,_Pr)' being compiled with [ _InIt=std::list<MyObject *>::iterator, _Ty=MyObject *, _Arg=MyObject *, _Result=bool, _Pr=std::unary_function<MyObject *,bool> ] Any ideas? Am I not allowed to pass functors as arguments to methods? If so, why not? The idea here is to have a list of objects derived from MyObject with common functionality, but custom insertion order .. Cheers, Andrew

Share this post


Link to post
Share on other sites
Advertisement
std::unary_function is not a polymorphic functor class. It's a utility class that provides the typedefs any Adaptable Function Object must provide (result_type, argument_type or first_argument_type and second_argument_type). If you want to pass function objects around, either make the receiving function a function template (because each functor class really is a different type, completely unrelated to any other functor class), or use something like boost::function, which is designed to erase such type differences.

Share this post


Link to post
Share on other sites
Okay, that makes some sense. I am very new to functors, so thanks for the help.

So, in my example, if I want to template the Add function, does that mean that I virtually need to redeclare unary_function myself?

As in



class MyPredicate
{
bool operator()(const & MyObject );
}

void Add(MyObject * object, MyPredicate myP);




I know I am missing something here. What am I supposed to be templating on? What do I need to do to convert a normal class or function to a functor? I have read stuff on ptr_fun and mem_fun etc. If these questions are obvious, or outlined in the documentation somewhere, can you please point me in a direction to start reading?

Thanks for your response,
Andrew

Share this post


Link to post
Share on other sites
Okay.

Worked it all out.

For those reading this list some time in the future :)


#ifndef MYTEST_H
#define MYTEST_H

#include <list>
#include <algorithm>
#include <functional>


class MyObject
{
int a;
};

class DerivedObject : public MyObject
{
};

class MyStuff
{
private:
std::list<MyObject *> myList;

public:
template<typename T>
void Add (MyObject * object, T t);
};

temlate <typename T>
void MyStuff::Add
(
MyObject * object,
T t
)
{
std::list<MyObject *>::iterator i = find_if (myList.begin(), myList.end(), t);

myList.insert(i, object);
}

#endif




We just declare the function to be templated on type T. Then, when I go to use it:


class MyAdder : public std::unary_function<MyObject *, bool>
{
public:
bool operator()(MyObject *)
{
// do something
return true;
}
};
int main()
{
MyStuff myStuff;
MyObject *object = new MyObject();
myStuff.Add<MyAdder>(object, MyAdder());
delete object;
}



Too easy!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!