Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:48:13+00:00 2026-05-13T18:48:13+00:00

I’m wondering whether there is a way to find out whether a class is

  • 0

I’m wondering whether there is a way to find out whether a class is a direct base of another class i.e. in Boost type trait terms a is_direct_base_of function. As far as I can see, Boost doesn’t seem to support this kind of functionality, which leads me to think that it’s impossible with the current C++ standard.

The reason I want it is to do some validation checking on two macros that are used for a reflection system to specify that one class is derived from another, as in the example code below.

header.h:

#define BASE     A
#define DERIVED  B

class A {};
class B : public A 
{
   #include <rtti.h>
};

rtti.h:

// I want to check that the two macro's are correct with a compile time assert
Rtti<BASE, DERIVED> m_rtti;

Although the macros seem unnecessary in this simple example, in my real world scenario rtti.h is a lot more complex.

One possible avenue would be to compare the size of the this pointer with the size of a this pointer cast to the base type and somehow trying to figure out whether it’s the size of the base class itself away or something. (Yeah, you’re right, I don’t know how that would work either!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:48:14+00:00Added an answer on May 13, 2026 at 6:48 pm

    I asked myself, “What C++ constructs do differentiate between direct inheritance vs. indirect?” It comes to mind that C++ constructors of derived types directly call constructors for their direct base(s) only. So code like this:

    Derived::Derived() : Base() {}
    

    Is only valid if Base is is a direct base of Derived. And since you are injecting the code of rtti.h into the body of Derived, you can tolerate the restriction that this technique is only directly visible within the derived class itself (i.e. it is not as general as a hypothetical type_traits::is_direct_base_of, but does not need to be).

    So since we probably don’t want to mess around with the default constructors per se, how about adding some special-purpose ones?

    #define BASE     A
    #define DERIVED  B
    
    struct rtti_tag {}; // empty type
    
    class A
    {
    protected:
        A(rtti_tag) { assert(false); } // never actually called
    };
    
    #include <rtti.h>
    class B : public A
    {
        IS_DIRECT_BASE_OF(DERIVED, BASE); // fails to compile if not true
    };
    

    rtti.h:

    #define IS_DIRECT_BASE_OF(_B_, _A_) _B_(rtti_tag tag) : _A_(tag) \
        { assert(false); } // never actually called
    

    This code compiles for me with g++ 4.2; if I insert a new class in the inheritance hierarchy, the assertion breaks and compilation fails with what I think is a reasonably descriptive diagnostic:

    In constructor ‘B::B(rtti_tag)’:
    error: type ‘A’ is not a direct base of ‘B’
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.