[webkit-dev] converting by constructor

Maciej Stachowiak mjs at apple.com
Mon May 17 13:30:43 PDT 2010


On May 17, 2010, at 12:39 PM, Chris Jerdonek wrote:

> Hi, I have a basic question.  What has been WebKit's stance on the use of the
> explicit keyword (for higher-level objects in particular)?  Do we prefer the
> looser API's that conversion by constructor affords, or do we more often
> discourage relying on conversion by constructor?
> 
> For comparison, the Google C++ style guide says the explicit keyword should
> almost always be used.  Note that I'm not suggesting that this be added to
> our style guide.

We like to use implicit conversion where it makes sense. We only use the "explicit" keyword to prevent specifically undesired implicit conversions.

As an example, the implicit conversions between String and AtomicString are very useful. They let the bindings generator write code from IDL without having to know whether the underlying C++ method takes a String or an AtomicString.

An example of a bad implicit conversion would be the Vector constructor that takes just a size_t. We don't want numbers passed as parameters to magically turn into Vectors, not only is that confusing but it would lead to ambiguous overloads. So we mark it explicit.

Regards,
Maciej



More information about the webkit-dev mailing list