[webkit-dev] Using C++ constant local variables in WebKit

David Levin levin at chromium.org
Wed Nov 30 13:35:18 PST 2011


On Tue, Nov 29, 2011 at 6:19 PM, Darin Adler <darin at apple.com> wrote:

> On Nov 28, 2011, at 1:38 PM, David Kilzer wrote:
>
> In a discussion on Bug 71921<https://bugs.webkit.org/show_bug.cgi?id=71921>,
> Antti, Darin Adler and I started a discussion about using C++ constant
> pointers in WebKit.  Does the WebKit community have a consensus opinion on
> the matter?
>
>
> I thought we were discussing local variables in general, not pointer-typed
> ones specifically.
>
> * Pros
>   - Documents use of variable.
>
>
> I would say “documents the fact that the variable’s value is not changed”.
> I think it’s overstating things to say it “documents use”.
>
>   - Prevents misuse of variable in a later patch (by a different author)
> through enforcement of const-ness.
>
>
> Prevents one specific type of misuse: Setting the variable to another
> value. And that may not be misuse despite the fact that the original author
> didn’t plan on changing it.
>
>   - May help compiler optimize code.  (We weren't sure whether modern
> compilers do this on their own or not.)
>
>
> Doesn’t.
>
> * Cons
>   - Darin Adler doesn't ever recall fixing a bug in WebKit where a
> constant pointer would have helped.
>
>
> While true, not really a “con”; just weakens the “pro” argument above that
> this prevents misuse.
>
>   - Slightly more verbose syntax for constant pointers to a constant
> string (const char * const pointer;) or even a constant pointer to a
> mutable string (char * const pointer;).
>
>
> Not sure this is a con. Just stating what the C++ syntax.
>
> This is the con I am aware of:
>
>   - Less brief than omitting const.
>
> I’m not strongly opposed to using const more, but I am mildly opposed to
> it.
>

I can see your point of view. All of this seems to apply equally to
const_iterator as well. Are you mildly opposed to it as well? Or is
something different about it?

Minor plug (which is why I happened this think about this): An additional
current downside for const_iterator is that hashtable const_iterator has an
unfortunate issue where it can't be compared ==, != to an iterator which
isn't nice. https://bugs.webkit.org/show_bug.cgi?id=73370

dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20111130/3680bd73/attachment.html>


More information about the webkit-dev mailing list