c++ - std::unique_ptr usage -


std::unique_ptr<int> p1(new int); std::unique_ptr<int> p2(new int); p2=p1; 

it seems here p1 no longer "unique" since p2 refer also

it legal c++ ? unique_ptr have copy_semantics ? if no, , if has move semantics, p1 set null after assign p2 ?

edit:

ok correct version is

 p2=std::move(p1) 

according that, after assign, p1 not valid ? , difference auto_ptr here? more safe explictly specfiy transfer of ownership implicitly case auto_ptr guess

std::unique_ptr non-assignable , non-copyable. need use std::move();

so

p1 = std::move(p2); 

have here more info.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -