Thread: compiletime test for inheritance between classes

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    compiletime test for inheritance between classes

    a while back, a guy i worked with came up with a really cool bit of code; it would tell you if one class inherited from another, and this test was done at compile time.

    it was used like this:

    Inheritance<Base,Derived>::exists

    i could really use it now, but i don't have his code and i don't know how he did it. any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it probably used dynamic_cast. it could be done like this at runtime:

    Code:
    template<typename Base, typename Derived>
    bool InheritanceExists()
    {
      Derived* derived;
      return dynamic_cast<Base*>(derived);
    }

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    actually it didn't. it was a pure compile-time operation. i just found it. it's on p34 of alexandrescu's modern c++.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Boost has some interesting meta template stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    definitely not worth pulling in boost over. the implementation is trivial, actually.

    it just so happens this is in the book preview:

    Modern C++ design: generic programming and design patterns applied - Andrei Alexandrescu - Google Books

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Boost has a lot of other goodies, too. Boost is always worth having.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance and Template Classes
    By brunion1 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2011, 02:23 AM
  2. Help with classes and inheritance!!
    By alacahzam in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2011, 11:39 AM
  3. help in inheritance classes
    By Agnesa in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2003, 10:43 PM
  4. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM
  5. Classes & Inheritance
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2002, 06:25 PM