smart-pointers

How can unique_ptr have no overhead if it needs to store the deleter?

[亡魂溺海] 提交于 2019-12-03 22:10:45
First take a look at what C++ Primer said about unique_ptr and shared_ptr : $16.1.6. Efficiency and Flexibility We can be certain that shared_ptr does not hold the deleter as a direct member, because the type of the deleter isn’t known until run time. Because the type of the deleter is part of the type of a unique_ptr , the type of the deleter member is known at compile time. The deleter can be stored directly in each unique_ptr object. So it seems like that the shared_ptr does not have a direct member of deleter, but unique_ptr does. However, the top-voted answer of another question says: If

Boost shared_ptr: How to use custom deleters and allocators

和自甴很熟 提交于 2019-12-03 21:35:05
Free function allocate_shared can be used with any standard compliant allocator. But what about shared_ptr's constructor and reset method. template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); template<class Y, class D, class A> void reset(Y * p, D d, A a); The manual says that D should provide a call operator which will be used to delete the pointer and A must be a standard compliant allocator. If so, why D is needed? Can't A do both allocation and delocation? Don't you think that the requirement to provide a deleter for every custom allocator makes the above methods pretty much

How to enforce users to create objects of class derived from mine with “new” only?

邮差的信 提交于 2019-12-03 20:46:03
To implement reference counting we use an IUnknown -like interface and a smart pointer template class. The interface has implementation for all the reference-count methods, including Release() : void IUnknownLike::Release() { if( --refCount == 0 ) { delete this; } } The smart pointer template class has a copy constructor and an assignment operator both accepting raw pointers. So users can do the following: class Class : public IUnknownLike { }; void someFunction( CSmartPointer<Class> object ); //whatever function Class object; someFunction( &object ); and the program runs into undefined

Smart pointers + cycles + “->”

南楼画角 提交于 2019-12-03 17:26:05
问题 Sometimes I'm really sure that I want to have circular dependence of pointers, and every object on cycle should be able to use his pointer (so it can't be weak_ptr). My question is: Does this mean that I have bad design? What if I want to implement graph? Can I use smart pointers? In graphs there are cycles, but with weak_ptr I can't use "->". What can I do? I read some articles, reference and topics on StackOverflow, but it looks like I still don't get smart pointers. Really, why doesn't

C++ Iterating through a vector of smart pointers

我的未来我决定 提交于 2019-12-03 17:20:33
问题 i have a class that has this function: typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; class Control{ public: //other functions RenderVectors(SDL_Surface*destination, sp_PrimShapeBase); private: //other vars vector<sp_PrimShapeBase> LineVector; }; //the problem of the program void Control::RenderVectors(SDL_Surface*destination, sp_PrimShapeBase){ vector<sp_PrimShapeBase>::iterator i; //iterate through the vector for(i = LineVector.begin(); i != LineVector.end(); i ++ ){ //access a

Dependency injection in C++11 without raw pointers

醉酒当歌 提交于 2019-12-03 16:58:45
问题 I often use the "dependency injection" pattern in my projects. In C++ it is easiest to implement by passing around raw pointers, but now with C++11, everything in high-level code should be doable with smart pointers. But what is the best practice for this case? Performance is not critical, a clean and understandable code matters more to me now. Let me show a simplified example. We have an algorithm that uses distance calculations inside. We want to be able to replace this calculation with

Is this a valid use of intrusive_ptr?

China☆狼群 提交于 2019-12-03 16:18:14
Within my code I follow two rules when it comes to intrusive_ptrs: Passing a raw pointer by value implies that the raw pointer is guaranteed to be valid during the lifetime of that function. If the raw pointer is to be stored and used beyond the lifetime of the function, it should be stored in an intrusive_ptr. Many Internet commenters have written that shared_ptr should be preferred over intrusive_ptr except when working with third party code. However, intrusive_ptr obviates passing around smart pointers since you can create an intrusive_ptr from a raw pointer, as when the object is needed

Pimpl idiom using shared_ptr working with incomplete types

只愿长相守 提交于 2019-12-03 15:41:10
I'm reading Effective Modern C++ by Scott Meyers and he's discussing the use of the pimpl idiom and pointing to the implementation class with unique_ptr , but there is an issue of special member functions (such as destructors) requiring the type to be complete. This is because unique_ptr 's default deleter statically asserts whether the type to be deleted is complete, before delete p is used. So any special member functions of the class must be defined in the implementation file (rather than being compiler-generated), after the implementation class has been defined. At the end of the chapter,

C++ Smart Pointer performance

拟墨画扇 提交于 2019-12-03 15:22:18
问题 How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers in terms of time and memory? Is using bare pointers better for performance intensive parts of gaming/embedded systems? Would you recommend using bare pointers or smart pointers for performance intensive components? 回答1: Dereferencing smart pointers is typically trivial, certainly for boost in release mode. All boost checks are at compile-time. (Smart pointers could in theory do smart stuff

Why is there no boost::copy_on_write_ptr?

时光怂恿深爱的人放手 提交于 2019-12-03 14:37:54
I just saw this nice copy-on-write pointer implementation. It looks pretty generic and useful, so my question is: Is such a class contained in any of the C++ toolkits (boost, loki, etc.)? If not, I'd really like to know why because it is a really useful idiom and apparently a generic implementation seems doable (like the one I linked to). There was a lot of debate over the possibility, and at least one suggested version of what eventually came out as auto_ptr was for a reference counted COW pointer. Unfortunately, the time for COW has mostly passed. Making a COW pointer (or COW-whatever)