views:

426

answers:

4

We have a restriction that a class cannot act as a base-class for more than 7 classes. Is there a way to enforce the above rule at compile-time?

I am aware of Andrew Koenig's Usable_Lock technique to prevent a class from being inherited but it would fail only when we try to instantiate the class. Can this not be done when deriving itself?

The base-class is allowed to know who are its children. So i guess we can declare a combination of friend classes and encapsulate them to enforce this rule. Suppose we try something like this

class AA {
   friend class BB;
   private:
      AA() {}
      ~AA() {}
};

class BB : public AA {

};

class CC : public AA 
{};

The derivation of class CC would generate a compiler warning abt inaccessible dtor. We can then flag such warnings as errors using compiler tweaks (like flag all warnings as errors), but i would not like to rely on such techniques.

Another way, but to me looks rather clumsy is:-

class B;

class InheritanceRule{
    class A {
    public:
     A() {}
     ~A() {}
    };
    friend class B;
};

class B {
public:
    class C : public InheritanceRule::A
    {};
};


class D : public InheritanceRule::A{};

The derivation of class D will be flagged as a compiler error, meaning all the classes to be derived should be derived inside class B. This will allow atleast an inspection of the number of classes derived from class A but would not prevent anyone from adding more.

Anyone here who has a way of doing it ? Better still if the base-class need not know who are its children.

NOTE: The class which acts as a base-class can itself be instantiated (it is not abstract).

Thanks in advance,

EDIT-1: As per Comment from jon.h, a slight modification

// create a template class without a body, so all uses of it fail
template < typename D> 
class AllowedInheritance;

class Derived; // forward declaration
// but allow Derived by explicit specialization 
template<> 
class AllowedInheritance< Derived> {};

template<class T>
class Base : private AllowedInheritance<T> {};

// privately inherit Derived from that explicit specialization    
class Derived : public Base<Derived> {};

// Do the same with class Fail Error
// it has no explicit specialization, so it causes a compiler error
class Fail : public Base<Fail> {}; // this is error

int main()
{   
   Derived d;

   return 0;
}
+2  A: 

Lots of the various static code analysis tools provide information about inheritance hierarchy. Rather than try and handle it in your code, I would look into a tool that could set up some rules for inheritance hierarchy and fail the build if those rules are not followed. Might cost a little $ and you might have to write a custom rule (I've seen inheritance depth, but not inheritance "breadth" like you want). But, in the long run, I think that's your best bet.

Per comment: I've used Coverity with some success. Bit spendy. There are several good SO threads that may have better options.

JP Alioto
Thanks for the suggestion. Can you kindly quote the tool that has given you a good experience w.r.t to static code analysis? I will check with the same.
Abhay
+2  A: 

I'm tired as crap, can barely keep my eyes open, so there's probably a more elegant way to do this, and I'm certainly not endorsing the bizarre idea that a Base should have at most seven subclasses.

// create a template class without a body, so all uses of it fail
template < typename D, typename B> class AllowedInheritance;


class Base {};
class Derived; // forward declaration

// but allow Derived, Base by explicit specialization 

template<> class AllowedInheritance< Derived, Base> {};

// privately inherit Derived from that explicit specialization    
class Derived : public Base, private AllowedInheritance<Derived, Base> {};


// Do the same with class Compiler Error
// it has no explicit specialization, so it causes a compiler error
class CompileError: public Base, 
     private AllowedInheritance<CompileError, Base> { };

//error: invalid use of incomplete type 
//‘struct AllowedInheritance<CompileError, Base>’


int main() {

   Base b;
   Derived d;
   return 0;
}

Comment from jon.h:

How does this stop for instance: class Fail : public Base { }; ? \

It doesn't. But then neither did the OP's original example.

To the OP: your revision of my answer is pretty much a straight application of Coplien's "Curiously recurring template pattern"]

I'd considered that as well, but the problem with that there's no inheritance relationship between a derived1 : pubic base<derived1> and a derived2 : pubic base<derived2>, because base<derived1> and base<derived2> are two completely unrelated classes.

If your only concern is inheritance of implementation, this is no problem, but if you want inheritance of interface, your solution breaks that.

I think there is a way to get both inheritance and a cleaner syntax; as I mentioned I was pretty tired when I wrote my solution. If nothing else, by making RealBase a base class of Base in your example is a quick fix.

There are probably a number of ways to clean this up. But I want to emphasize that I agree with markh44: even though my solution is cleaner, we're still cluttering the code in support of a rule that makes little sense. Just because this can be done, doesn't mean it should be.

If the base class in question is ten years old and too fragile to be inherited from, the real answer is to fix it.

tpdi
+1, Definitely much cleaner. Though i will have to check with the architect of this rule whether templates are allowed :-)
Abhay
How does this stop for instance:class Fail : public Base{};?
jon hanson
Rightly pointed-out, see my edit. But the solution was in the right direction and much cleaner than the originally posted one. Thanks
Abhay
@tpdi, ya we will ultimately have to clean-up the old-mess. Right now there are other important things, but may be soon! Cheers
Abhay
+3  A: 

Sorry, I don't know how to enforce any such limit using the compiler.

Personally I wouldn't bother trying to force the rule into the code itself - you are cluttering the code with stuff that has nothing to do with what the code is doing - it's not clean code.

Rather than jumping through hoops, I'd try to get that rule relaxed. Instead it should be a guideline that could be broken if necessary and in agreement with others in the team.

Of course, I lack the knowledge of exactly what you're doing so the rule could be appropriate, but in general it probably isn't.

Any programming "rule" that says you must never do x or you must always do y is almost always wrong! Notice the word "almost" in there.

Sometimes you might need more than 7 derived classes - what do you do then? Jump through more hoops. Also, why 7? Why not 6 or 8? It's just so arbitrary - another sign of a poor rule.

If you must do it, as JP says, static analysis is probably the better way.

markh44
I agree with your sentiments. What if your codebase is different for different platforms. Not all shops have the financial bandwidth to purchase such tools for every platform.Also, it is afterall a project guideline and we are checking if this can be enforced, becoz we think it is essential. You may despise it, no issues.There is obviously a special case why it needs to be done. We had a base class that was designed specially for *only some* classes which is being used without complete knowledge. The code for that dates back even before C++98. Imagine a special String/Iterator class.
Abhay
Fair enough. I don't despise the idea because it sounds like there is a legitimate reason. I sometimes encounter unjustified rules and guidelines that force people down the wrong path, and this initially sounded like one of them.
markh44
+1  A: 

Rather than cluttering the code with assertions, you might be able to use something like GCC-XML, which parses C++ source using the g++ compiler frontend and generates XML output. I expect that it would be reasonably straightforward to develop a tool that parses this output and checks for violations of the rule; this could then be integrated with source code check-in.

BTW, having base classes know about their descendants violates the Open-Closed Principle, meaning that it actually undercuts the usefulness of OO programming in general. The main reason for separating code into base classes and subclasses is so that the base class does not have to know about its subclasses -- this makes possible things like plugin packages delivered after installation.

j_random_hacker
+1 for the idea. The Open-Closed idiom is not to be disputed, but the problem domain was more like a compiler-firewall to prevent abuse of some guidelines. I feel this idea is better than having static code-analysis and then effecting changes especially for large codebases and large teams. Thanks
Abhay
I agree, there are cases where it's convenient to use inheritance for reasons besides subtyping in the formal sense. As with most things, "So long as you know what you're doing..." :)
j_random_hacker