shared-ptr Questions

4

Solved

I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one pl...
Cartage asked 5/3, 2014 at 0:8

4

Solved

I am trying to keep a list of connected clients in asio. I have adapted the chat server example from the docs (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_serve...
Foldboat asked 30/12, 2014 at 0:24

5

Solved

std::unique_ptr has 2 template parameters, the second of which is the deleter to be used. Thanks to this fact, one can easily alias a unique_ptr to a type, which requires a custom deleter (e.g. SDL...
Seen asked 19/6, 2016 at 13:29

2

Solved

std::shared_ptr<std::string> test() { return std::make_shared<std::string>("sdsd"); } cout << *test() << endl; The above code works. Can someone please let me k...
Patricio asked 13/4, 2022 at 3:17

2

Solved

I would like to understand why unique_ptr destructors require the type to be complete upon destruction while that isn't the case with shared_ptr. This blog from Howard Hinnant briefly mentions it h...
Rattly asked 10/4, 2022 at 22:47

6

I have functions that take in std::shared_ptr as an argument so I am forced to use std::shared_ptr, but the object I am passing to the function is not dynamically allocated. How do I wrap the objec...
Unifilar asked 21/11, 2013 at 21:7

2

Solved

I'm trying to check if a std::shared_ptr is null. Is there a difference between doing std::shared_ptr<int> p; if (!p) { // method 1 } if (p == nullptr) { // method 2 }
Cyclosis asked 29/11, 2016 at 0:43

15

Solved

I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr. Can someone tell me when std::weak_ptr is useful/necessary?
Churchill asked 19/8, 2012 at 23:0

2

Solved

This is more of a follow up question on the second answer posted here. The code from this answer is shown below: template<typename T> void do_release(typename boost::shared_ptr<T> const...
Prospectus asked 22/3, 2022 at 12:36

4

The equality operator for shared_ptr's is defined as follows: template<class T, class U> inline bool operator==( shared_ptr<T> const & a, shared_ptr<U> const & b) { ret...
Illusory asked 19/4, 2011 at 21:33

4

Solved

Here's my code. When compiling I get the error invalid declarator before ‘geometry’ at line 16 and line 48, I am not sure what I am doing wrong. Please advise. #include <iostream> #inc...

2

Solved

I'd like to reset a shared_ptr without deleting its object and let weak_ptr of it loses a reference to it. However, shared_ptr doesn't have release() member function for reasons, so I can't directl...
Uncinate asked 3/2, 2022 at 23:47

2

Solved

How to get a reference to an object having shared_ptr<T> to it? (for a simple class T)
Katekatee asked 27/11, 2012 at 0:56

4

Solved

buffer = new char[64]; buffer = std::make_shared<char>(char[64]); ??? Can you allocate memory to an array using make_shared<>()? I could do: buffer = std::make_shared<char>( ne...
Lancey asked 10/12, 2012 at 3:5

5

Solved

In the following code example: #include <iostream> class Foo{ }; class Bar{ public: void addFoo(Foo *foo){ auto my_foo = std::shared_ptr<Foo>(foo); } }; int main() { auto bar = B...
Mazur asked 3/1, 2022 at 9:3

2

I'm learning smart pointers and shared_from_this. In Class Inheritance Relations, it will be very hard to understand. I have two base classes CA and CB, they are derived from enable_shared_from_th...
Triplenerved asked 31/8, 2019 at 13:23

8

Solved

I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = std::move(Value...
Leroi asked 26/1, 2017 at 10:11

1

Solved

Consider // https://godbolt.org/z/z5M9b9jzx #include <memory> #include <cassert> struct B {}; struct D : B {}; int main() { std::shared_ptr<B> b = std::make_shared<D>(); ...
Actually asked 27/5, 2021 at 7:26

3

I'm converting a large code to use custom shared pointers instead of raw pointers. I have a problem with overload resolution. Consider this example: #include <iostream> struct A {}; struct...
Usher asked 8/12, 2016 at 13:11

4

Solved

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 ...
Ainsworth asked 14/12, 2016 at 12:10

3

Solved

Suppose I had two shared_ptr types such as boost::shared_ptr<ObjA> sptrA; boost::shared_ptr<ObjB> sptrB; Now suppose that sptrA->SomeMethod() returned a simple ObjB type (not a sh...
Stithy asked 29/10, 2012 at 4:9

4

Here's a quote from cppreference's implementation note section of std::shared_ptr, which mentions that there are two different pointers(as shown in bold) : the one that can be returned by get(), an...
Downtown asked 2/12, 2015 at 15:7

4

Solved

I would like to use a std::atomic<std::shared_ptr> in my code so that the shared_ptr can be atomicaly updated, but I have a problem when accessing the shared_ptr. The load() method on the ato...
Morelock asked 4/11, 2012 at 8:40

1

Solved

I faced a problem with std::shared_ptr. I have a code: void Receive(const std::shared_ptr<char[]>& data, uint32_t length) { this->next->Receive(data + 2, length - 2); } I need to ...
Confession asked 13/7, 2021 at 14:35

1

Solved

While comparing assembly for std::shared_ptr vs. boost::shared_ptr, I noticed that GCC generates a whole lot more code for void test_copy(const std::shared_ptr<int> &sp) { auto copy = sp;...
Sawtelle asked 10/7, 2021 at 16:31

© 2022 - 2025 — McMap. All rights reserved.