次のコードはどのように機能しますか?
typedef char (&yes)[1];
typedef char (&no)[2];
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
//Test sample
class Base {};
class Derived : private Base {};
//Expression is true.
int test[is_base_of<Base,Derived>::value && !is_base_of<Derived,Base>::value];
B
それはプライベートベースであることに注意してください。これはどのように作動しますか?operator B*()
constであることに注意してください。どうしてそれが重要ですか?なぜ
template<typename T> static yes check(D*, T);
より良いのですstatic yes check(B*, int);
か?
注:これは、の縮小バージョン(マクロは削除されています)ですboost::is_base_of
。そして、これは幅広いコンパイラで機能します。