<div dir="ltr"><div>Hi Steve,</div><div><br></div><div>I don't think we have to worry about overhead in VTK's observers.  Our main concerns should be safety and usability.  The VTK observer implementation is heavy by design, it is meant to used for overall application control (e.g. for the "controller" part of the MVC pattern).  It is generally not meant to be used within the VTK computational algorithms themselves.</div><div><br></div><div> - David</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 23, 2017 at 9:14 AM, Hahn, Steven E. <span dir="ltr"><<a href="mailto:hahnse@ornl.gov" target="_blank">hahnse@ornl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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 <a href="https://vittorioromeo.info/index/blog/passing_functions_to_functions.html" rel="noreferrer" target="_blank">https://vittorioromeo.info/<wbr>index/blog/passing_functions_<wbr>to_functions.html</a> ) and may be overkill.<br>
<span class="HOEnZb"><font color="#888888"><br>
Steven<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On 3/23/17, 10:31 AM, "vtk-developers on behalf of Robert Maynard" <<a href="mailto:vtk-developers-bounces@vtk.org">vtk-developers-bounces@vtk.<wbr>org</a> on behalf of <a href="mailto:robert.maynard@kitware.com">robert.maynard@kitware.com</a>> wrote:<br>
<br>
    While allowing lambda's to be held by std::function is really nice, it<br>
    becomes very easy to mistakenly captures items whose lifetime is<br>
    shorter than the Observer you are binding too.<br>
<br>
    I am currently writing up the first draft of c++11 guidelines for VTK<br>
    and for lambda functions and these are the guidelines I currently<br>
    have:<br>
<br>
    Usage of lambda expressions are allowed with the following guidelines.<br>
<br>
    - Use default capture by value ([=]) only as a means of binding a few<br>
    variables for a short lambda, where the set of captured variables is<br>
    obvious at a glance. Prefer not to write long or complex lambdas with<br>
    default capture by value.<br>
    - Except for the above, all capture arguments must be explicitly<br>
    captured. Using the default capture by reference ([&]) is not allowed.<br>
    This is to done so that it is easier to evaluate lifespan and<br>
    reference ownership.<br>
    - Keep unnamed lambdas short. If a lambda body is more than maybe five<br>
    lines long, prefer using a named function instead of a lambda.<br>
    - Specify the return type of the lambda explicitly if that will make<br>
    it more obvious to readers.<br>
<br>
    On-top of these guidelines I have also stated that lambda functions<br>
    should be kept to implementation files in VTK, which is fine as we go<br>
    with an approach like David proposed.<br>
<br>
    On Wed, Mar 22, 2017 at 2:00 PM, Elvis Stansvik<br>
    <<a href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>> wrote:<br>
    > Den 22 mars 2017 6:48 em skrev "David Gobbi" <<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>>:<br>
    >><br>
    >> It doesn't look hard.  New AddObserver methods would be needed that take<br>
    >> std::function<> as a parameter, and they'd have to store the "function"<br>
    >> object and call it.  Overall, it would probably be simpler than the code<br>
    >> that currently exists in vtkObject.h for adding observers.<br>
    ><br>
    > Good to hear, I might have a go at it then.<br>
    ><br>
    > Elvis<br>
    ><br>
    >><br>
    >> On Wed, Mar 22, 2017 at 12:53 AM, Elvis Stansvik<br>
    >> <<a href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>> wrote:<br>
    >>><br>
    >>> Hi all,<br>
    >>><br>
    >>> With C++11 now required, would there be interest in an AddObserver<br>
    >>> version that takes a C++11 lambda? I've found myself wishing there was<br>
    >>> one a couple of times.<br>
    >>><br>
    >>> Anyone know off the top of their head how easy/hard that would be to add?<br>
    >>><br>
    >>> Cheers,<br>
    >>> Elvis<br>
    >>> ______________________________<wbr>_________________<br>
    >>> Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
    >>><br>
    >>> Visit other Kitware open-source projects at<br>
    >>> <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
    >>><br>
    >>> Search the list archives at: <a href="http://markmail.org/search/?q=vtk-developers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtk-developers</a><br>
    >>><br>
    >>> Follow this link to subscribe/unsubscribe:<br>
    >>> <a href="http://public.kitware.com/mailman/listinfo/vtk-developers" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/vtk-<wbr>developers</a><br>
    >>><br>
    >><br>
    ><br>
    ><br>
    > ______________________________<wbr>_________________<br>
    > Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
    ><br>
    > Visit other Kitware open-source projects at<br>
    > <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
    ><br>
    > Search the list archives at: <a href="http://markmail.org/search/?q=vtk-developers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtk-developers</a><br>
    ><br>
    > Follow this link to subscribe/unsubscribe:<br>
    > <a href="http://public.kitware.com/mailman/listinfo/vtk-developers" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/vtk-<wbr>developers</a><br>
    ><br>
    ><br>
    ______________________________<wbr>_________________<br>
    Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
    Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
<br>
    Search the list archives at: <a href="http://markmail.org/search/?q=vtk-developers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtk-developers</a><br>
<br>
    Follow this link to subscribe/unsubscribe:<br>
    <a href="http://public.kitware.com/mailman/listinfo/vtk-developers" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/vtk-<wbr>developers</a><br>
<br>
<br>
<br>
<br>
</div></div></blockquote></div><br></div>