我有以下代碼:爲什麼shared_ptr <T>期望T中的複製/移動構造函數?
#include <memory>
using namespace std;
template<typename U> class A;
template<typename U>
class B
{
private:
shared_ptr<const A<U>> _a;
public:
B (shared_ptr<const A<U>> __a) {
_a = __a;
}
};
template<typename U>
class A
{
public:
B<U> foo() const {
return { make_shared<const A<U>> (this) };
}
};
int
main()
{
A<int> a;
B<int> b = a.foo();
return 0;
}
G ++ 4.8.0和鏘3.3svn報告,類A
還沒有一個複製或移動的構造。對於 例如,G ++打印以下消息:
/home/alessio/Programmi/GCC/include/c++/4.8.0/ext/new_allocator.h:120:4: error: no matching function for call to ‘A<int>::A(const A<int>* const)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/home/alessio/Programmi/GCC/include/c++/4.8.0/ext/new_allocator.h:120:4: note: candidates are:
prova.cpp:19:7: note: constexpr A<int>::A()
class A
^
prova.cpp:19:7: note: candidate expects 0 arguments, 1 provided
prova.cpp:19:7: note: constexpr A<int>::A(const A<int>&)
prova.cpp:19:7: note: no known conversion for argument 1 from ‘const A<int>* const’ to ‘const A<int>&’
prova.cpp:19:7: note: constexpr A<int>::A(A<int>&&)
prova.cpp:19:7: note: no known conversion for argument 1 from ‘const A<int>* const’ to ‘A<int>&&’
的原因是什麼?
'A'不是一個類。這是一個模板。 – 2013-05-08 12:48:22