In below code snippet i am getting segmentation fault while passing unique_ptr as as value. usually this is known issue with auto_ptr as due to ownership issue (Assignee pointer becomes NULL) it can't be accessed after assignment. but why i am facing the same issue with unique_ptr even though it is having move semantics.
what i understood auto_ptr use copy constructor and unique_ptr use move function to transfer the ownership. But in both cases assignee pointer becomes null then what is the significance of having move semantics here. I believe compiler should flash the error if unique_ptr is passed as a value like below statement as copy constructor is private in unique_ptr. could you throw some light on this regarding this behaviour.
unique_ptr<Test> a = p;//deleted function (copy constructor).
Here is the code snippet:
#include<iostream>
#include<memory>
using namespace std;
class Test
{
public:
Test(int a = 0 ) : m_a(a)
{
}
~Test( )
{
cout<<"Calling destructor"<<endl;
}
int m_a;
};
//***************************************************************
void Fun(std::unique_ptr<Test> p1 )
{
cout<<p1->m_a<<endl;
}
//***************************************************************
int main( )
{
std::unique_ptr<Test> p( new Test(5) );
Fun(std::move(p));
cout<<p->m_a<<endl;
return 0;
}