smart-pointers

How to forward declare a class to be used in a standard container of unique_ptr

孤者浪人 提交于 2019-12-11 02:25:39
问题 Is it possible to avoid having full class definition visible when using it in standard container of smart pointers? For example I can't get the following to compile: #include <memory> #include <map> class Foo; class Bar { public: Bar(); std::map<int, std::unique_ptr<Foo>> myMap; }; The Clang compiler seems to insist on having full definition of Foo available when compiling Bar . Is there a technique I could use to avoid having to include Foo.h? Edit1: error: invalid application of 'sizeof' to

How to implement deep copy feature in some smart pointer?

﹥>﹥吖頭↗ 提交于 2019-12-11 01:57:12
问题 unique_ptr is quite useful. However, it is not copyable. If virutal clone (deep copy) methods are provided for its pointed class, I think it will become more useful. Is it necessary or any better way to implement it? Any similar smart pointer exist in some library? Here is a version template<class T> class deep_ptr: private unique_ptr<T> { public: using unique_ptr<T>::operator *; using unique_ptr<T>::operator ->; using unique_ptr<T>::operator bool; using unique_ptr<T>::release; using unique

smart pointers - why use them, and which to use?

穿精又带淫゛_ 提交于 2019-12-11 00:54:30
问题 general questions Now I've been reading quite a bit about smart pointers, and shared pointers seem like "perfect" in many cases. However I also read about cyclical reference or something like that? Where shared_ptr can't be used? I'm having a difficult time undestanding this, can someone give a trivial example showing this? Also I'm really wondering, what do weak_ptr's provide that normal pointers don't? - As they don't increase the reference count they give no guarantee that the memory they

How can I obtain an &A reference from a Rc<RefCell<A>>?

左心房为你撑大大i 提交于 2019-12-11 00:47:48
问题 I have design issue that I would like solve with safe Rust that I haven't been able to find a viable solution. I can't use a RefCell because you can't get a & reference to the data, only Ref / RefMut . Here is a simplified example with irrelevant fields / methods removed use std::cell::RefCell; use std::rc::Rc; struct LibraryStruct {} impl LibraryStruct { fn function(&self, _a: &TraitFromLibrary) {} } trait TraitFromLibrary { fn trait_function(&self, library_struct: LibraryStruct); } // I don

shared_ptr for a raw pointer argument

不羁的心 提交于 2019-12-10 17:26:23
问题 When the function requires a char*, can you pass in a shared_ptr? I'm reading in a whole text file (length = 100), and want to store the char's into a char[] array. The naive way I used was this: ifstream dictFile(fileName); size_t fileLength = 100; char* readInBuffer(new char[fileLength]); dictFile.read(readInBuffer, fileLength); //processing readInBuffuer.............. delete[] readInBuffer; dictFile.close(); Of course there is memory leak if an exception is thrown before the delete[]

Getting into smart pointers, how to deal with representing ownership?

只愿长相守 提交于 2019-12-10 16:04:13
问题 i've made a dynamic graph structure where both nodes and arcs are classes (i mean arcs are an actual instance in memory, they are not implied by an adjacency list of nodes to nodes). Each node has a list of pointers to the arcs it's connected to. Each arc has 2 pointers to the 2 nodes it's connecting. Deleting a node calls delete for each of its arcs. Each arc delete removes its pointer from the arcs lists in the 2 nodes it connects. Simplified: ~node() { while(arcs_list.size()) { delete arcs

Vector with references to unique_ptr

拈花ヽ惹草 提交于 2019-12-10 14:55:15
问题 I have a collection of unique_ptr s. Here i want to take some of them and return them to the caller. The caller only needs to read the content, so i wanted to use constant references. But I'm not sure how to do this with unique_ptr s. Here is some code I used to do this with raw pointers: class entry { }; vector<entry*> master; const vector<entry*> get_entries() { vector<entry*> entries; // peusocode, master contains all entries. // only some entries are copied, filtered by some predicate

Avoid object slicing for non-virtual destructors

筅森魡賤 提交于 2019-12-10 14:51:59
问题 I am writing code for smart pointers as an exercise. Using tutorials online (1 , 2) I have developed a normal smart-pointer class with reference counting. The problem is I am unable to figure out the following: when the smart pointer detects that no more references exist to a particular object, it must delete the object via a pointer to the original type, even if the template argument of the final smart pointer is of a base type. This is to avoid object slicing for non-virtual destructors.

Read-write thread-safe smart pointer in C++, x86-64

余生长醉 提交于 2019-12-10 14:44:41
问题 I develop some lock free data structure and following problem arises. I have writer thread that creates objects on heap and wraps them in smart pointer with reference counter. I also have a lot of reader threads, that work with these objects. Code can look like this: SmartPtr ptr; class Reader : public Thread { virtual void Run { for (;;) { SmartPtr local(ptr); // do smth } } }; class Writer : public Thread { virtual void Run { for (;;) { SmartPtr newPtr(new Object); ptr = newPtr; } } }; int

How to reduce the implementation code of lots of wrapper classes?

血红的双手。 提交于 2019-12-10 13:33:38
问题 I am developing a library with some classes, let's call them C1, C2 and ... Cn . Each of these classes realize some interfaces, i.e. I1, I2, ... Im. (n > m). The relationship between objects in the library is complex and I have to provide some API for my library users to access these objects using smart pointers. After some discussions, I found that returning shared pointers to the library users is not a good idea, because in that case I cannot make sure that the object can be removed