std::for_each on a map containing unique_ptr

B

Brice Gagnage

Hi folks,

Given this piece of code:

std::map< int, std::unique_ptr< int > > map;

std::for_each( std::begin( map ), std::end( map ),
[&] ( const std::pair< int, std::unique_ptr< int > >& pair )
{
cout << pair.first;
} );

My compiler pops an error: for_each is trying to create a pair, thus taking
map elements' unique_ptr's as parameters. At this point, I don't understand
why for_each has to create a pair, since I'm asking explicitly for a const ref
in my lambda. Is there any way to make this map work with this algorithm ?
Any help appreciated.

Thanks in advance,
 
B

Brice Gagnage

My compiler pops an error: for_each is trying to create a pair, thus taking
map elements' unique_ptr's as parameters. At this point, I don't understand
why for_each has to create a pair, since I'm asking explicitly for a const ref
in my lambda. Is there any way to make this map work with this algorithm ?

I found the solution, the type of the parameter is incorrect. The lambda
should expect here a const std::pair said:

It seems that in the for_each implementation, the map's key is casted to const
to avoid the possibility of changing it inside the lambda. Since the
parameter I used was different, the compiler simply tried to create
similar pair with the key as const.

Anyway, I think the best practice here would have been to use the map
iterator reference_type instead of writing it myself.

Hope this helps,
 
V

Vianney Lançon

Le 04/04/2012 14:26, Brice Gagnage a écrit :
Hi folks,

Given this piece of code:

std::map< int, std::unique_ptr< int> > map;

std::for_each( std::begin( map ), std::end( map ),
[&] ( const std::pair< int, std::unique_ptr< int> >& pair )
{
cout<< pair.first;
} );

My compiler pops an error: for_each is trying to create a pair, thus taking
map elements' unique_ptr's as parameters. At this point, I don't understand
why for_each has to create a pair, since I'm asking explicitly for a const ref
in my lambda. Is there any way to make this map work with this algorithm ?
Any help appreciated.

Thanks in advance,

Hello Miaou

std::map::value_type is defined as
typedef pair<const Key, Type> value_type

so you are trying to cast a pair< Key, Type> to a const pair<const Key,
Type>&

So by changine the signature of the paire as std::pair< const int,...
it should fix the issue


Hamster

:)
 
J

Juha Nieminen

Brice Gagnage said:
Anyway, I think the best practice here would have been to use the map
iterator reference_type instead of writing it myself.

Or you could just do a: for(auto& elem: map) std::cout << elem.first;

(But yes, I understand that this solution only works as a substitute
for a for_each() that traverses the entire data container, not one that
traverses only a subrange, or for a completely different algorithm.)
 
R

red floyd

Or you could just do a: for(auto& elem: map) std::cout<< elem.first;

(But yes, I understand that this solution only works as a substitute
for a for_each() that traverses the entire data container, not one that
traverses only a subrange, or for a completely different algorithm.)

Well, if you're using C++11 features...

for (auto iter = map_var.begin(); iter != map_var.end(); ++iter)
{
// yada yada yada

}
 
B

Brice Gagnage

Le 04/04/2012 14:26, Brice Gagnage a écrit :
so you are trying to cast a pair< Key, Type> to a const pair<const Key,
Type>&

Yep, that was the point :) Thanks Mr. Hamster :)
 
B

Brice Gagnage

Well, if you're using C++11 features...

for (auto iter = map_var.begin(); iter != map_var.end(); ++iter)
{
// yada yada yada

}

Thanks for all your answers, I agree that I should use the range-based
for-loop, but my compiler doesn't support it yet :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top