Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind] counterintuitive behavior with vector and auto_ptr
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-06-12 01:57:58


AMDG

Christian Henning wrote:
> Hi there, I'm doing some job interview preparations. Always a good
> idea to go back to the basics and tinker around a bit. I came across a
> surprising behavior when using std::vector, std::auto_ptr, and
> boost::bind.
>

auto_ptr and vector don't mix. Use unique_ptr instead.

> Basically, I have a vector of auto_ptrs which is copied into a second
> vector, thus the first vector's auto_ptr loss their resource and point
> to nowhere. Basic stuff, as I said. Now, when I try to use an auto_ptr
> from the first vector using boost::bind everything still works.
> Meaning the code doesn't fail, although it should. I find that
> surprising. Why is boost::bind recreating the resource?
>

It isn't. Boost.Bind passes the arguments by reference, so
the auto_ptr is never copied.

In Christ,
Steven Watanabe


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