Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Iterator runtime assertion with MSVC
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2011-07-22 01:31:23


Hi Narinder,

On Thu, Jul 21, 2011 at 5:21 PM, Narinder Claire
<narinder.claire_at_[hidden]> wrote:
> Hello
>
> The following piece of code :
<snip code>
> executes fine when compiled with mingw(gcc 4.5.2) and gcc 4.5.2(Ubuntu
> 11.04) but crashes when compiled with VS2010 & VS2008. The issue seems
> to be something about not being allowed to dereference an iterator toa
> string.
> Can anyone shed any light on this ?

The problem is the result_type of the transformer struct -- it ensures
the returned string is copied, despite the return value of the
operator() call. For details on why the it_works example works, see
http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/.

The it_fails example is due to the string being copied, the iterator
being retrieved, and the copied string being destroyed (all on the
it_fails initialization line). It appears that changing your
transformer declaration as follows will work:
struct transformer
{
   typedef const std::string& result_type;
   result_type operator()(
      const std::pair<const std::string, int> & thePair)const
   {
       return thePair.first;
   }
};

HTH,
Nate


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