c++ - How to properly forward unique_ptr? -


what proper way forward std::unique_ptr?

the following code uses std::move, thought considered practice, crashes clang.

class c {    int x; }  unique_ptr<c> transform1(unique_ptr<c> p) {     return transform2(move(p), p->x); // <--- oops! access p after move construction takes place, compiler-dependant }  unique_ptr<c> transform2(unique_ptr<c> p, int val) {     p->x *= val;     return p; } 

is there more robust convention making sure need p before transferring ownership next function via std::move? seems me using move on object , accessing provide parameter same function call common mistake make.

since not need access p after moved, 1 way p->x before move , use it.

example:

unique_ptr<c> transform1(unique_ptr<c> p) {     int x = p->x;     return transform2(move(p), x); } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -