shared-ptr

Why do std::shared_ptr<void> work

时间秒杀一切 提交于 2019-12-04 14:29:35
I found some code using std::shared_ptr to perform arbitrary cleanup at shutdown. At first I thought this code could not possibly work, but then I tried the following: #include <memory> #include <iostream> #include <vector> class test { public: test() { std::cout << "Test created" << std::endl; } ~test() { std::cout << "Test destroyed" << std::endl; } }; int main() { std::cout << "At begin of main.\ncreating std::vector<std::shared_ptr<void>>" << std::endl; std::vector<std::shared_ptr<void>> v; { std::cout << "Creating test" << std::endl; v.push_back( std::shared_ptr<test>( new test() ) ); std

Error while copy constructing boost::shared_ptr using C++11

我与影子孤独终老i 提交于 2019-12-04 13:44:47
Yesterday I installed clang 3.1 and g++ 4.7 and tried compiling a project I'm working on. I was surprised to see that it didn't compile using both of the compilers. But what surprises me most is that the problem is in boost::shared_ptr . Apparently, since that class defines a move constructor/assignment operator, the copy constructor is implicitly deleted. So this code: #include <boost/shared_ptr.hpp> int main() { boost::shared_ptr<int> x; boost::shared_ptr<int> y(x); } Does not compile. clang echoes this error: test.cpp:5:28: error: call to implicitly-deleted copy constructor of 'boost:

Get Eclipse CDT + boost::shared_ptr<T> to work with syntax completion?

扶醉桌前 提交于 2019-12-04 13:20:36
How to get Eclipse CDT to treat shared_ptr as T * for syntax completion? I'm using windows in this instance. I have 1.39 in the "Program Files" folder. I am about to try 1.37. I am using the Galileo release of Eclipse. Also, I am only editing and browsing the source in Eclipse and building in VC++ Express. (but that is another story) What version of boost are you using? According to this thread : Turns out there is something about Boost 1.39 that the CDT indexer does not like. If I allow CDT to index boost 1.36 then auto-complete works for typedef'ed elements like below. If I allow it to index

Shared pointers to a singleton do not recognize each other

半腔热情 提交于 2019-12-04 12:57:33
问题 I am currently learning how to use the C++11 smart pointers while programming a 2D game engine as a hobby using SDL. However, I ran into a problem while implementing an OOp wrapper for SDL. The intent is to create a singleton class, which initializes SDL when it is constructed, and shuts SDL down when it gets destroyed. The singleton class has a static method getInstance that returns a shared_ptr to the singleton, and constructs the single instance if no instance exists, the idea being that

C++ weak_ptr creation performance

不羁的心 提交于 2019-12-04 12:24:06
问题 I've read that creating or copying a std::shared_ptr involves some overhead (atomic increment of reference counter etc..). But what about creating a std::weak_ptr from it instead: Obj * obj = new Obj(); // fast Obj * o = obj; // slow std::shared_ptr<Obj> a(o); // slow std::shared_ptr<Obj> b(a); // slow ? std::weak_ptr<Obj> c(b); I was hoping in some faster performance, but i know that the shared pointer still have to increment the weak references counter.. So is this still as slow as copying

Does shared_ptr's dtor require the use of a “deleter”?

怎甘沉沦 提交于 2019-12-04 09:45:10
问题 It's widely known that you can use a shared_ptr to store a pointer to an incomplete type, as long as the pointer can be deleted (with well-defined behaviour) during the construction of the shared_ptr . For example, the PIMPL technique: struct interface { interface(); // out-of-line definition required ~interface() = default; // public inline member, even if implicitly defined void foo(); private: struct impl; // incomplete type std::shared_ptr<impl> pimpl; // pointer to incomplete type };

Why is the size of make_shared two pointers?

故事扮演 提交于 2019-12-04 08:53:06
问题 As illustrated in the code here, the size of the object returned from make_shared is two pointers. However, why doesn't make_shared work like the following (assume T is the type we're making a shared pointer to): The result of make_shared is one pointer in size, which points to of allocated memory of size sizeof(int) + sizeof(T) , where the int is a reference count, and this gets incremented and decremented on construction/destruction of the pointers. unique_ptr s are only the size of one

Why is std::shared_ptr::unique() deprecated?

时光毁灭记忆、已成空白 提交于 2019-12-04 08:25:41
问题 What is the technical problem with std::shared_ptr::unique() that is the reason for its deprecation in C++17? According to cppreference.com, std::shared_ptr::unique() is deprecated in C++17 as this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment. I understand this to be true for use_count() > 1 : While I'm holding a reference to it, someone else might simultaneously let go of his or create a new copy. But if use_count() returns 1

Correct use of shared_ptr to eliminate deallocation across DLL boundaries

China☆狼群 提交于 2019-12-04 07:49:31
I'm reading " Using shared_ptr in dll-interfaces ". In that post, phlipsy suggested a way to pass no implementation specific object across DLL boundaries, at the end of his answer. Basically, the idea is to return a raw pointer from DLL and initialize the shared_ptr in EXE w/ that raw pointer. I don't think it's correct. Let me reprototype it for simplicity. // wrong version?? // DLL Object* createObject() { return new Object; } // EXE std::tr1::shared_ptr<Object> p(createObject()); .. When object is deallocated, the destruction context/heap used by shared_ptr is different from that used in

Verify at compile time that objects are created as shared_ptr

谁说我不能喝 提交于 2019-12-04 05:51:08
There are classes that I write (often as part of boost::asio ) whose objects depend on being wrapped in a shared_ptr because they use shared_from_this() . Is there a way to prevent an object from being compiled if it's not instantiated in a shared_ptr ? So, what I'm looking for: std::shared_ptr<MyClass> a = std::make_shared<MyClass>(); // should compile fine std::unique_ptr<MyClass> a = std::make_unique<MyClass>(); // compile error MyClass a; // compile error Make its constructor private and give it a static factory member function that creates a shared_ptr . Don't forget to document your