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

Cosmin Truta ctruta at gmail.com
Fri Jan 3 10:55:07 PST 2014


On Fri, Jan 3, 2014 at 1:11 PM, Alexey Proskuryakov <ap at webkit.org> wrote:
>> auto sourceDescendants = descendantsOfType<HTMLSourceElement >(*this));
> So do we need auto or auto& here? I would know how to answer this
question immediately if it was an explicit type, but I don't know how to
answer it with auto in this particular case.
> [...]
> One shouldn't need even basic familiarity with style system to see
whether code leaks, introduces refcount thrashing, or copies too much.
Using auto tends to make such mistakes much more opaque.

How about preserving not only the ptrs and refs, but also the
CV-qualifiers, when applicable?

Example 1:
auto* cache = new Cache; // right
auto cache = new Cache; // wrong, should be auto*
Cache* cache = new Cache; // wrong, should be auto*

Example 2:
const ClipboardFormatMap& formatMap = getClipboardMap(); // right
const auto& formatMap = getClipboardMap(); // also right
auto& formatMap = getClipboardMap(); // wrong, missing const
auto formatMap = getClipboardMap(); // wrong, missing const and &

Sincerely,
Cosmin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-dev/attachments/20140103/2a99b1e5/attachment.html>


More information about the webkit-dev mailing list