Boost logo

Boost :

From: Atry (pop.atry_at_[hidden])
Date: 2007-06-25 01:52:28


Hi everybody, in some case I want to have an template parameter of functor
like this:

////////////////////
template<typename Functor>
double add(double l, double r)
{
  return Functor::invoke(l, r);
}

int main()
{
  // simply invoke:
  BOOST_ASSERT( BOOST_STATIC_LAMBDA_FUNCTOR(_1 + _2 * _3)::invoke(1, 3, 5)
== 1 + 3 * 5 );

  // as a template parameter.
  BOOST_ASSERT( add<BOOST_STATIC_LAMBDA_FUNCTOR(_1 + _2)>(0.1, 0.2) == 0.1 +
0.2 );

  // define as a type
  typedef BOOST_STATIC_LAMBDA_FUNCTOR(_1 << _2 << _3) left_shift_type;

  // print hello world, first parameter(cout) is reference.
  left_shift_type::invoke<ostream&>(cout, "hello world", &endl<char,
char_traits<char> >);
}
/////////////////////////////////////

The BOOST_STATIC_LAMBDA_FUNCTOR used an internal BOOST_TYPEOF, could parse a
lambda expression to a static type, which has a template function "invoke"
with any type parameter. I think that would be useful and I have done some
work on it. Is there anyone interested in it?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk