Boost logo

Boost Users :

From: William E. Kempf (wekempf_at_[hidden])
Date: 2003-05-19 09:52:36


Mark Sizer said:
> This is also stuff that has been answered before. I really have
> searched. Links to the answers will be as appreciate as an inline
> responses.
>
> I've been using free file-scoped functions up to now because I really
> don't understand what a boost::function0<void>& is.

It's a "function object" which takes no parameters and returns nothing.
Function pointers are valid "function objects", but so are class instances
with an overloaded operator().

> I get, I think, that it can't be a member function, because there is no
> 'this' attached.

Not directly, however boost::bind can be used to create a function object
that does call a member function.

> However, I'm having trouble with two things:
>
> #1 from: function.tutorial.html
> <quote>
> boost::function2<float, int, int> f;
>
> struct int_div {
> float operator()(int x, int y) const { return ((float)x)/y; };
> };
>
> f = int_div();
> </quote>
>
> operator() _is_ a member function. When assigning int_div() to f, which
> of potentially zillions of instances of int_div is being referenced? Is
> a new one created on invocation? (the example uses std::cout << f(5,2)
> << endl;)

The syntax "int_div()" is a call to the int_div constructor. So, 'f' is
being assigned to a new instance of int_div. This can be done, because
int_div *is a function object* who's signature matches that required by
boost::function2<float, int, int>.

> I'm missing a theoretical bit of information that makes this hang
> together. Help, please.
>
> #2 from:
> http://www.cuj.com/articles/2002/0205/0205a/0205a.htm?topic=articles
> <quote>
> However, because Boost.Threads uses a function object instead of just
> a function pointer, it is possible for the function object to carry data
> needed by the thread. This approach is actually more flexible and is
> type safe. When combined with functional libraries, such as Boost.Bind,
> this design actually allows you to easily pass any amount of data to the
> newly created thread.
> </quote>
>
> BUT HOW?!?! Any pointers (with many examples, please) would be much
> appreciated.

$BOOST_ROOT/libs/thread/tutorial/helloworld4.cpp illustrates using
Boost.Bind to pass data while creating a thread.

Sorry for the frustration. I need a full tutorial in the documentation,
so this isn't something that's your fault, but mine. For now, all I can
recommend is that you look at the numerous example programs included in
the CVS archive.

-- 
William E. Kempf

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net