
sboschi75-personal@yahoo.it 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