Cron Merdek
Cron Merdek

Reputation: 1124

for_each in place modification with lambda of vector<bool>

When I try to use for_each to change vector in place:

vector<bool> sub_accs_ind(vec_ids_.size());
std::for_each(sub_accs_ind.begin(), sub_accs_ind.end(), [](bool& b){ b = false; });

It results in error /usr/include/c++/4.8/bits/stl_algo.h:4417:14: error: no match for call to ‘(main(int, char* const*)::__lambda3) (std::_Bit_iterator::reference)’ __f(*__first);

Could you please guide me about what is wrong here?

Upvotes: 1

Views: 582

Answers (1)

Quentin
Quentin

Reputation: 63174

std::vector<bool> is not a container !

Its iterators don't return a bool&, but a proxy instance. In C++11, you have to name its type explicitly:

std::for_each(
    sub_accs_ind.begin(),
    sub_accs_ind.end(),
    [](decltype(sub_accs_ind)::reference b){ b = false; }
);

C++14 would allow you to declare the parameter as auto&& to accomodate both real references and proxies.

Upvotes: 8

Related Questions