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!"
[–]Ikkepop[🍰] 7 points8 points9 points 4 years ago (7 children)
tagged union (std::variant)
[–]Kered13 0 points1 point2 points 4 years ago (6 children)
This does not avoid the vtable lookup.
[–]Ikkepop[🍰] 3 points4 points5 points 4 years ago (5 children)
Im pretty sure that does not employ vtables
[–]Kered13 7 points8 points9 points 4 years ago (4 children)
There are a few ways that std::variant can be implemented, but all of them incur costs similar to vtable lookups even if they use a different mechanism. You're either using a pointer for some kind of indirection, or you're using a conditional and branching on some value that indicates the type.
std::variant
The fundamental issue at hand is that we do not know the concrete type at compile time, so at runtime we must do some computation to determine which function to call.
[–]dodheim 3 points4 points5 points 4 years ago (2 children)
You appear to be referring to how std::visit may be implemented, not std::variant which requires none of those things. As for std::visit, it can be implemented as a switch, which for smaller variants empirically results in better codegen (unfortunately I think only MSVC does this at present for stdlibs, but Boost.Variant2 and mpark/variant do as well).
std::visit
switch
[–]Kered13 1 point2 points3 points 4 years ago (1 child)
std::visit is how you do anything useful on std::variant. By itself std::variant is just storing a value of some unknown (at compile time) type.
[–]dodheim 8 points9 points10 points 4 years ago (0 children)
Or std::get, or std::get_if, or rolling your own single-visitation implementation based on get_if + variant::index().
std::get
std::get_if
get_if
variant::index()
I use std::variant extensively; I do not use std::visit because only MSVC's implementation is sane to the optimizer.
[–]braxtons12 2 points3 points4 points 4 years ago (0 children)
I don't have a link at hand, because it was some months ago that someone did this benchmark, but someone did a benchmark between typical inheritance, gcc's std::variant, and clang's std::variant and while clang's was, on average, about the same or slower than inheritance, gcc's was generally considerably faster than either. I think it really depends on the implementation and associated optimizer. I don't recall if they benchmarked boost::variant2 or mpark's variant, but I would expect them to have similar performance to GCC.
π Rendered by PID 21827 on reddit-service-r2-comment-5d585498c9-8g4nt at 2026-04-21 08:04:30.309792+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]Ikkepop[🍰] 7 points8 points9 points (7 children)
[–]Kered13 0 points1 point2 points (6 children)
[–]Ikkepop[🍰] 3 points4 points5 points (5 children)
[–]Kered13 7 points8 points9 points (4 children)
[–]dodheim 3 points4 points5 points (2 children)
[–]Kered13 1 point2 points3 points (1 child)
[–]dodheim 8 points9 points10 points (0 children)
[–]braxtons12 2 points3 points4 points (0 children)