`is_base_of`是如何工作的?
以下代码如何工作?
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 sampleclass 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。这适用于各种编译器。
MM们
侃侃无极
叮当猫咪
随时随地看视频慕课网APP
相关分类