Compile time type determination in C++

后端 未结 6 1845
囚心锁ツ
囚心锁ツ 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:13

    It's pretty cool but it doesn't actually work, because user-defined conversion is preferred to ellipsis match, and const reference can bind the temporary result of a user-defined conversion. So char*p; is_related(p); would return true (tested in VS2010).

    If you want to truly test for an inheritance relationship, a similar approach can be taken but using pointers instead of references.

提交回复
热议问题