[webkit-dev] When to use "auto"? (I usually consider it harmful)

Darin Adler darin at apple.com
Mon Jan 6 15:33:13 PST 2014


On Jan 6, 2014, at 1:49 PM, Geoffrey Garen <ggaren at apple.com> wrote:

>     auto children = elementChildren(*dummySpanAncestor);
>     for (auto child = children.begin(), end = children.end(); child != end; ++child) {
>         if (isSpanWithoutAttributesOrUnstyledStyleSpan(&*child))
>             toRemove.append(&*child);
>     }

This should be written with a C++11 loop:

    for (auto& child : elementChildren(*dummySpanAncestor)) {
        if (isSpanWithoutAttributesOrUnstyledStyleSpan(&child))
            toRemove.append(&child);
    }

Not sure if you would say that helps or hurts readability. I could easily imagine either Element& or auto&; in cases like this where we don’t have to name the iterator type we don’t need auto as much as in the non-C++-11-loop where the iterator type strongly motivates it.

— Darin


More information about the webkit-dev mailing list