In bug 1193762, there's been work on eliminating the implicit
conversion from nsCOMPtr<T> to T*, at least for rvalues, to avoid the
safety problem discussed there.  The problem is that then you can't
pass an nsCOMPtr<T>&& to a function that wants a T*, even though this
is perfectly safe: the lifetime of the temporary is guaranteed to
outlast the function call.  The only solution hitherto devised other
than requiring lots of .get() is to invent a new type for refcounted
function parameters, which is awkward.

A new language feature could be used to solve this: allow conversion
operators to behave differently based on how the variable is declared.
For instance, it might convert differently if the source or
destination is a local variable, global/static variable, member
variable, or function parameter.  This would allow our problem to be
easily solved by defining something in nsCOMPtr like:

  operator T* [[parameter]]()&&;

while leaving the operator deleted for non-parameters.

If this can be declared on any method, or perhaps on the class itself,
it could also be used to enforce nsCOMPtr not being used as a global
or static variable.  Are there other places where this would be
useful?  I don't know if this makes sense to propose as a language
feature, but I thought it would be worth bringing up in case anyone
has more compelling use-cases.
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to