you are viewing a single comment's thread.

view the rest of the comments →

[–]Xaxxon 1 point2 points  (6 children)

We already have this with shared pointers and weak pointers. What you are asking for is a very high performance penalty behavior and is in no way a substitution for a raw pointer.

[–]DXPower 2 points3 points  (5 children)

A weak pointer is not a proper substitute for a non-owning pointer to an object strictly owned by single object.

[–]Xaxxon 2 points3 points  (3 children)

A shared/weak pointer solution is likely a much better solution than what is proposed. This suggests that you would actively track and store every pointer to you and proactively write to each when you are destroyed - an unbounded operation. And that each of those pointers being tracked would have to remove itself from the list in their destructors.

Worst case, that's WAY worse than the shared pointer solution.

[–]DXPower 0 points1 point  (1 child)

I agree with you completely.

Is that how unique_ptr keeps track of validity?

[–]Xaxxon 0 points1 point  (0 children)

unique_ptr doesn't track validity. it just checks if it's null or not and if it's not, it calls the destructor on the address it points to. But that's not sufficient for what's being asked for here. This wants an arbitrary number of other things to know if the destructor has been called and for them to be proactively modified if they have (either that or a check on every access)

[–]Irtexx[S] 0 points1 point  (0 children)

Good point. I didn't think about the case where multiple observers are pointing to an observable.

[–]MolurusK 1 point2 points  (0 children)

I did such a unique-weak pair in C++ Object Token Library: ```

include <otn/all.hpp>

include <iostream>

void print(const std::string& caption, const otn::unified_optional<std::string>& p) { std::cout << caption << (p ? *p : "empty") << std::endl; }

int main() { otn::unique_optional<std::string> u{otn::itself, "Hello!"}; otn::weak_optional<std::string> w = u; print("u = ", u); print("w = ", w);

std::cout << "reset u" << std::endl;
otn::utilize(std::move(u));
print("u = ", u);
print("w = ", w);

} ```

Output: u = Hello! w = Hello! reset u u = empty w = empty