octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mapper functions for 3.1


From: John W. Eaton
Subject: Re: mapper functions for 3.1
Date: Sat, 16 Feb 2008 15:10:05 -0500

On 16-Feb-2008, John W. Eaton wrote:

| There might be a slick way to combine the functor
| and functor_with_conversion classes using default template parameters
| but I don't see it at the moment.

OK, here is one way.  Define the functor class like this:

  template <typename RT, typename PT, typename CT = RT>
  class functor
  {
  private:
    typedef typename fcn_ptr<RT, PT>::TYPE fcn_ptr_type;
    fcn_ptr_type fptr;

  public:

    functor (fcn_ptr_type p) : fptr (p) { }

    CT operator () (PT arg) { return CT (fptr (arg)); }
  };

and then define two functions for generating the functor object:

  template <typename RT, typename PT>
  functor<RT, PT>
  func_ptr (RT (*f) (PT))
  {
    return functor<RT, PT> (f);
  }

  template <typename CT, typename RT, typename PT>
  functor<RT, PT, CT>
  func_ptr (RT (*f) (PT))
  {
    return functor<RT, PT, CT> (f);
  }

The first doesn't do type conversion and relies on the default
template parameter in the functor class (typename CT = RT).  The
second uses all three args.  Note that CT comes first in the func_ptr
declaration and last in the functor class declaration.  It has to be
last in the functor class declaration to allow us to omit it when it
is the same as RT, but it must be first in the func_ptr function
declaration so that we only have to use

  func_ptr<bool> (std::isalpha)

(for example) instead of

  func_ptr<int,int,bool> (std::isalpha)

when using the conversion type.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]