Comments (4)
@jwakely : About suggestion 4: It was the right link, but on second thought I agree that this is not a well-enough-baked idiom to suggest in the guidelines. I didn't mean the static-reflection-based idea, I meant the manual vtable approach. There is the dyno library for that, mentioned in the video as well, but - yeah, on second thought I wouldn't recommend it in a "core guidelines" document.
from cppcoreguidelines.
filling classes up with functionality which isn't inherently part of the class.
Do you have an example of this? It's not a problem I think I've encountered.
a polymorphic base class becomes a kitchen sink pulling in the code of everything which uses that class.
This seems like a slippery slope fallacy. Do you have an example of how following the guideline leads to this?
implement the three methods:
This approach seems to be solving a different problem. If you have the concrete classes, that is, you have objects of static types A, B, and C, why would you be using a virtual function A::foo() or casting from A to B or C in the first place? The guideline is talking about code which has a parameter of type A& and wants to use features of the derived types. It's not saying that when you already have the derived types you should discard all that type information and push everything into a virtual function and call it through a reference to the base type. It's talking about cases when you don't have the derived types. In those cases, it says you should use virtual functions instead of assuming/guessing/detecting the derived types and casting to them.
If the suggestion is that code should not take A& and should just work directly with concrete types, that's a very different approach, but not really in scope for this C.153 guideline. Duplicating every API for every concrete derived type is not practical, and sometimes not even possible (not all derived types are known initially). But if you know the concrete types and want to access functionality of those types, sure, go ahead and access it directly. The guideline is not saying you can't do that.
Maybe this C.153 guideline should be prefixed by saying "when using OOP-style class hierarchies..." whereas your challenge seems to be more like "don't use OOP hierarchies". That seems far broader than C.153 and needs more than a single example with three overloads to demonstrate it.
from cppcoreguidelines.
- Consider suggesting value-semantics-maintaining dynamic 'hand-crafted' dynamic polymorphism, as in This CppCon 2023 talk.
Did you paste the right link? That's from 2020 and to quote the video:
"It's a lot, of course you can do a bunch of stuff with macros of clever templates to make this easier, but the point is I don't want to have to do this for every class. [...] It's a lot of work, it's a lot of things to get wrong. And because it's kind of weird code, it's easy to get wrong."
And then the rest of the talk is about how it might be done better in a hypothetical future version of C++.
I don't think that is suitable for the Core Guidelines. These are supposed to be tried and tested techniques that can be applied to most code bases by most developers. Not advanced techniques for experts from leading edge conferences, and certainly not things that aren't even in C++ today.
Value semantics and dynamic polymorphism without virtual
are certainly powerful and useful, but I don't think they're sufficiently well understood to say "stop writing classic OOP-style code and do this instead". At least not in this document, yet.
from cppcoreguidelines.
filling classes up with functionality which isn't inherently part of the class.
Do you have an example of this? It's not a problem I think I've encountered.
a polymorphic base class becomes a kitchen sink pulling in the code of everything which uses that class.
This seems like a slippery slope fallacy. Do you have an example of how following the guideline leads to this?
Think of one of the quintessential class hierarchy example: Geometric shapes or UI widgets. You need to paint them, you need to move them, you need to serialize them (possibly in more than one way, e.g. for debug-printing and for persistence when shutting down an app), you may need to check whether they cover a given point, whether they intersect another shape or widget, you may need to compute the union of shapes, you need to compute the convex hull of shapes, you need to apply a certain style to them given a style rule object, etc. That's already quite a lot of stuff, and (almost) none of it is inherent to the class.
But regardless - even doing this once breaks C.4 . I was just taking this to the extreme to emphasize that point.
from cppcoreguidelines.
Related Issues (20)
- Ranges require exception to F.19 HOT 7
- Wrong links in P.8 HOT 1
- Halo HOT 1
- Remove enforcement rules of rule F.17 HOT 7
- ES.79 is a terrible advice and should be dropped HOT 14
- C.21 Exception for copy-and-swap idiom HOT 4
- Broken links HOT 3
- F.20 should exclude swap HOT 3
- SF.2 allow inline variables and static inline class member variables HOT 1
- F.16 (pass by reference to const / by value): Request for clarification HOT 6
- Poor (and nearly-incorrect) example for rule F.27 (shared_ptr) HOT 4
- ES.56 Clarification on moving when assigning to reference outside of scope HOT 3
- F.16 Allow pass-by-value for types that are copied HOT 2
- `gsl::suppress` issues collected: Strings, literals, cases HOT 2
- Hi
- New NL Rule suggestion: Place comment-only lines before the code they apply to HOT 3
- Should/can Expects and Ensures be macros? HOT 1
- Check for invalidated iterator/dangling reference on heap reallocation HOT 2
- Turning off clang unsafe buffer warnings fails when compiling with a gcc based compiler HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppcoreguidelines.