I have a assignment operator.
AP<T>& operator=(AP<T>& o) {Super::operator=(o); return *this; }
When I expect the g++ compiler finds this operator for this code.
AP<System> to = System::Create();
I got compilation error.
no matching function for call to ‘H::AP<H::System>::AP(H::AP<H::System>)’
ap.cpp:13: note: candidates are: H::AP<T>::AP(H::AP<U>&) [with U = H::System, T = H::System]
ap.cpp:12: note: H::AP<T>::AP(H::AP<T>&) [with T = H::System]
ap.cpp:11: note: H::AP<T>::AP(T*) [with T = H::System]
Why is this? MSVC compiles this code without problem.
The source is as follows.
#include <memory>
namespace H {
template<typename T>
class AP : public std::auto_ptr<T>
{
typedef std::auto_ptr<T> Super;
public:
template<typename U> AP<T>& operator=(AP<U>& o) { Super::operator=(o.release()); return *this; }
AP<T>& operator=(AP<T>& o) { Super::operator=(o); return *this; }
};
class System {
public:
static AP<System> Create();
};
AP<System> System::Create()
{
AP<System> a(new System());
return a;
}
int main()
{
AP<System> to = System::Create();
}
};
ADDED
With AP(const AP<T>& o) : Super(o) { }
, I got those errors.
ap.cpp: In copy constructor ‘H::AP<T>::AP(const H::AP<T>&) [with T = H::System]’:
ap.cpp:33: instantiated from here
ap.cpp:12: error: passing ‘const H::AP<H::System>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = H::System, _Tp = H::System]’ discards qualifiers
ADDED2
I don't know it's the best solution, but this code seems to work.
int main()
{
H::AP<H::System> tox(H::System::Create().release());
return 0;
}