'Will ObjC setter automatically copy a C++ object passed as a parameter when get called?
I recently read about a std::unique_ptr as a @property in objective c and the suggestion to store a unique_ptr in ObjC as a property is as following:
-(void) setPtr:(std::unique_ptr<MyClass>)ptr {
_ptr = std::move(ptr);
}
My question is in ObjC, does the parameter get copied in this case? Because if that happens, unique_ptr shall never be declared as a property right?
Solution 1:[1]
I would not expect that code to compile as ptr is being passed by value there.
Better would be:
-(void) setPtr:(std::unique_ptr<MyClass>) &&ptr {
_ptr = std::move(ptr);
}
Edit: Thinking about it, that might not compile either. I don't know if Objective_C understands passing parameters by reference, rvalue or otherwise. But if it doesn't, this should work:
-(void) setPtr:(std::unique_ptr<MyClass>) *ptr {
_ptr = std::move(*ptr);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
