On 7/22/2013 1:59 AM, Justin Lebar wrote:
It seems really dangerous that there is an implicit conversion from a strong
ref ptr to a weak pointer. With C++11, you can thankfully require this
conversion to be explicit which should alleviate your concern.
Wouldn't disallowing this implicit conversion break code which does

   void UseFoo(nsIFoo* foo);

   nsCOMPtr<nsIFoo> foo;
   UseFoo(foo);

?  That is an extremely common idiom in our code.

By "extremely common idiom", what is really meant is "this is basically how we expect XPIDL bindings to work in all C/C++ code."

Like I say, maybe there's a way to allow lvalue nsRefPtr<T> to convert
to T*, but disallow the conversion for rvalue nsRefPtr<T>.  That would
be reasonable, I think.

I think N2439 gives us a way out here:
$ cat test.cpp
template <typename T>
struct RefPtr {
    T *inner;
    operator T*() & { return inner; }
};

struct Foo {};

void bar(Foo *x) {}
RefPtr<Foo> baz() { return RefPtr<Foo>(); }
void qux() {
    RefPtr<Foo> yz = baz();
    bar(yz);
    Foo *asdf = baz();
}

$ clang++ -std=c++11 test.cpp
test.cpp:15:10: error: no viable conversion from 'RefPtr<Foo>' to 'Foo *'
    Foo *asdf = baz();
         ^      ~~~~~
test.cpp:5:5: note: candidate function not viable: no known conversion from
      'RefPtr<[...]>' to 'RefPtr<[...]>' for object argument
    operator T*() & { return inner; }
    ^
1 error generated.

A ref-qualifier on the operator conversion allows it to exist only for lvalue *this values; for better diagnostics, we could add a &&-ref qualifier to the conversion operator that is deleted.

Note, however, that N2439 has minimum requirements of GCC 4.8.1 and is not planned to reach MSVC 2013. It is listed in the "where we're going" slides of the MSVC C++11/C++14 conformance talk under the category of things which would probably make it into the next CTP post-2013.

--
Joshua Cranmer
Thunderbird and DXR developer
Source code archæologist

_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to