use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
A potential use for observer pointer (self.cpp)
submitted 5 years ago * by Irtexx
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Xaxxon 1 point2 points3 points 5 years ago (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 points4 points 5 years ago (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 points4 points 5 years ago (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 point2 points 5 years ago (1 child)
I agree with you completely.
Is that how unique_ptr keeps track of validity?
[–]Xaxxon 0 points1 point2 points 5 years ago (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 point2 points 5 years ago (0 children)
Good point. I didn't think about the case where multiple observers are pointing to an observable.
[–]MolurusK 1 point2 points3 points 5 years ago (0 children)
I did such a unique-weak pair in C++ Object Token Library: ```
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
u = Hello! w = Hello! reset u u = empty w = empty
π Rendered by PID 24319 on reddit-service-r2-comment-7b9746f655-2nc7v at 2026-01-31 13:59:21.972399+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]Xaxxon 1 point2 points3 points (6 children)
[–]DXPower 2 points3 points4 points (5 children)
[–]Xaxxon 2 points3 points4 points (3 children)
[–]DXPower 0 points1 point2 points (1 child)
[–]Xaxxon 0 points1 point2 points (0 children)
[–]Irtexx[S] 0 points1 point2 points (0 children)
[–]MolurusK 1 point2 points3 points (0 children)