tags:

views:

97

answers:

4

I have created a very simple Tree implementation, and I would like to be able to pass a function object to my traverse() function. e.g.

template<class T>
class MyTree
{
public:

    void traverse(MyFunction f) {
        traverse(root, f);
    }
private:
      MyTreeNode<T>* root;
      void traverse(MyTreeNode<T>*, MyFunction f);
};

The thing is, how do I declare f if I want to pass in some parameters as well as the node in question? ( pointers to other structs ). Alternatively, can anyone point me in the direction of some tutorials?

+2  A: 

One possibility is to use boost::function and define a function type which only takes your node as parameter. Then, you can create a boost::function object from whatever function you like as long as you bind the additional parameters.

template<class T>
class MyTree
{
public:
    typedef boost::function< void (MyTreeNode<T>*) > MyFunction;

    void traverse(MyFunction f) {
        traverse(root, f);
    }
private:
      MyTreeNode<T>* root;
      void traverse(MyTreeNode<T>*, MyFunction f);
};

tree.traverse( boost::bind( &func_with_args, _1, arg1, arg2 ) );
UncleZeiv
+2  A: 

You could make it a template member function,

template<class T>
class MyTree
{
public:

    template<typename Functor>
    void traverse(Functor f) {
        traverse(root, f);
    }
private:
      MyTreeNode<T>* root;

      template<typename Functor>
      void traverse(MyTreeNode<T>* node, Functor f) {
         f(node->value);
         // etc...
      }
};

In this form, any function objects that can be called as f(x) will be accepted.

KennyTM
@KennyTM where should I declare Functor? What would it look like if it has extra parameters?
BeeBand
@BeeBand: There's no need to declare Functor because it is a template parameter.
KennyTM
+1  A: 

There are different things that you can do at this stage.

The old style C type solution: you can pass a function pointer:

template <typename T>
void MyTree<T>::traverse( void (*function)( MyTreeNode<T>*, int ) );

That will take as argument a function pointer (free function) that takes a pointer to a MyTreeNode<T> object as first argument and an integer as second argument. And you can typedef the function type to ease usage:

template <typename T>
class MyTree {
public:
   typedef void func( MyTreenode<T>*, int );     // typedef the function type
   typedef void (*pfunc)( MyTreenode<T>*, int ); // or the function pointer
   void traverse( func* function );
   void traverse2( pfunc function );
};

You can take any callable thing as argument with the help of a template:

template <typename T>
class MyTree {
public:
   template <typename Functor> 
   void traverse( Functor f );
};

Which in the simplest approach is simple if you leave the argument types free, but the error messages if you try to pass a callable entity that takes a different set of arguments may not be so simple to parse. Internally you can use f as if it were a function, but externally can be either a function or function objects, including the result of std::bind (c++0x) or boost::bind

You can go one step forward and actually enforce in the signature of the traverse function the arguments you are going to use by using std::function (again, c++0x) or boost::function:

template <typename T>
class MyTree {
public:
   void traverse( std::function< void (MyTreeNode<T>*, int )> f );
};

The advantage of this solution is that it is clear how you are going to use the passed in parameter (as in the function pointer approach): you will call f( x, y ) where x is a MyTreeNode<T>* and y is an integer. At the same time you provide the genericity of the templated solution by means of std::bind (c++0x) or boost::bind, that can be used to adapt different entities to the function<...> argument, including free functions, function objects, member functions...

David Rodríguez - dribeas
+1  A: 

If I understand it right, you are asking about how a functor is declared. Here is a small code that should give you the idea.

struct A{
 void operator()(int x, double d){}   // overloaded operator()
};

int main(){
 A a;
 a(2, 3.0);
}
Chubsdad