Compile time type determination in C++

后端 未结 6 1833
囚心锁ツ
囚心锁ツ 2021-02-01 22:26

A coworker recently showed me some code that he found online. It appears to allow compile time determination of whether a type has an \"is a\" relationship with another type. I

6条回答
  •  孤独总比滥情好
    2021-02-01 23:05

    The original code will construct an object of Derived, it may brings unexpected result. Below work may be an alternative choice:

    template
    inline  bool    isDerived(const CheckT &t){
        char    test(const BaseT   *t);
        char    (&test(...))[2];
        return  (sizeof(test(&t)) == sizeof(char) );
    }
    

提交回复
热议问题