【发布时间】:2011-05-11 17:45:59
【问题描述】:
考虑以下 C++ 代码,
template <typename Derived>
struct A
{
bool usable_;
};
template <typename Derived>
struct B : A< B<Derived> >
{
void foo()
{
usable_ = false;
}
};
struct C : B<C>
{
void foo()
{
usable_ = true;
}
};
int main()
{
C c;
}
我得到编译错误:在成员函数void B<Derived>::foo():
template_inherit.cpp:12: 错误: 'usable_' 未在此声明 范围。
这是为什么呢?有什么好的办法吗?
【问题讨论】:
-
struct B : A< B<Derived> >wat. -
@GMan haha CRTP 变相 :)
标签: c++ templates inheritance base-class