Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-01-05 15:00:05

Trent Hill wrote:

> int
> main(int a_argc, char **a_argv)
> {
> C c;
> boost::bind(&foo, c);
> return EXIT_SUCCESS;
> }
> results in this output (Visual C++ .NET with /O2):
> C(const C &c)
> C(const C &c)
> C(const C &c)
> C(const C &c)
> Four copies - I had hoped to only see a single copy. Logically, there
> doesn't seem to be a need for more than one copy. Is there an
> implementation issue that requires this many copies?

Not really, but eliminating the unnecessary copies is (relatively) hard, at
least with the current code base (it has been developed with portability in
mind and supports VC6, among others). The specification requires one copy.
One more is almost unavoidable. The other two can be eliminated, in theory.

> I was hoping to use bind with some moderately weighted objects,
> however the number of copies that seem to occur may be prohibitive
> for me.

... please note that the copies occur at bind time. In a typical scenario
you bind once and call O(N) times (or bind and store in a function<> with
the associated dynamic memory allocation), so this isn't usually a problem.
And, as others have pointed out, there's always ref(). ;-)

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at