smart-pointers

Lock-free Reference counting and C++ smart pointers

随声附和 提交于 2019-12-03 07:32:54
问题 In general, most widely known implementations of reference-counting smart ptr classes in C++, including the standard std::shared_ptr , use atomic reference counting, but do not provide atomic access to the same smart ptr instance. In other words, multiple threads may safely operate on separate shared_ptr instances which point to the same shared object, but multiple threads cannot safely read/write instances of the same shared_ptr instance without providing some kind of synchronization such as

Should I use C++11 emplace_back with pointers containters?

浪子不回头ぞ 提交于 2019-12-03 06:57:49
问题 Having a usual Base -> Derived hierarchy, like: class Fruit { ... }; class Pear : Fruit { ... }; class Tomato : Fruit { ... }; std::vector<Fruit*> m_fruits; Has it sense (e.g: it has better performance) to use emplace_back instead of push_back? std::vector::emplace_back( new Pear() ); std::vector::emplace_back( new Tomato() ); 回答1: Pointers are scalar types and therefore literal types, and so copy, move and emplace construction (from an lvalue or rvalue) are all equivalent and will usually

Why does unique_ptr have the deleter as a type parameter while shared_ptr doesn't?

狂风中的少年 提交于 2019-12-03 06:28:03
问题 The std::unique_ptr template has two parameters: the type of the pointee, and the type of the deleter. This second parameter has a default value, so you usually just write something like std::unique_ptr<int> . The std::shared_ptr template has only one parameter though: the type of the pointee. But you can use a custom deleter with this one too, even though the deleter type is not in the class template. The usual implementation uses type erasure techniques to do this. Is there a reason the

C++ Iterating through a vector of smart pointers

a 夏天 提交于 2019-12-03 06:22:12
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 certain function of the class PrimShapeBase through the smart //pointers (i)->RenderShape(destination);

Dependency injection in C++11 without raw pointers

可紊 提交于 2019-12-03 05:57:28
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 different distance metrics (Euclidean, Manhattan, etc.). Our goal is to be able to say something like:

Is there a C++/CLI smart pointer project (e.g. scoped_ptr)?

感情迁移 提交于 2019-12-03 05:35:30
Is there a C++/CLI RAII smart pointer class for containment of a native pointer in a managed type? Just wondering, before I go write my own clr_scoped_ptr value class template. I'm aware of the Microsoft-provided: containment of a managed handle in a native class: auto_gcroot containment of a managed handle in a managed class: auto_handle The above two are similar to auto_ptr or unique_ptr . I gave skeleton code for a counted_handle here, similar to shared_ptr But all these are for disposing managed ref class instances, not for freeing native objects. Ben Voigt This one looks fairly complete,

How do I pass smart pointers into functions?

痴心易碎 提交于 2019-12-03 04:44:30
问题 When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory? When I pass, for example, a std::vector<std::string> into a function I always consider the following options: I'm going to change the state of the vector object, but I do not want those changes reflected after the function has finished, AKA make a copy. void function(std::vector<std::string> vec); I'm going to change the state of the vector object, and I do want

Why can't a weak_ptr be constructed from a unique_ptr?

寵の児 提交于 2019-12-03 04:42:43
问题 If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see why a weak_ptr can't be constructed from a unique_ptr , but only a shared_ptr . Can someone briefly explain this? 回答1: std::weak_ptr can't be used unless you convert it to std::shared_ptr by the means of lock() . if the standard allowed what you

How to force only smart pointers instance for a class?

纵然是瞬间 提交于 2019-12-03 02:33:34
I've been working on a way to prevent user of using a class without smart pointers. Thus, forcing them to have the object being heap allocated and managed by smart pointers. In order to get such a result, I've tried the following : #include <memory> class A { private : ~A {} // To force use of A only with std::unique_ptr friend std::default_delete<A>; }; This work pretty well if you only want your class users being capable of manipulating instance of your class through std::unique_ptr . But it doesn't work for std::shared_ptr . So I'd like to know if you had any ideas to get such a behavior.

creating a shared_ptr from unique_ptr

≡放荡痞女 提交于 2019-12-03 00:54:38
In a piece of code I reviewed lately, which compiled fine with g++-4.6 , I encountered a strange try to create a std::shared_ptr from std::unique_ptr : std::unique_ptr<Foo> foo... std::make_shared<Foo>(std::move(foo)); This seems rather odd to me. This should be std::shared_ptr<Foo>(std::move(foo)); afaik, though I'm not perfectly familiar with moves (and I know std::move is only a cast, nothing get's moved). Checking with different compilers on this SSC(NUC*)E #include <memory> int main() { std::unique_ptr<int> foo(new int); std::make_shared<int>(std::move(foo)); } Results of compilation: g++