【发布时间】:2012-04-11 07:44:04
【问题描述】:
如果我这样做
struct A{};
struct C:private A{};
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);
};
int main(){
std::cout<<is_base_of<A,C>::value<<std::endl;
}
我得到一个 1。我想在 C 是私有 A 时得到一个 0,当 C 是一个公共 A 时得到一个 1。
[代码来源于How does `is_base_of` work?]
【问题讨论】:
-
有点跑题了,但如果你的声望低于 X,你真的能提供 X 的赏金吗?
-
我删除了我的回答,基本上说“你不能”使用模板,以及可以做到这一点的模板替代方案。
标签: c++ templates inheritance