smart-pointers

enable_shared_from_this - empty internal weak pointer?

你。 提交于 2019-11-29 11:50:58
问题 I'm using enable_shared_from_this<Base> and then inherit from Base . When trying to use shared_from_this() in Derived 's constructor (not initializer list), I get an exception. Turns out that the internal weak pointer is null and doesn't point to this at all. How can this happen? My other use case of exactly this works perfectly fine. I don't even know where to start. I looked down at the source code of enable_shared_from_this , and it looks to me like that pointer would always be nullptr.

Why can operator-> be overloaded manually?

烂漫一生 提交于 2019-11-29 11:40:16
问题 Wouldn't it make sense if p->m was just syntactic sugar for (*p).m ? Essentially, every operator-> that I have ever written could have been implemented as follows: Foo::Foo* operator->() { return &**this; } Is there any case where I would want p->m to mean something else than (*p).m ? 回答1: operator->() has the bizarre distinction of implicitly being invoked repeatedly while the return type allows it . The clearest way to show this is with code: struct X { int foo; }; struct Y { X x; X*

Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr

北战南征 提交于 2019-11-29 11:37:53
I have read through many questions on SO on custom deleter for shared_ptr and unique_ptr , and the difference between the two. But, I still haven't found any clear answer to this question: How can one best go about creating a type that acts as a shared_ptr with a custom deleter, similar to how unique_ptr has the deleter as part of the type definition? For unique_ptr usage, I use a deleter class, that handles deletion of individual types (limiting it to just two types, for brevity): struct SDL_Deleter { void operator()( SDL_Surface* ptr ) { if (ptr) SDL_FreeSurface( ptr );} void operator()( SDL

Using std::shared_ptr<void> to point to anything

余生长醉 提交于 2019-11-29 07:33:10
I'm using a std::shared_ptr<void> in my application to make a smart pointer which can point to many different types of data structures like structs, vectors, matrices... basically anything. What I'm trying to do is map some names to their data structures. I'm performing the mapping using a hashtable: std::unordered_map<std::string, std::shared_ptr<void>> Can I cast the std::shared_ptr<void> returned by find() back to std::shared_ptr<my_type> ? If so, how? More importantly, is this good practice? Will this increase the complexity too much as the application scales? Or, is there some other

Why is std::rc::Rc<> not Copy?

只愿长相守 提交于 2019-11-29 03:13:14
Can someone explain to me why Rc<> is not Copy ? I'm writing code that uses a lot of shared pointers, and having to type .clone() all the time is getting on my nerves. It seems to me that Rc<> should just consist of a pointer, which is a fixed size, so the type itself should be Sized and hence Copy , right? Am I missing something? Lukas Kalbertodt It seems to me that Rc<> should just consist of a pointer, which is a fixed size, so the type itself should be Sized and hence Copy , right? This is not quite true. Rc is short for R eference C ounted. This means that the type keeps track of how many

getting a normal ptr from shared_ptr?

烈酒焚心 提交于 2019-11-29 02:04:25
问题 I have something like shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type . How do I get it from shared_ptr ? 回答1: Use the get() method: boost::shared_ptr<foo> foo_ptr(new foo()); foo *raw_foo = foo_ptr.get(); c_library_function(raw_foo); Make sure that your shared_ptr doesn't go out of scope before the library function is done with it -- otherwise badness could result, since the library may try to do something

Why shouldn't you use references to smart pointers?

痴心易碎 提交于 2019-11-29 01:54:45
问题 I recall reading somewhere that using references to smart pointers can cause memory corruption. Is this simply because of using the reference of the smart pointer after its been destroyed? Or does the reference counting get messed up? Thanks for clarifying 回答1: Assuming you are talking about shared_ptr here... Is this simply because of using the reference of the smart pointer after its been destroyed? This is a good answer. You may not know absolutely the lifetime of the pointer your

unique_ptr<T> lambda custom deleter for array specialization [duplicate]

我是研究僧i 提交于 2019-11-29 01:39:08
问题 This question already has answers here : How do I use a custom deleter with a std::unique_ptr member? (6 answers) Closed last year . I recently started porting lots of my existing C++ application code to over to C++11 and now that I am converting to the new smart pointers std::unique_ptr and std::shared_ptr , I have a specific question about custom deleters. I want to add a lambda logger to see where my deletes are being called but I cannot get the array specialization version to compile.

Can storing unrelated data in the least-significant-bit of a pointer work reliably?

梦想的初衷 提交于 2019-11-28 23:33:51
Let me just say up front that what I'm aware that what I'm about to propose is a mortal sin, and that I will probably burn in Programming Hell for even considering it. That said, I'm still interested in knowing if there's any reason why this wouldn't work. The situation is: I have a reference-counting smart-pointer class that I use everywhere. It currently looks something like this (note: incomplete/simplified psuedocode): class IRefCountable { public: IRefCountable() : _refCount(0) {} virtual ~IRefCountable() {} void Ref() {_refCount++;} bool Unref() {return (--_refCount==0);} private:

What is the difference between std::shared_ptr and std::experimental::atomic_shared_ptr?

喜你入骨 提交于 2019-11-28 22:33:58
问题 I read the following article by Antony Williams and as I understood in addition to the atomic shared count in std::shared_ptr in std::experimental::atomic_shared_ptr the actual pointer to the shared object is also atomic? But when I read about reference counted version of lock_free_stack described in Antony's book about C++ Concurrency it seems for me that the same aplies also for std::shared_ptr , because functions like std::atomic_load , std::atomic_compare_exchnage_weak are applied to the