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! ]


|