Type trait to identify primary base class

前端 未结 2 2015
迷失自我
迷失自我 2021-01-12 07:02

If I have a class Base, with at least one virtual function, and a class Derived which inherits singly from this then (uintptr_t)derived - (uintptr_t)static_cast

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 07:43

    This will be part of the next standard This was part of the aborted TR2 via the std::bases and std::direct_bases traits. If you happen to be working with a compiler that includes the draft-TR2, you might have support for this. For example in GCC 4.7.2:

    #include 
    #include 
    #include 
    
    struct T1 { };
    struct T2 { };
    struct Foo : T1, T2 { };
    
    
    int main()
    {
        std::cout << demangle::type>() << std::endl;
    }
    

    This prints:

    std::tr2::__reflection_typelist
    

    (The demangler is my own; you may have seen it elsewhere.)

    I trust you can build a suitable "is polymorphic and has precisely zero or one bases" trait yourself.

提交回复
热议问题