shared-ptr

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.

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

How to accomplish covariant return types when returning a shared_ptr?

≯℡__Kan透↙ 提交于 2019-11-29 11:15:31
问题 using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around this, if there is one? 回答1: I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers.

How does shared_ptr work in if condition

给你一囗甜甜゛ 提交于 2019-11-29 09:29:27
In C++, I can write something like: shared_ptr<A> a_sp = someFunctionReturningSharedPtr(); if (a_sp) { cout << a_sp->someData << endl; } else { cout << "Shared Pointer is NULL << endl; } Why does if (a_sp) check work correctly? a_sp is not a boolean, but how is it checked for true or false? How does the if condition know to check the result of a_sp.get() function? Or if it does not, how is the NULL ity of the a_sp checked? Is there some function in shared_ptr defined that converts it to boolean value? shared_ptr has an operator unspecified-bool-type() const that allows it to be used in boolean

Differences between different flavours of shared_ptr

蹲街弑〆低调 提交于 2019-11-29 09:14:00
Are there any differences between boost::shared_ptr , std::tr1::shared_ptr and the upcoming (in C++0x ) std::shared_ptr ? Will porting from one to another have any overhead or are they basically the same? According to the Boost website, the boost::shared_ptr ... ...conforms to the TR1 specification, with the only exception that it resides in namespace boost instead of std::tr1 . According to the Wikipedia C++0x page The TR1 implementation lacked certain pointer features such as aliasing and pointer arithmetic, but the C++0x version will add these. If your code works with the TR1/Boost version,

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

About shared_ptr and pointer to member operator `->*` and `std::bind`

ⅰ亾dé卋堺 提交于 2019-11-29 03:58:24
Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why shared_ptr does not have this operator? I added such operator for shared_ptr and the example started to

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 std::shared_ptr calls destructors from base and derived classes, where delete calls only destructor from base class? [duplicate]

故事扮演 提交于 2019-11-29 01:50:13
This question already has an answer here: shared_ptr magic :) 3 answers Why when using std::shared_ptr deallocation calls destructors from both base and derived classes when second example calls only destructor from base class? class Base { public: ~Base() { std::cout << "Base destructor" << std::endl; } }; class Derived : public Base { public: ~Derived() { std::cout << "Derived destructor" << std::endl; } }; void virtual_destructor() { { std::cout << "--------------------" << std::endl; std::shared_ptr<Base> sharedA(new Derived); } std::cout << "--------------------" << std::endl; Base * a =

boost shared_from_this and multiple inheritance

不羁岁月 提交于 2019-11-29 01:45:41
I am currently having some troubles when using boost shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from shared_from_this Class B implements another functionality and should inherit from shared_from_this Class D inherits functionalities from A and B ( class D : public A, public B {} ) When using some class B functionality from class D I got an exception ( weak_ptr ) To inherit shared_from_this from class D is not an option for me I am not sure about how to solve this. Oh, I am using Visual C++ 2010.