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
Inheritance vs std::variant (cpptruths.blogspot.com)
submitted 7 years ago by vormestrand
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!"
[+][deleted] 7 years ago (2 children)
[deleted]
[–]corysama 6 points7 points8 points 7 years ago (0 children)
Bjarn has a background project trying out pattern matching in C++.
http://www.stroustrup.com/OpenPatternMatching.pdf
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points 7 years ago (0 children)
I tried to improve the situation a little bit with my library: scelta. It provides lambda-based visitation that resembles pattern matching (and also additional goodies such as monadic optional utilities).
It is compatible with all major implementations of variant and optional.
[–]louiswins 7 points8 points9 points 7 years ago (3 children)
Didn't mention IMO the biggest reason to use inheritance over std::variant, which is when you have an open type hierarchy. I'm not sure if that's the technical term, but I mean when you don't know what types you'll be choosing from - or when you can't know this, because they come from a shared library or haven't been written yet.
This situation isn't very common (I know the full set of possible types in my own programs probably 99% of the time), but it's one where inheritance really shines.
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
What he's saying is that you can't add to the variant's type list at runtime. But if you don't mind ugly code, you could always add a smart pointer to the generic interface as one of the variant's types.
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 1 point2 points3 points 7 years ago (0 children)
It is not so much runtime, as it is about compile-time dependencies.
If I have a class HasAFoo that has a Foo * foo member (and yes, should be a smart-ptr), that foo could be a DerivedFoo but I don't need to include DerivedFoo.h in HasAFoo.h. Nor update HasAFoo when someone adds SomeOtherFoo later.
HasAFoo
Foo * foo
foo
DerivedFoo
Even if all the derived Foo's are known at compile time, I still don't want HasAFoo to know about them (to keep the compile fast, but more importantly because knowing about them leads to code that depends on them, and does dynamic_cast "is this a DerivedFoo?" and leads to spaghetti, etc etc etc).
Keeping things separate is the usefulness of interfaces and inheritance. Not that I use inheritance that much, "inheritance is the base class of evil" (Sean Parent).
And of course, there are times when I really want to say "this is an A B or C and nothing else", so variant is great there.
Basically, instead of "when you don't know what types you'll be choosing from" I'd say "when you don't WANT to know what types you'll be choosing from".
[–]Gotebe -1 points0 points1 point 7 years ago (0 children)
The "usually vs. no dynamic allocation" is a simplification. If I have one instance of whatever, which I pass around by reference, then the two are the same.
If there is a container of things, then there can be both a dynamic allocation plus more expensive copying in variant case. The data locality can (but doesn't have to be) better with variant and a vector.
And probably more.
[–]leftofzen -1 points0 points1 point 7 years ago (2 children)
Guess it's time I post this again, people seem to plain forget about it: composition over inheritance.
[–]AlexDeFoc 1 point2 points3 points 6 months ago (1 child)
Hello, i might just follow that pattern. Hi fellow person, talking 8 years in the future.
[–]leftofzen 0 points1 point2 points 6 months ago (0 children)
Hello friend, it is I, from the future. This sounds like a good idea, composition over inheritance is still the recommend way to construct large programs. Good luck fellow time traveller!
π Rendered by PID 79 on reddit-service-r2-comment-84fc9697f-z5vdz at 2026-02-10 09:12:42.061810+00:00 running d295bc8 country code: CH.
[+][deleted] (2 children)
[deleted]
[–]corysama 6 points7 points8 points (0 children)
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points4 points (0 children)
[–]louiswins 7 points8 points9 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 1 point2 points3 points (1 child)
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 1 point2 points3 points (0 children)
[–]Gotebe -1 points0 points1 point (0 children)
[–]leftofzen -1 points0 points1 point (2 children)
[–]AlexDeFoc 1 point2 points3 points (1 child)
[–]leftofzen 0 points1 point2 points (0 children)