У меня есть оператор присваивания.
AP<T>& operator=(AP<T>& o) {Super::operator=(o); return *this; }
Когда я ожидаю, что компилятор g ++ найдет этот оператор для этого кода.
AP<System> to = System::Create();
Я получил ошибку компиляции.
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]
Почему это?MSVC компилирует этот код без проблем.
Источник выглядит следующим образом.
#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();
}
};
ДОБАВЛЕНО
При AP(const AP<T>& o) : Super(o) { }
я получил эти ошибки.
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
ДОБАВЛЕНО2
Я не знаю, что это лучшее решение, но этот код работает.
int main()
{
H::AP<H::System> tox(H::System::Create().release());
return 0;
}