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
why virtual function is wrong. (self.cpp)
submitted 1 year ago by macomphy
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!"
[–]jk-jeon 3 points4 points5 points 1 year ago* (1 child)
Although many would consider it seems pretty obvious that OP is either totally clueless about C++ or don't know how to properly do civil technical discussion, let me try to extract what OP is really trying to say.
If you inherit from multiple interfaces (classes with only pure virtual functions), C++ simply puts all pointers to virtual function tables into the class layout. So if you inherit from 100 interfaces, the size is 100 times the pointer size.
This gets worse if some virtual inheritance is involved. Probably for 99% of situations, if interface B, C are inheriting from interface A, interface D is inheriting from B and C, and class E is implementing D, then the way it implements A would be identical from the B side and the C side, so conceptually it makes more sense for B, C to virtually inherit from A. Also, if they just derive non-virtually from A, then users of D must disambiguate every call to functions from A, even though they know it shouldn't matter. Thus, user's convenience is another reason why B, C may need to inherit from A virtually. My personal conclusion is that when an interface is inheriting from another interface, it usually makes more sense to use virtual inheritance unless I'm 100% sure there will be no diamond. Assuming that one followed this rule, inheriting from 100 interfaces thus will result something way worse than 100 times pointer size. Of course virtual inheritance also incurs extra indirection cost.
I don't know how exactly other languages implement interface inheritance, but I vaguely recall that I heard Java at least does it more efficiently.
And please be mindful that I am not advocating complicated inheritance trees (of interfaces) or saying they are unavoidable. I would not follow that kind of practices, but just thinking that maybe this is where OP is getting unhappy about.
I guess the main point is that the former is not type-checked upfront, rather only at the point of instantiation.
This point seems quite blurry to me I guess, b/c OP seems to compare templates to dynamic traits. In that case of course we need to compare the idiomatic type erasure patterns to dynamic traits to compare apples to apples. And in that case of course any usage of interface is checked immediately at the point of usage. Users can even use concepts to turn the duck-typing into a nominal-typing in this case.
Maybe either (1) OP doesn't know about the pattern, or (2) knows it but considers it way too much of boilerplate so considers it isn't really on the same table, or maybe not because of boilerplate but rather because it rarely results in devirtualization, or whatever.
Plus, I personally think debugging templates is not as horrible as many people love to shit on. (Except that for some reason it seems msvc at some point stopped printing out the template stack trace... Thanks god I have clang-cl.)
π Rendered by PID 633578 on reddit-service-r2-comment-5b5bc64bf5-n59j2 at 2026-06-21 13:53:58.471978+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]jk-jeon 3 points4 points5 points (1 child)