Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-10-30 04:04:43


sboschi75-personal_at_[hidden] wrote:

> Hallo,
>
> I would like to override operators for boost::function.

Hi Stefano,
in future, please use more specific subject for your postings. Also, putting
your full name in the "From" line is considered more polite than using
"sboschi75"

> Forexample I was thinking of an operator "-" :
>
> template <T>
> function<T> operator - (const function<T>& a) {
> // what do I write here in roder to return the symmetric
> function?
> }

The following works for me.

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>
#include <iostream>

using namespace std;

template<class T>
boost::function<T> operator-(boost::function<T> a)
{
    typedef typename boost::function<T>::result_type rt;
    return boost::bind(std::minus<rt>(), rt(),
                boost::bind(a, _1));
}

int foo(int i)
{
    return i + 2;
}

int main()
{
    boost::function<int (int)> f = foo;
    boost::function<int (int)> f2 = -f;
    cout << f(4) << " " << f2(4) << "\n";
    return 0;
}

- Volodya


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net