shared-ptr

Why calling shared_from_this calls std::terminate

空扰寡人 提交于 2019-12-06 09:03:35
Consider this code: class A : public std::enable_shared_from_this<A> { public: std::shared_ptr<A> f() { return shared_from_this(); } }; int main() { A a; std::shared_ptr<A> ptr = a.f(); } This code terminated in Visual Studio 2017. I guess I am doing something wrong here. Can anyone help me with this? I want a shared_ptr on a created by shared_from_this(). Because a is not a owned by a shared pointer. From cppreference : It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr. Otherwise the behavior is undefined (until C++17)std

Why do std::shared_ptr<void> work

旧城冷巷雨未停 提交于 2019-12-06 08:22:41
问题 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>>

Shared_Ptr of socket creation - what is wrong?

允我心安 提交于 2019-12-06 07:47:15
So I try: boost::shared_ptr<tcp::socket> socket = boost::make_shared<tcp::socket>(io_service); As described here . But It bring me an error: Compiler tells me that it can not turn ( error C2664: boost::asio::basic_stream_socket<Protocol>::basic_stream_socket( boost::asio::io_­service &)) 'boost::asio::io_service *const ' into 'boost::asio::io_service &' \include\boost\smart_ptr\make_shared.hpp What shall I do? You need to pass the io_service as a reference when using make_shared . boost::shared_ptr<tcp::socket> socket = boost::make_shared<tcp::socket>(boost::ref(io_service)); 来源: https:/

C++NRVO guarantees? Or better prefer non-const ref param or shared_ptr?

 ̄綄美尐妖づ 提交于 2019-12-06 07:08:22
I have been using C++ since 1992 (and reading copious amounts about the language), so I know a fair amount about the language, but far from all. My question is about C++11 named return value optimization - what guarantees are there that it will be performed? I tend to prefer to send in non-const parameters (C++97 style) or use shared_ptr (C++11 style), or even used ptr-to-ptr (C style). One reason is that with non-const ref args or shared_ptr, I am guaranteed that NO extra object copies are made. So my question is (especially for those C++ programmers who do hard real-time or kernel work):

C++11 cast const iterator pointing to container of shared_ptr objects

醉酒当歌 提交于 2019-12-06 05:21:24
I have an STL container whose element type is const std::shared_ptr<MyClass> . I want to supply two iterator types to the user: MyContainer::iterator typedefed as std::vector<const std::shared_ptr<MyClass>>::iterator (which should be the same type as std::vector<const std::shared_ptr<const MyClass>>::const_iterator MyContainer::const_iterator typedefed as std::vector<const std::shared_ptr<const MyClass>>::iterator (which should be the same type as std::vector<const std::shared_ptr<const MyClass>>::const_iterator In other words, I want the " const " to refer to the MyClass constness, not shared

Correct use of shared_ptr to eliminate deallocation across DLL boundaries

感情迁移 提交于 2019-12-06 03:33:42
问题 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

Why doesn't std::shared_ptr use reference linking?

佐手、 提交于 2019-12-06 01:47:40
std::shared_ptr needs to allocate a control block on the heap which holds the reference count. There was another approach I learnt from http://ootips.org/yonat/4dev/smart-pointers.html which keeps all the references in a doubly linked list. It doesn't need additional allocations nor a counter but the reference object itself is larger. Is there a benchmark or any clear reason showing one implementation is better than the others? The standard does in theory allow a linked list to be used, but because copying a shared_ptr must be threadsafe it would be harder to implement that with a linked list.

shared_ptr and use_count

谁说胖子不能爱 提交于 2019-12-05 22:08:30
In the following code snippet: shared_ptr<int> p; { p = shared_ptr<int>(new int); cout<<p.use_count()<<endl; } cout<<p.use_count()<<endl; The output comes out to be 1 1 I don't understand why the 1st output is 1 -- shouldn't it be 2 ? The temporary object's lifetime does not last long enough for the first p.use_count() to return 2. The temporary object is destroyed first, relinquishing its ownership on anything it owned. Furthermore, since the temporary is an rvalue, the assignment to p will result in a move-assignment, which means the use-count will never be 2 anyway (assuming a quality

Disadvantages of shared_ptr

耗尽温柔 提交于 2019-12-05 21:23:53
问题 With shared_ptr included in c++11, one could achieve a semi garbage-collected enviroment. Does the (inflationary?) usage come along with some disadvantages? I could imagine a class model, where you create a class in which you typedef your class at the end as a shared_ptr to abbreviate the syntax. ///////////////// //// MyClass //// ///////////////// #include <memory> class MyClass { public: Myclass(); }; typedef std::shared_ptr<MyClass> SharedMyClass; /////////////////////// //// Example

c++: how to return a shared_ptr from function

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 20:07:32
when trying to return a shared_ptr from a function I get: reference to local variable 'recipe' returned [-Werror=return-local-addr] where did I go wrong ? shared_ptr<Recipe>& Group::addRecipe(const string& groupName, unsigned int autherId, const string& recipeName){ shared_ptr<Recipe> recipe(new Recipe(recipeName, autherId)); recipes.push_back(recipe); return recipe; } what is the right way to return a shared_ptr ? The function's signature isn't shown, but it sounds like it's probably returning shared_ptr<Recipe>& . Returning a reference to a temporary is a big no-no since the referenced