【发布时间】:2021-03-09 21:52:06
【问题描述】:
假设我有一个动态分配的对象foo,它有一个std::mutex 成员:
#include <mutex>
#include <memory>
class foo {
public:
foo()=default;
~foo();
private:
std::mutex lock;
};
int main(){
std::unique_ptr<foo> a = std::make_unique<foo>(foo());
return 0;
}
我尝试过使用智能指针,但没有任何意义:
rog.cc:4:7: error: use of deleted function 'std::mutex::mutex(const std::mutex&)'
In file included from /opt/wandbox/gcc-head/include/c++/11.0.0/mutex:43,
from prog.cc:1:
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/std_mutex.h:94:5: note: declared here
94 | mutex(const mutex&) = delete;
| ^~~~~
我必须使用原始指针来管理这个对象吗?
【问题讨论】:
标签: c++ c++11 unique-ptr