lundi 1 août 2016

(Re)Assignment to a 2 dimensional std::vector

When implementing a class there is an internal object:

std::vector<std::vector<bool>> a;

The class initializes this object with the operator[] to assign false:

for(auto i = 0; i < limit; ++i) {
    for(auto j = 0; j < limit; ++j) {
        a[i][j] = false;
    }
}

During a private member function we update this object to reflect the current state:

a[object.x][object.y] = false;
a[object.new_x][object.new_y] = true;

Why does the compiler allow for the initialization but then says:

error: expression is not assignable

when I am reassigning the bit in the vector in the private member function?

I understand that there are some quirks to std::vector<bool> that allow for optimization and the vector holds bits not necessarily abool&. Does this play a role in the re-assignment error I am receiving?

Could it be that because object.x and likewise object.new_x and the respective y variant are of type int and the keyword auto within my initialization are choosing the correct type for me without realizing it? If this case is true then how does std::vector::size_type get chosen because isn't that the type which operator[] takes when performing accesses? But I would think that auto would be lazy and just create an int since I am initializing the variable to 0.

According to http://ift.tt/1EvbA9Y size_type is implementation defined, what does that mean exactly?, that the size_type depends on which version of the STL I am using?

Aucun commentaire:

Enregistrer un commentaire