No, you don't need to use new or smart pointers here. This line will work correctly and the assignment will persist after the function goes out of scope.
Here's why: std::pair<int, int>(this->thread_id(), node->GetSig()) creates a temporary pair object, and the assignment operator copies its contents into E->bbt_->threadStopRequest. Since threadStopRequest appears to be a member variable of the object that E->bbt_ points to, the copied data lives in that object's memory, not on the current function's stack.
The key distinction is:
- The temporary
std::pair object created by the constructor will be destroyed when the function exits
- But the contents of that pair have already been copied into
threadStopRequest via the assignment operator
threadStopRequest itself lives in the object pointed to by E->bbt_, so it persists as long as that object exists
Using new would actually be problematic here because:
- You'd be creating a
std::pair* (pointer to pair) but assigning it to what appears to be a std::pair member
- You'd introduce a memory leak since nothing would delete the dynamically allocated pair
- It's unnecessary complexity
Your current code is the correct approach. The assignment copies the pair's values into the member variable, and those values will persist as long as the containing object exists.