[webkit-dev] Using C++ constant pointers (type_name * const) in WebKit
jarred at sencha.com
Mon Nov 28 14:46:48 PST 2011
On Mon, Nov 28, 2011 at 5:19 PM, Ryosuke Niwa <rniwa at webkit.org> wrote:
> On Mon, Nov 28, 2011 at 1:38 PM, David Kilzer <ddkilzer at webkit.org> 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?
>> * Pros
>> - Documents use of variable.
>> - Prevents misuse of variable in a later patch (by a different author)
>> through enforcement of pointer const-ness.
>> - May help compiler optimize code. (We weren't sure whether modern
>> compilers do this on their own or not.)
> Don't think so. As far as I know, C++ compilers ignore "const" for the
> purpose of code optimization because we can always re-cast it to non-const
> variable. On the other hand, modern optimizing compilers might do SSA-based
> propagation <http://gcc.gnu.org/news/ssa-ccp.html> regardless of
> constness of a variable.
> * Cons
>> - Darin Adler doesn't ever recall fixing a bug in WebKit where a
>> constant pointer would have helped.
>> - 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;).
> I'd argue against using constant pointers simply because I don't think the
> existing WebKit code uses it, and I don't see much benefit in comparison to
> pointers or references to const objects.
I would agree with Ryosuke. Other than the self-documenting benefits, it's
not as valuable as ref-to-const and pointer-to-const when one is passing
the variable around into different functions.
The bugs it would help prevent at compile-time are few and far between; too
much so to start enforcing the style right now. But technically I don't
see the harm in letting them slip in where it makes sense. From a
readability standpoint, they are very verbose and make it a little more
confusing to understand the intention during quick scans (ptr-to-const vs
const-ptr) - especially since ptr-to-const can be pre- or post- type. *const
char** is the same as *char const** which is not the same as *char* const*.
I will say that in all the code I've written and read, const ptrs were
rarely used, if at all.
> - Ryosuke
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
Jarred Nicholls, Senior Software Architect
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the webkit-dev