[webkit-dev] On returning mutable pointers from const methods

Maciej Stachowiak mjs at apple.com
Sun Oct 28 06:12:13 PDT 2012

On Oct 25, 2012, at 12:48 PM, Andreas Kling <kling at webkit.org> wrote:

> So, I propose that we allow only these two signature formats for raw pointers:
> - const Foo* foo() const;
> - Foo* foo();
> Moreover, for methods that return references to objects where call sites are expected to take a strong reference, we should really be using PassRefPtr<Foo> as the return type.
> Thoughts? Objections? Am I missing something?

I am not sure a blanket rule is correct. If the Foo* is logically related to the object with the foo() method and effectively would give access to mutable internal state, then what you say is definitely correct. But if the const object is a mere container that happens to hold pointers, there's no particular reason it should turn them into const pointers. For example, it is fine for const methods of HashSet or Vector to return non-const pointers if that happens to be the template parameter type. In such cases, constness of the container should only prevent you from mutating the container itself, not from mutating anything held in the container, or else const containers of non-const pointers (or non-const types in general) would be useless.

I can see how a blanket rule may be easier to enforce, but we may need to actually make judgment calls about logical constness.


More information about the webkit-dev mailing list