Problem definition: Most projects have a document called 'Coding standards' that represent rules for how the programmers should use C++. Most of these documents basically define a subset of C++ that is enforced for that project. But the enforcement is usually done at the code review level where people point out when some C++ usage violates the rules. Since these code reviews aren't always as thorough as they should be, or there aren't any code reviews due to other constraints, wouldn't it be nice if the compiler could enforce these rules and wouldn't let a piece of code compile when the rules of the coding standard have been violated?
Solution (proposed): Since C++ has object oriented support, I thought of something similar. You have a base class that represents the full set of the C++ standard. In order to enforce this class you don't have to do anything, so all C++ projects out there can choose to implement this enforcement in their own time if they choose to. This base class can be derived to add rules: for example you add the rule that goto is banned. Then, you add something that would look like a preprocessor #ifdef - #endif block that means that for that part of code the subclass rules are enforced, thus any goto gets flagged and not allowed. These subclasses can be derived further and more and more rules be enforced. With each derivation, more things from the C++ standard are removed. So you only substract with each derived class, you cannot add a new rule that goto is allowed after its superclass banned it.
But, at the code level, the subclassing is not enforced, you can have a block that bans goto and inside have a subblock that allows it temporarily. The basic idea isn't to have a million of these classes enforcing a billion rules but to be able to divide the codebase in things like high-level code and low-level code. In the high level portion you aren't allowed to use things like pointer hacking, or naked pointers, while in the low level portion everything, or most things, are allowed. This way, if somebody is trying to modify some piece of code that he has never seen before, he doesn't need to guess the way that code should be written in that module just by looking at the neighbouring code, but can actually jump to the class that defines the rules and can read and follow them from there. And if he doesn't follow them he can't compile. If he chooses to create a subblock where he allows himself to do what he pleases, that is something that can be seen easier in the repository. Since creating new rules is basically creating new headers (or maybe a new type of file, special for these rules), it's easier to control who creates the rules in a team so that not everybody can use and create whatever rule he wants whenever he wants (that would defeat the purpose of trying to enforce the coding standard).
Since I've heard that the module concept is coming to the next version of C++, maybe this would be a good idea to enforce the coding standard at the module level.
Right now I am only interested to see the reaction for this proposal as I haven't mapped out the nitty gritty details and see what corners this design would force you into, so what do you think about the idea of finding a way to enforce a subset of C++ at the compiler level?