unique-ptr

std::unique_ptr<T[]> and custom allocator deleter

时间秒杀一切 提交于 2019-12-30 08:23:15
问题 I am trying to use std::unique_ptr<T[]> with custom memory allocators. Basically, I have custom allocators that are subclasses of IAllocator , which provides the following methods: void* Alloc( size_t size ) template<typename T> T* AllocArray( size_t count ) void Free( void* mem ) template<typename T> void FreeArray( T* arr, size_t count ) Since the underlying memory might come from a pre-allocated block, I need the special ...Array() -methods to allocate and free arrays, they allocate/free

Is unique_ptr<Derived> to unique_ptr<Base> up-casting automatic?

流过昼夜 提交于 2019-12-30 08:15:41
问题 I know it is possible that a derived class unique_ptr can take place where base class unique_ptr is required for polymorphic types. For example, while returning from function unique_ptr<Base> someFunction() { return make_unique<Derived>(new Derived()); } or passing to function as argument. // Function taking unique pointer void someOtherFunction(unique_ptr<Base>&& ptr) // Code calling this function someOtherFunction(std::move(ptrToDerived)); My question is: Is this upcasting always automatic?

Deleting derived classes in std::unique_ptr<Base> containers

青春壹個敷衍的年華 提交于 2019-12-30 07:31:35
问题 I'm a little confused. Basically, I've got 2 different resource managers (AudioLibrary and VideoLibrary) that both inherit from a shared BaseLibrary class. This base class contains references to both audio and video. Both audio and video inherit from a parent class called Media. I'm keeping the data in a map, filled with unique_ptr. But, to my surprise, I've discovered when these pointers are eventually deleted via .erase, only the base destructor for Media is called. I guess I've missed

Linker error when using unique_ptr in C++/CLI

巧了我就是萌 提交于 2019-12-30 06:25:26
问题 I'm currently converting my instances of auto_ptr to unique_ptr , but I'm hitting an issue. It works great in the C++ part of the code, but when doing it in my managed C++/CLI layer (the software uses both C# and C++) I get link errors. It compiles fine, but it breaks at link time. There were never any issues with auto_ptr . I'm currently using Visual Studio 2010. Does anybody know of any issues with using unique_ptr in C++/CLI? I've tried to sum up my issue in a piece of code below, but

Why std::unique_ptr does not permit type inference?

自古美人都是妖i 提交于 2019-12-30 03:55:28
问题 All is in the title. It would be easier to read/write the second line of my example since the type of the template argument is obvious: #include <memory> struct Foo{}; int main() { // redundant: good auto foo1 = std::unique_ptr<Foo>(new Foo()); // without explicitness: does not compile auto foo2 = std::unique_ptr(new Foo()); } Of course, if you want to use polymorphism, we could always write: auto base = std::unique_ptr<Base>(new Derived()); What is the reason of such a constraint? 回答1: This

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

此生再无相见时 提交于 2019-12-30 03:45:26
问题 OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a quirk of Clang or libc++ but I can't speak C++ standardese and I have no access to other compilers with C++11 support so I can't really check it, and I've searched the internet and Stack Overflow to the best of my ability without finding anything

Where's the proper (resource handling) Rule of Zero? [closed]

好久不见. 提交于 2019-12-29 07:33:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Here's an article that talks about an idiom named Rule of Zero. Here's an excerpt: class module { public: explicit module(std::wstring

Why does unique_ptr take two template parameters when shared_ptr only takes one?

不羁岁月 提交于 2019-12-28 04:54:14
问题 Both unique_ptr and shared_ptr accept a custom destructor to call on the object they own. But in the case of unique_ptr , the destructor is passed as a template parameter of the class , whereas the type of shared_ptr 's custom destructor is to be specified as a template parameter of the constructor . template <class T, class D = default_delete<T>> class unique_ptr { unique_ptr(T*, D&); //simplified ... }; and template<class T> class shared_ptr { template<typename D> shared_ptr(T*, D); /

How can I pass std::unique_ptr into a function

女生的网名这么多〃 提交于 2019-12-28 04:51:08
问题 How can I pass a std::unique_ptr into a function? Lets say I have the following class: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The following does not compile: void MyFunc(unique_ptr<A> arg) { cout << arg->GetVal() << endl; } int main(int argc, char* argv[]) { unique_ptr<A> ptr = unique_ptr<A>(new A(1234)); MyFunc(ptr); return 0; } Why can I not pass a std::unique_ptr into a function? Surely this is the primary purpose of the construct?

Should std::unique_ptr<void> be permitted

爱⌒轻易说出口 提交于 2019-12-28 04:22:08
问题 This is a very simple question. Consider the following code: #include <iostream> #include <memory> typedef std::unique_ptr<void> UniqueVoidPtr; int main() { UniqueVoidPtr p(new int); return 0; } Compiling with cygwin (g++ 4.5.3) with the following command g++ -std=c++0x -o prog file.cpp works just fine. However, compiling with the microsoft compiler (either VS 2010 or 2013) I get this error: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2067) : error C2070: 'void':