Open Bug 827611 Opened 12 years ago Updated 2 years ago

nsAutoPtr cannot be initialized with operator=

Categories

(Core :: XPCOM, defect)

x86
macOS
defect

Tracking

()

People

(Reporter: ehsan.akhgari, Unassigned)

Details

This doesn't work:

nsAutoPtr<X> x = new X();

This does:

nsAutoPtr<X> x(new X());

This is a pain.  Can we fix that?
I think this is sort of intentional... in that I think fixing that would require giving nsAutoPtr a copy-constructor that takes (const nsAutoPtr<T>&), which we definitely don't want, since the point of nsAutoPtr<T> is to be the *unique* owner of something.  (I think C++ auto_ptr should have the same issue... though maybe it doesn't anymore.)
Hmm, indeed it does look like that:

 ehsanakhgari  sparky  tmp  $ cat test.cpp 
#include <memory>
class X{};
void f() {
  std::auto_ptr<X> x = new X();
}
 ehsanakhgari  sparky  tmp  $ clang++ -c test.cpp 
test.cpp:4:20: error: no viable conversion from 'X *' to 'std::auto_ptr<X>'
  std::auto_ptr<X> x = new X();
                   ^   ~~~~~~~
/usr/include/c++/4.2.1/memory:198:7: note: candidate constructor not viable: no known conversion from 'X *' to 'std::auto_ptr<X> &' for 1st argument
      auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
      ^
/usr/include/c++/4.2.1/memory:348:7: note: candidate constructor not viable: no known conversion from 'X *' to 'auto_ptr_ref<element_type>' for 1st argument
      auto_ptr(auto_ptr_ref<element_type> __ref) throw()
      ^
/usr/include/c++/4.2.1/memory:211:9: note: candidate template ignored: failed template argument deduction
        auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
        ^
1 error generated.
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.