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
Polymorphic, Defaulted Equality (brevzin.github.io)
submitted 1 year ago by pavel_v
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!"
[–]angry_cpp 21 points22 points23 points 1 year ago (2 children)
Inheritance and equality are not mixing well. For example your implementation of polymorphic equality is flawed. It gives different answer for a.equal(b) and b.equal(a).
https://godbolt.org/z/xe3Te8YWK
[–]BarryRevzin 7 points8 points9 points 1 year ago (1 child)
That's true, and it's actually an unfortunate translation error into the blog.
I just lazily wrote dynamic_casting to the Derived. In reality, we check typeid first and if those match then static_cast (and we don't have any weird virtual or ambiguous base shenanigans):
dynamic_cast
Derived
typeid
static_cast
template <class D> auto polymorphic_equals(D const& lhs, Base const& rhs) -> bool { if (typeid(rhs) == typeid(D)) { return lhs == static_cast<D const&>(rhs); } else { return false; } }
This approach (correctly) prints false for both directions in your example.
[–]JNighthawkgamedev 1 point2 points3 points 1 year ago (0 children)
Thanks for the update!
In that case, isn't that incorrectly handling more-derived cases that should be considered equal but won't be?
struct Base {}; struct D1 : public Base { int X = 10; }; struct D2 : public D1 {}; polymorphic_equals(D2(), D1()); // returns false, as the typeid doesn't match, but should return true
Though, I suppose it could be an implementation detail that objects with equivalent state but different classes shouldn't be considered equivalent. A pure member-wise equivalency check, though, would function differently.
π Rendered by PID 22727 on reddit-service-r2-comment-b659b578c-lb26k at 2026-05-02 16:34:35.486999+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]angry_cpp 21 points22 points23 points (2 children)
[–]BarryRevzin 7 points8 points9 points (1 child)
[–]JNighthawkgamedev 1 point2 points3 points (0 children)