unique-ptr

std::unique_ptr, deleters and the Win32 API

雨燕双飞 提交于 2019-12-17 09:33:48
问题 In VC2012, I want to create a mutex in a constructor using a unique pointer and a deleter, so that I don't need to create a destructor just to call CloseHandle. I would have thought that this would work: struct foo { std::unique_ptr<HANDLE, BOOL(*)(HANDLE)> m_mutex; foo() : m_mutex(CreateMutex(NULL, FALSE, NULL), CloseHandle) {} } but on compiling I get an error: error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,int (__cdecl *const &)(HANDLE)) throw()' : cannot convert parameter 1

std::unique_ptr, deleters and the Win32 API

安稳与你 提交于 2019-12-17 09:33:10
问题 In VC2012, I want to create a mutex in a constructor using a unique pointer and a deleter, so that I don't need to create a destructor just to call CloseHandle. I would have thought that this would work: struct foo { std::unique_ptr<HANDLE, BOOL(*)(HANDLE)> m_mutex; foo() : m_mutex(CreateMutex(NULL, FALSE, NULL), CloseHandle) {} } but on compiling I get an error: error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,int (__cdecl *const &)(HANDLE)) throw()' : cannot convert parameter 1

Copy constructor for a class with unique_ptr

心不动则不痛 提交于 2019-12-17 03:54:44
问题 How do I implement a copy constructor for a class that has a unique_ptr member variable? I am only considering C++11. 回答1: Since the unique_ptr can not be shared, you need to either deep-copy its content or convert the unique_ptr to a shared_ptr . class A { std::unique_ptr< int > up_; public: A( int i ) : up_( new int( i ) ) {} A( const A& a ) : up_( new int( *a.up_ ) ) {} }; int main() { A a( 42 ); A b = a; } You can, as NPE mentioned, use a move-ctor instead of a copy-ctor but that would

Copy constructor for a class with unique_ptr

南楼画角 提交于 2019-12-17 03:54:27
问题 How do I implement a copy constructor for a class that has a unique_ptr member variable? I am only considering C++11. 回答1: Since the unique_ptr can not be shared, you need to either deep-copy its content or convert the unique_ptr to a shared_ptr . class A { std::unique_ptr< int > up_; public: A( int i ) : up_( new int( i ) ) {} A( const A& a ) : up_( new int( *a.up_ ) ) {} }; int main() { A a( 42 ); A b = a; } You can, as NPE mentioned, use a move-ctor instead of a copy-ctor but that would

Differences between std::make_unique and std::unique_ptr with new

回眸只為那壹抹淺笑 提交于 2019-12-16 22:45:08
问题 Does std::make_unique have any efficiency benefits like std::make_shared ? Compared to manually constructing std::unique_ptr : std::make_unique<int>(1); // vs std::unique_ptr<int>(new int(1)); 回答1: The motivation behind make_unique is primarily two-fold: make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries. foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique

make_unique and perfect forwarding

一笑奈何 提交于 2019-12-16 19:58:39
问题 Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3); This hides the new nicely and only mentions the type once. Anyway, here is my attempt at an implementation of make_unique : template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std:

unique_ptr with vector: error: call to implicitly-deleted copy constructor of XXX

落花浮王杯 提交于 2019-12-13 15:24:57
问题 I want to manage a two dimensional array as below: std::vector<std::unique_ptr<int []>> vec(5, nullptr); vec[0] = std::make_unique<int []>(3); vec[1] = std::make_unique<int []>(4); ... However I get an error: error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr< int [], std::__1::default_delete< int []> >' 回答1: I believe the issue is with your vector constructor call ( 2: fill constructor ): std::vector<std::unique_ptr<int []>> vec(5, nullptr); Here, you're essentially

Add a deep copy ctor to std::unique_ptr<my_type>

偶尔善良 提交于 2019-12-13 13:11:20
问题 I would like to store some std::unique_ptr<my_type> into a std::vector . Since my_type provides a clone() method it's quite straightforward to make deep copies of my_type * . The point is how to extend std::unique_ptr preserving all its functionalities while adding the copy ctor and the assignment operator. Inheritance? Templace specialization? Could you please provide a code snippet? 回答1: The purpose of std::unique_ptr is for it to be unique i.e. it shouldn't be copyable. That's why they

Nonvirtual deleter with smart pointers

霸气de小男生 提交于 2019-12-13 04:56:35
问题 I was reading the latest Overload (link) and decided to test out the statement at page 8: shared_ptr will properly invoke B’s destructor on scope exit, even though the destructor of A is not virtual. I am using Visual Studio 2013, compiler v120: #include <memory> #include <iostream> struct A { ~A() { std::cout << "Deleting A"; } }; struct B : public A { ~B() { std::cout << "Deleting B"; } }; int main() { std::shared_ptr<A> ptr = std::make_shared<B>(); ptr.reset(); return 0; } This works as

Why doesn't this RAII move-only type properly emulate `std::unique_ptr`?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 04:49:01
问题 I took the code from this question and edited it to produce a segfault by explicitly calling the destructor of one of the move-constructed objects: using namespace std; struct Foo { Foo() { s = new char[100]; cout << "Constructor called!" << endl; } Foo(const Foo& f) = delete; Foo(Foo&& f) : s{f.s} { cout << "Move ctor called!" << endl; f.s = nullptr; } ~Foo() { cout << "Destructor called!" << endl; cout << "s null? " << (s == nullptr) << endl; delete[] s; // okay if s is NULL } char* s; };