Bunny objects exercise; MSVC bug?

I decided to try implementing the list of bunny objects from this list of exercises (http://www.cplusplus.com/articles/N6vU7k9E/ ).

Here's the code.
http://pastebin.com/WSezE4fL

It seems fine on gcc, but on Windows with MSVC 2015, the bunny names spontaneously become empty strings.

http://i.imgur.com/BA0OngS.png

So my first question is, can anyone else reproduce and confirm this issue? And finally, why does it happen and how can I fix it? For the life of me I can't figure out why the names go blank. And that it only seems to happen with MSVC makes me think it might be a compiler issue.
Last edited on
I don't know if this is related to the problem you describe but I noticed that you use remove_if without calling erase.

1
2
3
remove_if(bunnies.begin(), bunnies.end(), [] (const Bunny& bunny) {
    return !is_alive(bunny);
});

remove_if will not change the size of the container. Instead it returns an iterator to the new end of the sequence. You can remove the elements by passing this iterator to the erase function.

1
2
3
bunnies.erase(remove_if(bunnies.begin(), bunnies.end(), [] (const Bunny& bunny) {
    return !is_alive(bunny);
}), bunnies.end());

It's called the erase–remove idiom.
https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
That seems to have done the trick. Thanks! It seems I hadn't read the docs for remove_if closely enough, and instead I naively used it how I *assumed* it would work. It turns out remove_if's behavior is actually similar to partition.
Topic archived. No new replies allowed.