shared-ptr

C++11: extending std::is_pointer to std::shared_ptr

空扰寡人 提交于 2020-05-11 04:05:10
问题 I think about overloading std::is_pointer in C++11 to yield true for std::shared_ptr<T> as well, since the latter behaves very much as a T* . #include <type_traits> namespace std { template <typename T> struct is_pointer<shared_ptr<T>> : std::true_type {}; template <typename T> struct is_pointer<shared_ptr<T const>> : std::true_type {}; } I wonder why this overload has not already been included in the standard implementation. Is there a pitfall that I'm overlooking? As an alternative one

Why using shared_ptr in this boost SSL server example

纵然是瞬间 提交于 2020-04-16 05:48:39
问题 I'm checking this SSL server example and wondering why using shared_ptr . It start with the following method ( do_accept() ) and continuously using auto self(shared_from_this()) in the session class to extend its lifespan between handlers. Q: Is it possible to use a tcp::socket member inside session class and avoid shared_ptr ? What modification must be applied? void do_accept() { acceptor_.async_accept( [this](const boost::system::error_code& error, tcp::socket socket) { if (!error) { std:

How to create a shared_ptr in dll and export it via a factory function?

故事扮演 提交于 2020-04-10 04:41:46
问题 I have this pieces of code: class DLL_API MyClassWrapper { private: MyClass * m_myClass; public: MyClassWrapper(SIZE inputSize); ~MyClassWrapper(); inline int OutputSize(); } typedef std::shared_ptr<MyClassWrapper> MyClassWrapperPtr; extern "C" { DLL_API MyClassWrapperPtr CreatreMyClassWrapper(SIZE inputSize) { return std::make_shared<MyClassWrapper>(inputSize); } } But it doesn't work, with error: Error 1 error C2526: CreatreMyClassWrapper: C linkage function cannot return C++ class 'std:

When should we use std::enable_shared_from_this

六月ゝ 毕业季﹏ 提交于 2020-04-05 15:06:49
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std:

When should we use std::enable_shared_from_this

拈花ヽ惹草 提交于 2020-04-05 15:06:03
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std:

Passing const shared_ptr<T>& versus just shared_ptr<T> as parameter

拟墨画扇 提交于 2020-03-17 06:46:12
问题 I've been reading quite a number of discussions about performance issues when smart pointers are involved in an application. One of the frequent recommendations is to pass a smart pointer as const& instead of a copy, like this: void doSomething(std::shared_ptr<T> o) {} versus void doSomething(const std::shared_ptr<T> &o) {} However, doesn't the second variant actually defeat the purpose of a shared pointer? We are actually sharing the shared pointer here, so if for some reasons the pointer is

Passing const shared_ptr<T>& versus just shared_ptr<T> as parameter

三世轮回 提交于 2020-03-17 06:46:05
问题 I've been reading quite a number of discussions about performance issues when smart pointers are involved in an application. One of the frequent recommendations is to pass a smart pointer as const& instead of a copy, like this: void doSomething(std::shared_ptr<T> o) {} versus void doSomething(const std::shared_ptr<T> &o) {} However, doesn't the second variant actually defeat the purpose of a shared pointer? We are actually sharing the shared pointer here, so if for some reasons the pointer is

Why unique_ptr and shared_ptr do not invalidate the pointer they are constructed from?

一曲冷凌霜 提交于 2020-03-15 05:46:42
问题 A note: this is an API design question , riding on the design of the constructors of unique_ptr and share_ptr for the sake of the question, but not aiming to propose any change to their current specifications. Though it would usually be advisable to use make_unique and make_shared , both unique_ptr and shared_ptr can be constructed from a raw pointer. Both get the pointer by value and copy it. Both allow (i.e. in the sense of: do not prevent ) a continuance usage of the original pointer

C++11 make_shared instancing

北城余情 提交于 2020-02-21 11:43:15
问题 Apologies for the long question, but some context is necessary. I have a bit of code that seems to be a useful pattern for the project I'm working on: class Foo { public: Foo( int bar = 1 ); ~Foo(); typedef std::shared_ptr< Foo > pointer_type; static pointer_type make( int bar = 1 ) { return std::make_shared< Foo >( bar ); } ... } As you can see, it provides a straightforward way of constructing any class as a PointerType which encapsulates a shared_ptr to that type: auto oneFoo = Foo::make(

C++11 make_shared instancing

白昼怎懂夜的黑 提交于 2020-02-21 11:40:55
问题 Apologies for the long question, but some context is necessary. I have a bit of code that seems to be a useful pattern for the project I'm working on: class Foo { public: Foo( int bar = 1 ); ~Foo(); typedef std::shared_ptr< Foo > pointer_type; static pointer_type make( int bar = 1 ) { return std::make_shared< Foo >( bar ); } ... } As you can see, it provides a straightforward way of constructing any class as a PointerType which encapsulates a shared_ptr to that type: auto oneFoo = Foo::make(