copy-constructor

Why is std::mutex neither copyable nor movable? [duplicate]

帅比萌擦擦* 提交于 2020-12-26 05:18:06
问题 This question already has answers here : Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted? (1 answer) Move constructor for std::mutex (2 answers) Closed 6 months ago . Could somebody tell the reasons why std::mutex is neither copyable nor movable? Somebody told me that it has some relationship to avoid resource waste. Why the copy constructor of std::mutex should be marked as deleted? If not, is there any potential problem? Its copy

Why is std::mutex neither copyable nor movable? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2020-12-26 05:16:35
问题 This question already has answers here : Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted? (1 answer) Move constructor for std::mutex (2 answers) Closed 6 months ago . Could somebody tell the reasons why std::mutex is neither copyable nor movable? Somebody told me that it has some relationship to avoid resource waste. Why the copy constructor of std::mutex should be marked as deleted? If not, is there any potential problem? Its copy

Singleton implementation - why is a copy constructor needed?

回眸只為那壹抹淺笑 提交于 2020-08-04 08:56:37
问题 I found this code online for the singleton design pattern: class Foo { public: static Foo& getInstance() { static Foo instance; return instance; } private: Foo() {}; Foo(Foo const&); Foo& operator=(Foo const&); } I don't understand why the constructor Foo(Foo const&); and the Foo& operator=(Foo const&); are both needed. Can someone explain to me please? 回答1: Wouldn't you want the following code to fail? int main() { // Utilizes the copy constructor Foo x = Foo::getInstance(); Foo y = Foo:

Implicit move vs copy operations and containment

前提是你 提交于 2020-07-05 12:27:25
问题 I am struggling to understand implicit move operations when a class has a member whose move operations were not defined: int main() { struct A // no move: move = copy { A() = default; A(const A&) { cout << "A'copy-ctor\n"; }; A& operator=(const A&) { cout << "A'copy-assign\n"; return *this; } }; struct B { B() = default; A a; // does this make B non-moveable? unique_ptr<int> upi; // B(B&&) noexcept = default; // B& operator=(B&&)noexcept = default; }; A a; A a2 = std::move(a); // ok use copy