0
std::vector<std::vector<bool>> grid(4, std::vector<bool>(4, true));
std::for_each(grid.begin(), grid.end(), [](auto& row) {
    std::for_each(row.begin(), row.end(), [](auto& free) {
        free = false;
    });
});

I want to set a true initialized 4x4 matrix to false using std::for_each. Why doesn't it compile with a C2664 error?

2
  • 1
    std::vector<bool> is problematic. It does not actually store elements of type bool. If you use any other type, it should work. Commented May 10 at 13:00
  • 1
    Few people memorize error codes. What is the message that goes along with "C2664"? Commented May 10 at 15:03

1 Answer 1

4

std::for_each takes its iterator parameters by value, and your lambda captures the reference to row, but not the elements inside. The innermost lambda is trying to modify bool values via auto&, but due to proxy reference issues in std::vector<bool>, it's not working as expected.

std::vector<bool> is not a normal container of bool. It's optimized to pack bits, and its operator[] returns a proxy object, not a bool&. That proxy causes trouble with templated code like std::for_each.

How to solve this issue?

There is two way's to fix the mentioned issue:

  • using a traditional loop;

  • access via indexes (if you must use std::vector<bool>).

First solution:

std::vector<std::vector<bool>> grid(4, std::vector<bool>(4, true));

// Set all values to false
for (auto& row : grid) {
    for (auto cell = row.begin(); cell != row.end(); ++cell) {
        *cell = false;
    }
}

Second solution:

std::for_each(grid.begin(), grid.end(), [](std::vector<bool>& row) {
    for (size_t i = 0; i < row.size(); ++i) {
        row[i] = false; // Avoids the proxy reference issue
    }
});

To be frank, I recommend to avoid std::vector<bool> for mutability-critical logic. It’s a design compromise in the STL and causes more headaches than it’s worth.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.