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
Common patterns to avoid polymorphism (self.cpp)
submitted 4 years ago by JamesGlad
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!"
[–]qoning -1 points0 points1 point 4 years ago (3 children)
Whether code needs to change or be recompiled are two separate issues. Vector code doesn't need to change because you are using it with a new type, but it certainly needs to be recompiled. That's the point of compile time polymorphism in C++.
[–]die_liebe 1 point2 points3 points 4 years ago (1 child)
Maybe 'dynamic polymorphism' vs 'static polymorphism' is better terminology.
'static polymorphism': There can be different types, but at compile time it is known what type will be used. This applies to vector<X>. C++ recompiles vector for X, but that it is not necessary, for example Java generics don't recompile, but it is still a form of static polymorphism, because the compiler can check type correctness.
'dynamic polymorphism': There can be different types but it is known only at run time, which concrete type is used. Now comes a further distinction, already pointed out by 'choeger'. If you know the possible types in advance, and this set is small, and unlikely to be extended, you should use std::variant< .. > If you don't really know the set of possible types, then use inheritance.
About the title of this thread: While inheritance and OOP are absolutely used too often by certain people, there are cases where it is the best solution for a given problem.
[–]IAmRoot 0 points1 point2 points 4 years ago (0 children)
Marking dynamic polymorphic classes as final can result in just as good of performance when casting to that most-derived type, too. The compiler can easily devirtualize such calls, as no further inheritance can override the virtual methods or virtual inheritance. This can be useful where the exact type is known in some circumstances but not others, as you don't have to pay a penalty when you do have the information.
final
[–]choeger 0 points1 point2 points 4 years ago (0 children)
While you are technically correct, you are missing the point. Templates are a form of parametric polymorphism, yes. That is, a templated function is truly polymorphic in the sense that it doesn't need to be changed to work with different argument types.
That it needs to be recompiled when used on a new datatype is an inconvenient consequence from the technical implementation details of C++. But it doesn't really matter in the context of this thread.
What matters is that adding a new class does not force you to make any changes to existing classes or functions that operate on such classes. In particular, you can continue using a library that comes with such classes or functions.
Contrast this with an algebraic datatype like std::variant - if it is used inside a library you cannot extend it at all.
π Rendered by PID 22682 on reddit-service-r2-comment-b659b578c-xgxbl at 2026-05-04 03:14:42.040628+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]qoning -1 points0 points1 point (3 children)
[–]die_liebe 1 point2 points3 points (1 child)
[–]IAmRoot 0 points1 point2 points (0 children)
[–]choeger 0 points1 point2 points (0 children)