mercredi 31 janvier 2018

Twice the same for loop: one compiles, the other does not

I have this piece of code to make a couple of strings lower case (see this SO post).

void some_free_standing_function(std::string solver, std::map<std::string, option_t> opts) {

    for (auto & c : solver) c = tolower(c);
    for (auto p : opts)
        for (auto & c : p.first)
            c = tolower(c);
}

The first range-based for seems to compile, the last one does not: Clang gives me error: cannot assign to variable 'c' with const-qualified type 'const char &'.

Why does the first one pass but not the second one, given that they are exactly the same?

Aucun commentaire:

Enregistrer un commentaire