With C++17 is it possible to detect if a struct/class has any base?

Viewed 3263

I need a type trait which will be true if the given type derives from anything, and false otherwise.

For example:

template<class T>
struct is_inherit
    //... logic of inheritance detection
    ;

template<class T>
void AppLogic(){
    if constexpr(is_inherit<T>::value) {
        puts("T has base");
        //...
    } else {
        puts("T doesn't have base");
        //...
    }
}

struct A {};
struct C {};
struct B: C {};

int main() {
    AppLogic<A>(); // print: T doesn't have base 
    AppLogic<B>(); // print: T has base
}

Is it possible to somehow implement that "is_inherit" trait struct?


Why?

I am developing a manual stack frame builder for Windows x64. According the https://docs.microsoft.com/en-us/cpp/build/return-values-cpp documentation, if a type:

  • has a length of 1, 2, 4, 8, 16, 32, or 64 bits;
  • has no user-defined constructor, destructor, or copy assignment operator;
  • has no private or protected non-static data members;
  • has no non-static data members of reference type;
  • has no base classes;
  • has no virtual functions;
  • and has no data members that do not also meet these requirements;

then its return value is in the RAX register, otherwise the function has a hidden argument that I must detect and handle.

This used to be the definition of a C++03 POD, however in C++11 this changed:

Because the definition has changed in the C++11 standard, we do not recommend using std::is_pod for this test.

Up until now, with some conjugated traits I could detect if the type met the definition of a C++03 POD or not. However with C++17 the aggregate rules have changed, and that broke my solution.

If I can somehow detect whether a type T has any base class, my solution will work again.

2 Answers
Related