I wanted to make the future developers' life easier by showing them the right way about implementing things in the framework. This idea may be dumb/impractical/pointless. Even if it's pointless, I'm OK with that. At least I'll learn something by having a dumb idea. :) |
I wouldn't want to suggest pointless, or even misguided. Quite to the contrary. My own focus has been as a developer/engineer, so using C++ to write ambitious targets is a long practice for me. I was thinking, and perhaps erroneously interpreting, the nature of the descriptive term "force". Without any context to that point I wasn't sure what the implications, but from a software writer's perspective we thrive and many insist upon flexibility. Sometimes the various synonyms of "force" translate into brittle designs, but I sense now that may not be a problem for you.
Virtual classes are among the oldest paradigms in C++, and sometimes that shows its age. It has been successful, but was way overused in the early years (I remember, I was there when the language was new). It does present an interface which guide derived classes into compliance.
You may be a year early, though. Concepts, which are the C++20 expansion of templates, may be extremely expressive for your purposes. The problem is that the standards were just declared feature complete, and it will be until 2020 before compilers offer significant support. Only GCC offers limited support at this time, as an experimental option.
In the meantime, policy based templates can be summarized thus:
1 2 3
|
template <typename T> class A : public T
{
};
|
This is loosely related to the CRTP I mentioned, but is not exactly CRTP. Here, the template parameter becomes the base of A, which then infuses selected options into the resulting class. If one created class B, which was similarly declared to derive from its template parameter, this notion can nest.
While a virtual base class provides an interface which derived classes are expected to consume, class A would impose requirements upon T by virtue of the interface it expects (and would have code to use).
It is not customary for T to be a user class, but it could be. This provides compile time selection of behaviors without imposing the minor performance implications of virtual functions.
Alexandrescu uses this in Loki (also a project in Github as I recall, freely available). He builds a smart pointer with this technique, such that various types of pointers are assembled by policies (the T in the example above) and typedef'd to suit particular purposes.
I'm delighted to discover a genuine new member with real questions. We do see a large number of "if/then/else" questions and other elementary inquiry, some of them are worthwhile because of the member's personality. I've met a few here and on another site where pages of exchanges formed a kind of semester of education on some theory of the language or computer science.
I would be fascinated to learn more about where you're headed. I had the pleasure of assisting a PhD candidate in 2005, in those last days before his thesis had to be finished for the deadline. Though his code was beautifully done, it was written for a theoretically perfect computer with unlimited resources. It was crashing as a result. His doctorate was in computer science, but he almost missed the deadline. What he needed was an engineer to consult with, and I was privileged to participate.
I hope you have more time to work on this.