shared_ptr | minimal shared/unique_ptr implementation | SDK library
kandi X-RAY | shared_ptr Summary
kandi X-RAY | shared_ptr Summary
A minimal shared/unique_ptr implementation to handle cases where boost/std::shared/unique_ptr are not available.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of shared_ptr
shared_ptr Key Features
shared_ptr Examples and Code Snippets
Community Discussions
Trending Discussions on shared_ptr
QUESTION
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 has to do with static vs. dynamic deleters. I'm looking for a more detailed explanation of why that might be the case (it may be compiler implementation specific in which case an example would be helpful). With dynamic deleters, does it restrict the destructor from being inlined?
ANSWER
Answered 2022-Apr-11 at 12:39Howard Hinnant was simplifying. What he precisely meant was if you use the default deleter for std::unique_ptr
, you need a complete type. For the default deleter, it simply calls delete
for you.
The gist of static and dynamic deleters is
QUESTION
My server crashes when I gracefully close a client that is connected to it, while the client is receiving a large chunk of data. I am thinking of a possible lifetime bug as with the most bugs in boost ASIO, however I was not able to point out my mistake myself.
Each client establishes 2 connection with the server, one of them is for syncing, the other connection is long-lived one to receive continuous updates. In the "syncing phase" client receives large data to sync with the server state ("state" is basically DB data in JSON format). After syncing, sync connection is closed. Client receives updates to the DB as it happens (these are of course very small data compared to "syncing data") via the other connection.
These are the relevant files:
connection.h
...ANSWER
Answered 2022-Apr-05 at 01:14Reviewing, adding some missing code bits:
QUESTION
When I want to add a member variable with smart pointer type to a class, I found that it can't be initialized at the declaring place:
...ANSWER
Answered 2022-Apr-01 at 10:42std::shared_ptr
can't be copy-initialized from raw pointer, the conversion constructor is marked as explicit
.
You can use direct-initialization:
QUESTION
This is more of a follow up question on the second answer posted here. The code from this answer is shown below:
...ANSWER
Answered 2022-Mar-22 at 21:49The shared pointer created has a destroy function object (deleter) that has state. In particular it has a copy of the boost shared ptr.
The destruction action does nothing, it is the destruction of deleter that cleans up the boost shared ptr.
There is a problem though:
Does the C++ standard fully specify cleanup of the deleter itself? E.g. it might stay around when only weak references remain? The standard does guarantee a minimum lifetime for the deleter, leaving the possibility that lives longer than that.
Destroying the destruction object is not specified to occur in either the constructor of std::shared_ptr
nor the destructor of std::shared_ptr
. It would be reasonable to destroy it either after it is called, or when the reference counting block is destroyed - in the second case, we end up with it lasting until the last weak ptr goes away.
From the draft standard:
[Note: It is unspecified whether the pointer remains valid longer than that. This can happen if the implementation doesn’t destroy the deleter until all
weak_ptr
instances that share ownership with p have been destroyed. — end note]
The possibility to defer destruction of the deleter is clear. So in that case we end up with shared_ptr
s that doesn't destruct their element instance when we want it to be called depending on unspecified details of the C++ implementation you run it on. That is a bad idea.
I would personally go with a similar trick based on the aliasing constructor instead. It is just as simple, has no additional overhead, and the semantics are actually correct:
QUESTION
Since the constructor of std::shared_ptr
is marked as explicit one, so expressions like auto p = std::make_shared(1); p = new int(6);
is wrong.
My question is why does std::make_shared(1); p = nullptr;
compile?
Here is the aforementioned code snippet:
...ANSWER
Answered 2022-Mar-13 at 12:43The raw pointer constructor is explicit
to prevent you accidentally taking ownership of a pointer. As there is no concern taking ownership of nullptr
the constructor taking std::nullptr_t
is not marked explicit.
Note that this only applies to nullptr
these other assignments of a null pointer might not work (depending on your compiler):
QUESTION
I'm looking for a solution using only native C++ language features (up to C++17) for accomplishing the following:
...ANSWER
Answered 2022-Mar-11 at 20:28Like this:
QUESTION
I am not really getting any smarter from these error messages.
...ANSWER
Answered 2022-Feb-16 at 08:42{1,2,3}
can be multiple things, and make_shared has no possibility of knowing what it is at the time parameter pack is expanded.
If you don't want to state the long std::initializer_list{1,2,3}
explicitly, the easiest solutions would be:
a. shortening the type's name: using ints=std::initializer_list;
b. wrapping the call:
QUESTION
I wish to express that each object of type V owns an object of type B. B is a polymorphic type, and is therefor accessed through a pointer or reference to prevent slicing (C.145). I find it natural to express this as following
...ANSWER
Answered 2022-Feb-11 at 20:06You need to ask yourself a question : Why should B not be copyable?
The example you described, with a graph owning one (or probably multiple) vertexes shows the opposite : B should be copyable! What you don't want is two graphs sharing the same vertex instance.
So you can create a copy constructor / clone method for V along those lines :
QUESTION
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 directly do it. The easiest solution for this is just to call weak_ptr
's reset() but the class which owns the shared_ptr
and wants to release it doesn't know which class has weak_ptr
of it. How can that be achieved in this case?
I understand why shared_ptr
doesn't have release function but unique_ptr
. In my case, only one instance owns a pointer, but shared_ptr
can be owned by multiple instances and releasing doesn't make sense then. But if shared_ptr
doesn't have that function, how can I cut connections to weak_ptr
without deleting the object?
ANSWER
Answered 2022-Feb-04 at 00:06You may use a shared_ptr
with custom deleter that would prevent the object from being destroyed:
QUESTION
I'd like to call the class Foo
which has the abstract class Base
in its ctor. I'd like to be able to call Foo
from Derived
which is derived from Base
and use Derived
's overriding methods rather than Base
's.
I'm only able to do this by using a raw pointer as indicated. Is there any way to do this without raw pointers? I tried std::shared_ptr
but the compiler complains about abstract classes. Or perhaps is there a better way?
ANSWER
Answered 2022-Jan-15 at 15:02The code can be rewritten with const reference to Base
as
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install shared_ptr
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page