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

M Stauffer (V) mstauff at verizon.net
Wed Dec 22 16:57:52 EST 2010


Thanks Gang.  What makes you think operator() wasn't inlined? If you're
trying to test that an inlined FuncChild::Operator() would pick up the
declaration and value of y from the main scope, then you may have a
typo:

>    std::cout << "In FuncChild !" << "x=" << x << ", y=" << x 

this passes 'x' twice - did you mean 'y' for the 2nd 'x'?

Cheers,
Michael 

>-----Original Message-----
>From: gang song [mailto:songgang97 at gmail.com] 
>Sent: Wednesday, December 22, 2010 4:46 PM
>To: M Stauffer (V)
>Cc: Luis Ibanez; ITK-dev-list
>Subject: Re: [Insight-developers] Best method for a do-nothing 
>return-value function
>
>Hi Michael,
>
>My two cents. I think Luis means that inline and virtual are 
>kind of contradictory. Compiler will only try its best to make 
>one function inline when declared as inline. But it could not 
>able to. So I think it will not be inline if we declare something like:
>inline virtual operator( ) { ... }.
>
>I made some toy test like below, trying to recreate your 
>situation. It seems that the operator could not be inline 
>compiled into variable *gg.
>
>#include <iostream>
>#include <ctime>    // For time()
>#include <cstdlib>  // For srand() and rand()
>
>template<class TValue>
>class Func{
>public:
>  virtual ~Func() {};
>   inline virtual TValue operator()(TValue & x) {
>    std::cout << "In Func !" << "x=" << x << ", y=0" << std::endl;
>    return 0;
>  }
>};
>
>template<class TValue>
>class FuncChild:public Func<TValue> {
>public:
>  virtual ~FuncChild() {};
>  inline virtual TValue operator()(TValue & x) {
>    std::cout << "In FuncChild !" << "x=" << x << ", y=" << x 
><< std::endl;
>    return 0;
>  }
>};
>
>int main(int, char **) {
>  int y = 100;
>
>  Func<int> * g = new Func<int>;
>  Func<int> * g1 = new FuncChild<int>;
>  Func<int> * gg;
>
>  srand(time(0));  // Initialize random number generator.
>  int k = (rand() % 10);
>  std::cout << "k="<<k<<std::endl;
>  gg =  (k < 5) ? g : g1;
>  (*gg)(y);
>
>  return EXIT_SUCCESS;
>}
>
>$ ./itkFunctorTest
>k=1
>In Func !x=100, y=0
>$ ./itkFunctorTest
>k=5
>In FuncChild !x=100, y=100
>
>
>
>On Wed, Dec 22, 2010 at 4:21 PM, M Stauffer (V) 
><mstauff at verizon.net> wrote:
>> So if I create an abstract functor base class that has a 
>pure virtual 
>> function call operator, it seems a derived class can have an inlined 
>> overloaded function call operator and the compiler will 
>still be able 
>> to inline it since it knows the base class is abstract?
>>
>> Or do we eschew the idea of a functor base class altogether?
>>
>> Cheers,
>> Michael
>>
>>>-----Original Message-----
>>>From: Luis Ibanez [mailto:luis.ibanez at kitware.com]
>>>Sent: Wednesday, December 22, 2010 4:02 PM
>>>To: M Stauffer (V)
>>>Cc: ITK-dev-list
>>>Subject: Re: [Insight-developers] Best method for a do-nothing 
>>>return-value function
>>>
>>>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
>>>>
>>>>
>>
>> _______________________________________________
>> 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