Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: STL functio...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 9317 of 9971
Post > Topic >>

Re: STL function object vs. static member function

by Triple-DES <DenPlettfrie@[EMAIL PROTECTED] > Feb 27, 2008 at 04:04 AM

On 26 Feb, 19:44, gnuyuva <gnuy...@[EMAIL PROTECTED]
> wrote:
> On Feb 26, 3:12 am, michael.kierma...@[EMAIL PROTECTED]
 wrote:

> > Hello.
>
> > I want to embed a function in a class, lets say a function that
> > transforms an integer variable into another.
>
> > The first idea to do that is:
>
> > class Func {
> >      static int apply(int a) {...}
>
> > };
>
> > To invoke the function, I have to type
> > Func::apply(a);
>
> > But I want to use the boost lambda library on my function, which
> > demands to embed the function in a STL function object:
>
> > class Func {
> >      int operator(int a) const {...}
>
> I dont think you have compiled your program!! First do it.
> The correct syntax is  ' int operator()(int a) const {.. return val; }
> '
>
>
>
> > };
>

It's also a good thing to make the function object adaptable.
class Func : std::unary_function<int, int> // return type and arg.
type
(...)

> > Because operator(int a) is not a static member, for the invokation I
> > have to create an object of the class first:
>
> > Func()(a);
> > [Is this the correct way to invoke a STL function object?]
>
> no, its
>
> Func your_object;
> int number;
> your_object(number);

There's nothing wrong with
int a = 2;
Func()(a); // creates a tem****ary Func, and calls Func::operator()
(int)

> > Compared to the static-function-approach, now there is an extra call
> > to the constructor of the class Func.
> > I wonder if the compiler can optimize the constructor away in all
> > cases.
>
> > Concerning the performance, is the second approach as good as the
> > first approach?
>
Generally, yes. When using the function object with an algorithm, it
could sometimes be faster because it is more easily inlined than a
reference to function.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 1 Posts in Topic:
Re: STL function object vs. static member function
Triple-DES <DenPlettfr  2008-02-27 04:04:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:47:56 CDT 2008.