Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-11-12 16:13:17


On Wed, Nov 12, 2003 at 08:34:49PM +0100, Joaqu?n M? L?pez Mu?oz wrote:
> This is just an idea that I'm pondering in my mind, but maybe
> someone finds it interesting. Consider the following template:
>
> template<typename Functor>
> struct executer
> {
> int operator()(int x){return f(x);}
>
> private:
> Functor f;
> };
>...
> typedef execute_function<lambda::_1*2> execute_doubling;
> // KO: lambda::_1*2 is not a type

In my opinion, the better way to approach this whole issue is

   template <typename Functor>
   struct executer {
      Functor f;
   public:
      executer( const Functor& ff ) : f(ff) {}
      int operator()( int x ) const { return f(x); }
   };
   template <typename F>
   executer<F> make_executer( const F& f ) {
      return executer<F>(f);
   }
   ...
   make_executer( lambda::_1*2 );

This provides you a "value" rather than a "type", but in my experience
the values are more useful anyway. Note also that the expression

   make_executer( function1<int,int>( lambda::_1*2 ) )

even has a relatively "sane" typename:

   executer< function1<int,int> >

Is there some motivation I'm missing? Is there a reason one would
(need to) write classes like your "executer" example, which uses "type
as function", rather than classes like my "executer" example, which uses
"value" too (you pass in a function 'value' to the constructor)?

-- 
-Brian McNamara (lorgon_at_[hidden])

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