[Insight-developers] Best method for a do-nothing return-value function

Luis Ibanez luis.ibanez at kitware.com
Wed Dec 22 16:01:30 EST 2010


Hi Michael,

Yeap,
essentially the two typical options are


A) Functors:

     Class X is templated over the Functor type
     and everything gets resolved at compilation time.
     The code here gets inlined in the instantiation of
     Class X.

or

B)  Polymorphism and virtual methods.

      Class X knows about the base class of the
      Function classes, holds a pointer to one of such
      instances and call a virtual method of the Function class.
      Then you create a family of function classes and
      implement different behaviors in their virtual methods.

      It is flexible at run time,
      but don't get the benefit of inlining.


--

Something that can help in to choose between (A) and (B)
is to profile the time that one of the typical virtual methods
will take to execute at run time.  It might be that the overhead
of the virtual call is not significant when compared to the time
that it takes to execute the body of the method.


     Luis


-----------------------------------------------------------------
On Wed, Dec 22, 2010 at 1:20 PM, M Stauffer (V) <mstauff at verizon.net> wrote:
> Hi Luis,
>
> After looking into this more, it seems that to be able to inline a functor's
> function operator, I would have to template a containing class over the
> functor class.
>
> Otherwise, if I were to not template and simply require the user to assign a
> derived functor to a containing class' member variable of type
> MyFunctorBaseClass, then it seems the compiler wouldn't know ahead of time
> the particular functor implementation that's being used, and likely couldn't
> inline. This then seems like the same issue with inlining a virtual method
> in a regular class.
>
> Does this sound right?
>
> Cheers,
> Michael
>
> ________________________________
> From: Luis Ibanez [mailto:luis.ibanez at kitware.com]
> Sent: Saturday, December 18, 2010 6:22 PM
> To: M Stauffer (V)
> Cc: ITK-dev-list
> Subject: Re: [Insight-developers] Best method for a do-nothing return-value
> function
>
> Hi Michael,
>
> This call looks reasonable, note however that
> if you declare the function to be "virtual" as you
> probably need to do in this case in order to
> achieve polymorphism, then the call to this function
> will not be inlined.
>
> The typical option here is to use Functors, if you
> want to get both the flexibility of polymorphism,
> and the efficiency of inlining.
>
>
> ������ Luis
>
>
> On Fri, Dec 17, 2010 at 6:09 PM, M Stauffer (V) <mstauff at verizon.net> wrote:
>>
>> Hi,
>>
>> I'm not sure how to phrase the subject. I'm creating a function base
>> class whose "Evaluate", i.e. do-its-thing, method needs to simply return
>> the input value. Derived classes will modify the value in some way, not
>> surprisingly.
>>
>> Is there a more efficient way to implement this than what I have below?
>> I figure most compilers will actually make this inline, but maybe not
>> all?
>>
>> < typename TValue >
>> class FunctionBase {
>>
>> ...
>> ��inline TValue Evaluate( TValue value ) { return value; }
>> ...
>>
>> };
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Kitware offers ITK Training Courses, for more information visit:
>> http://kitware.com/products/protraining.html
>>
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-developers
>
>


More information about the Insight-developers mailing list