# New Functor Filter with lambda support

**URL:** <https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899>\
**Category:** Engineering\
**Created:** [May 3, 2018, 8:27pm UTC](https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899 "2018-05-03T20:27:18Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [May 4, 2018, 12:54pm UTC](https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899/3 "2018-05-04T12:54:04Z")

</div>

The following are the signatures added for “SetFunctor”:

```auto
  using ConstRefFunctionType = OutputImagePixelType (const Input1ImagePixelType &,
                                                     const Input2ImagePixelType &);
  using ValueFunctionType = OutputImagePixelType (Input1ImagePixelType,
                                                  Input2ImagePixelType);

  /** Set the pixel functor
   *
   * The functor defines an operation done per pixel.
   */
 void SetFunctor( const std::function<ConstRefFunctionType> &f);
 void SetFunctor( const std::function<ValueFunctionType> &f);

  /** Set the pixel functor by a C function pointer
   *
   * The functor defines an operation done per pixel.
   */
  void SetFunctor( ConstRefFunctionType *funcPointer);
  void SetFunctor( ValueFunctionType *funcPointer);

  /** Set the pixel functor by a "Functor Object"
   *
   * The functor defines an operation done per pixel. A single copy of
   * the argument is created an used for all threads, so the functor
   * must be concurrent thread-safe. The functor must a have an
   * operator() method which accept arguments of Input1ImagePixelType,
   * Input2ImagePixelType.
   */
  template <typename TFunctor>
  void SetFunctor( const TFunctor &functor);

```

So these signatures accept the C++ lambdas, C++ `std::function`s, C-Style function pointers, and ITK Style functors. The one negative thing is that the last template `SetFunctor`has no constraints so if “junk” is passed to it then an obscure compiler error message can be generated.

Also passing `SetFunctor` C++ lambda, will use the templated signature. While the type of a lambda can be converted to a `std::function`, the `SetFunction`’ temple signature has higher priority than implicitly converting to `std::function`. Because the template signature is used there is no enforcement of the specific arguments and return type for the lambda ( neither is there for the ITK Functors either ).

So this current approach has chosen flexibility over strong typing for the `SetFunctor` argument. Is there any options or feedback from the modern C++ gurus around?

---

_[View the full topic](https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899)._
