0

Given these class examples,

class A {};
class B : A {};
class C {};
class D : A, C {};
class E : D {};

The function should return the number of inheritances of class A as 0 whereas for classes B, C, D and E this function should return 1, 0, 2, 1 respectively.

Note: The function shouldn't take account the whole inherited classes (for example, E takes D that is inherited from A and C indirectly, so it would has 3 but in my case I want to know the number of inheritances of the class in its declaration)

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Jordi Espada
  • 419
  • 4
  • 13
  • 1
    Afaik C++ doesn't support this kind of reflection feature. You'd have to implement some kind of counter yourself. – nada Sep 12 '19 at 10:03
  • 2
    there was a proposal for `std::direct_bases`, but rejected [what-is-the-status-of-n2965-stdbases-and-stddirect-bases](https://stackoverflow.com/questions/18435001/what-is-the-status-of-n2965-stdbases-and-stddirect-bases) – Jarod42 Sep 12 '19 at 10:05
  • The closest thing I can think of would be to do this at compile time using `std::is_base_of` – Clonk Sep 12 '19 at 10:11
  • 6
    Why do You need this? – Robert Andrzejuk Sep 12 '19 at 10:39
  • 1
    This is not possible. Unfortunately, C++ does not work this way. – Sam Varshavchik Sep 12 '19 at 11:06
  • @RobertAndrzejuk I need this feature for my script engine that can bind classes from c++. The reason of this is, mainly, to avoid user declares a class with multiheritance. From the responses, sadly, now I know there's no std function that returns the number of inheritances. – Jordi Espada Sep 12 '19 at 12:12
  • @JordiEspada that should probably have been the question instead - you are asking X, but you are actually interested in Y - it's not the same thing really – darune Sep 12 '19 at 13:09
  • @darune You are right! but this should be another question that asks if exists a "std" method that knows whether any class given has multiheritance or not. This question is already answered and it is what I wanted to know. Thanks :-) – Jordi Espada Sep 12 '19 at 13:55
  • @JordiEspada if I understand correctly isn't your question: How to block multiple inheritance from a single class? Or: how to allow a class only once in a inheritance hierachy? – Robert Andrzejuk Sep 12 '19 at 14:27
  • But again: [Why](https://en.wikipedia.org/wiki/Five_Whys) do You want to do that? – Robert Andrzejuk Sep 12 '19 at 14:38
  • @RobertAndrzejuk what I need is basically to know whether one class has two or more direct inheritance hierarchy at runtime. The "why" it takes long to explain, but as a summary I can say that I could use this feature to warn or give error to user when it passes a class with multiple inheritance into engine script I'm developing. Because the engine can also register pointer offsets from class variable members ensures the same offset for one inheritance only whereas multiple inheritance can give a repeated or wrong variable member offset. – Jordi Espada Sep 14 '19 at 10:17
  • A simple example , class A{ int a; // a: it registers pointer this + offset 0 } class B{ int b; // b: it registers pointer this + offset 0 } class C:A,B{ // a inheritates pointer this + offset 0 from A // b inheritates pointer this + offset 0 from B } class D:A{ // a inheritates pointer this + offset 0 from A int d; // d: pointer this + offset 4 } class E:B{ // b inheritates pointer this + offset 0 from B int e; // e: pointer this + offset 4 } – Jordi Espada Sep 14 '19 at 10:23
  • From the previous example I've commented, Class C inheritates the same offset registered from variable registered in A and B, whereas D and E variable offsets takes the right offset because they take just ONE inheritance. I hope is clear the aim of the "why" of the purpose of this question I've asked. – Jordi Espada Sep 14 '19 at 10:26
  • I'm trying to understand, but You are fixated on this solution, and describing how this solution will fix Your problem. I believe another solution is possible. But what is Your problem? (have a look at [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – Robert Andrzejuk Sep 14 '19 at 11:37
  • @RobertAndrzejuk Well I'm not fixated on this solution really, but the solution of this question would have solve my problem. The question name would have been better on this way: "Is there any way to know whether a class has single inheritance or multiheritance?" but, I think that I cannot redesing the entire question once is made. But in the end, this question is valuable because, the fact, I didn't find on internet a way to count the number of inheritances of a class and I have tried luck on stackoverflow. Next time I will try my best to describe the root of what I need. – Jordi Espada Sep 14 '19 at 17:48
  • @JordiEspada im real curious, what are You using inheritance to create? – Robert Andrzejuk Sep 14 '19 at 18:57

1 Answers1

1

Unfortunately, this hasn't entered the language yet. There were std::direct_bases proposal but it was rejected: What is the status of N2965 - std::bases and std::direct_bases?

So you need to roll you own - there are many ways you could go about this, but for example it could be something like the following:

#include <tuple>

    class A{
        public:
      using bases = std::tuple<>;
    };

    class B:A{
        public:
      using bases = std::tuple<A>;
    };

    class C{
        public:
      using bases = std::tuple<>;
    };

    class D:A,C{
        public:
      using bases = std::tuple<A,C>;
    };

    class E:D{
        public:
      using bases = std::tuple<D>;
    };

    template <class T>
    size_t count_bases() {
        return std::tuple_size<typename T::bases>::value;
    }

    int main() {
      return count_bases<D>();//returns '2'
    }

Try it yourself: https://godbolt.org/z/u0qoZa

darune
  • 10,480
  • 2
  • 24
  • 62
  • Thanks. Unfortunately this doesn't fits what I need. I need some kind of automatic procedure to avoid to modify all classes. It's a pitty that std::bases and std::direct_bases were rejected. I wonder if there's a way to detect whether a class has multiheritance instead of know the number of inheritances of a class. – Jordi Espada Sep 12 '19 at 12:19
  • 1
    @JordiEspada it's not possible with out some work on your side (given current language) - you could also build a list of all types 'you are interested in' (it will be impossible in practice due to template classes) - and then check against list with https://en.cppreference.com/w/cpp/types/is_base_of – darune Sep 12 '19 at 13:03