Suppose i have something akin to this pseudocode:
std::optional<std::pair<double*, std::scope_lock<std::mutex> > > getDataTogetherWithLock() {
if (!some_ptr) {
return std::nullopt;
}
return some_ptr->getDataTogetherWithLock();//just returns std::pair<double*, std::scope_lock<std::mutex> >
}
This won't work, basicly if tried with real code will give an error about the return type cannot be converted to std::optional.
What's the best way to solve this conundrum ?
The problem is that
std::scoped_lockitself is neither movable nor copyable. So the return value of the member function cannot be copied/moved into the return value of the function you posted. The fix is simple. Use anstd::unique_lockthat is moveable. The following test code compiled for me on gccWith
std::scoped_lockit would fail to compile.