[vtk-developers] AddObserver taking C++11 lambda?

Hahn, Steven E. hahnse at ornl.gov
Thu Mar 23 11:14:33 EDT 2017


Would it be more efficient to pass the lambda via a template parameter, like what is done in vtkClassMemberCallback? std::function<> adds a lot of overhead (see https://vittorioromeo.info/index/blog/passing_functions_to_functions.html ) and may be overkill.

Steven

On 3/23/17, 10:31 AM, "vtk-developers on behalf of Robert Maynard" <vtk-developers-bounces at vtk.org on behalf of robert.maynard at kitware.com> wrote:

    While allowing lambda's to be held by std::function is really nice, it
    becomes very easy to mistakenly captures items whose lifetime is
    shorter than the Observer you are binding too.
    
    I am currently writing up the first draft of c++11 guidelines for VTK
    and for lambda functions and these are the guidelines I currently
    have:
    
    Usage of lambda expressions are allowed with the following guidelines.
    
    - Use default capture by value ([=]) only as a means of binding a few
    variables for a short lambda, where the set of captured variables is
    obvious at a glance. Prefer not to write long or complex lambdas with
    default capture by value.
    - Except for the above, all capture arguments must be explicitly
    captured. Using the default capture by reference ([&]) is not allowed.
    This is to done so that it is easier to evaluate lifespan and
    reference ownership.
    - Keep unnamed lambdas short. If a lambda body is more than maybe five
    lines long, prefer using a named function instead of a lambda.
    - Specify the return type of the lambda explicitly if that will make
    it more obvious to readers.
    
    On-top of these guidelines I have also stated that lambda functions
    should be kept to implementation files in VTK, which is fine as we go
    with an approach like David proposed.
    
    On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik
    <elvis.stansvik at orexplore.com> wrote:
    > Den 22 mars 2017 6:48 em skrev "David Gobbi" <david.gobbi at gmail.com>:
    >>
    >> It doesn't look hard.  New AddObserver methods would be needed that take
    >> std::function<> as a parameter, and they'd have to store the "function"
    >> object and call it.  Overall, it would probably be simpler than the code
    >> that currently exists in vtkObject.h for adding observers.
    >
    > Good to hear, I might have a go at it then.
    >
    > Elvis
    >
    >>
    >> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik
    >> <elvis.stansvik at orexplore.com> wrote:
    >>>
    >>> Hi all,
    >>>
    >>> With C++11 now required, would there be interest in an AddObserver
    >>> version that takes a C++11 lambda? I've found myself wishing there was
    >>> one a couple of times.
    >>>
    >>> Anyone know off the top of their head how easy/hard that would be to add?
    >>>
    >>> Cheers,
    >>> Elvis
    >>> _______________________________________________
    >>> Powered by www.kitware.com
    >>>
    >>> Visit other Kitware open-source projects at
    >>> http://www.kitware.com/opensource/opensource.html
    >>>
    >>> Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >>>
    >>> Follow this link to subscribe/unsubscribe:
    >>> http://public.kitware.com/mailman/listinfo/vtk-developers
    >>>
    >>
    >
    >
    > _______________________________________________
    > Powered by www.kitware.com
    >
    > Visit other Kitware open-source projects at
    > http://www.kitware.com/opensource/opensource.html
    >
    > Search the list archives at: http://markmail.org/search/?q=vtk-developers
    >
    > Follow this link to subscribe/unsubscribe:
    > http://public.kitware.com/mailman/listinfo/vtk-developers
    >
    >
    _______________________________________________
    Powered by www.kitware.com
    
    Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
    
    Search the list archives at: http://markmail.org/search/?q=vtk-developers
    
    Follow this link to subscribe/unsubscribe:
    http://public.kitware.com/mailman/listinfo/vtk-developers
    
    
    



More information about the vtk-developers mailing list