更新时间:2023-09-24 23:17:46
为什么不只是为函数使用其他名称?如果没有实现,对于 CRTP
类的每个派生,您都会遇到编译错误。考虑以下内容:
Why not just use a different name for the function? Then you will have a compilation error for each derivation of CRTP
class without and implementation. Consider this:
class Base
{
public:
class Params
{
public:
virtual ~Params() {}
};
virtual void myFunc( Params& p ) = 0;
};
template< typename T >
class CRTP : public Base
{
public:
virtual void myFunc( Base::Params& p ) final override
{
typename T::Params& typedParams = dynamic_cast<typename T::Params&>( p );
static_cast<const T*>( this )->myFuncImp( typedParams );
}
};
class Imp : public CRTP<Imp>
{
public:
class Params : public CRTP<Imp>::Params
{
public:
virtual ~Params() {}
int x, y, z;
};
};
int main(int argc, char** argv)
{
Imp imp;
}
由于没有 myFuncImp $,编译失败c $ c>由
Imp
提供。