• ugo@feddit.it
    link
    fedilink
    arrow-up
    1
    ·
    14 hours ago

    To answer you: no, x += 1 cannot mutate my_var, because it’s a copy. If you wanted something else you would say auto& or auto*.

    And if the type of x is such that even a copy can mutate the original (e.g. maybe it’s a wrapper around an index with an overloaded operator+=() that mutates the original entry in the storage), you are probably working with a specialized system of some kind, since that breaks the semantics of the language, and hopefully you would know from context that you have such cases.

    And yes, in such an environment I would see “never use auto for this wrapper type” as a valid additional strategy to help ensure code correctness.

    But by and large my direct experience is that using auto “almost always”, as Herb Sutter puts it, is beneficial and doesn’t harm readability or understandability.