Boost logo

Boost :

Subject: Re: [boost] [range] Problem with adaptors::map_keys
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2010-11-29 13:15:59


On 11/28/2010 11:44 PM, Alexander Fokin wrote:
>> Three things:
>> - value_type should just be pair< Key, T> (no reference or const
>> qualifications on Key or T)
>> - reference should be pair< Key const&, T& >, with T possibly
>> const-qualified (as you've done)
>> - you can (if you want) use iterator_adaptor instead of iterator_facade to
>> automatically get a correct equal, increment, and decrement.
> I have modified the code as you have suggested and now it compiles OK.
> The modified code is available at http://codepad.org/AJFIVbdY.
>
> However, there is one thing I'm concerned about. Consider the example
> at http://codepad.org/plyTCBpx. The following lines do not call the
> copy constructor of class A:
> BOOST_FOREACH(auto pair, m) {
> const A&key = pair.first;
> }

Good.

> However, when I use range adaptors, the copy constructor is called.
> That was exactly what I was trying to avoid by using references in
> value_type.

It's certainly a reasonable thing to avoid. I'm not sure why A's copy
constructor would be called. Perhaps you should run it through a
debugger and set a break point in A's copy constructor. I don't have Qt
installed so it would take me a little more effort to help you more than
that ;)

It could be the case that the iterator for the range produced by (m |
map_keys) returns by value upon dereferencing, rather than by reference.
  Might be worth checking...

> I guess I'll try to fix this by myself and then file a patch.

Good luck!

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk