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

Geoffrey Garen ggaren at apple.com
Mon Jan 6 13:49:53 PST 2014


Let me try to clarify with two more motivating examples:

(1) Nodes.cpp:

FunctionParameters::FunctionParameters(ParameterNode* firstParameter, unsigned size)
    : m_size(size)
{
    unsigned i = 0;
    for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam()) {
        auto pattern = parameter->pattern();
        pattern->ref();
        patterns()[i++] = pattern;
    }
}

If I had to describe this algorithm in English, I’d say, “Collect and retain all the [auto] from the list of parsed parameters.” I think that explanation would be stronger if “[auto]” were a concrete noun.

Does anybody prefer auto in this context? If so, why?

(2) ApplyStyleCommand.cpp:

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

I don’t understand why we’re &*’ing here. That’s a surprising idiom I haven’t seen before, which I would expect to be a no-op. My first question when reading this is, “What is the type of ‘child’, such that I would need to &* it?”. 

Is this "&*child” obvious to everyone else?

Thanks,
Geoff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-dev/attachments/20140106/74cf24e1/attachment.html>


More information about the webkit-dev mailing list