确保从父CRTP类派生的类实现功能 [英] Ensure that class derived from parent CRTP class implements function

查看:48
本文介绍了确保从父CRTP类派生的类实现功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简述:

我想确保派生类实现父CRTP类中某个函数所需的成员函数。

I want to make sure a derived class implements a member function required by a function within the parent CRTP class.

详细信息:

我有类似的代码

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 ) override
    {
        typename T::Params& typedParams = dynamic_cast<typename T::Params&>( p );
        static_cast<T*>( this )->myFunc( typeParams );
    }

};

class Imp : public CRTP<Imp>
{
public:
    class Params : public CRTP<Imp>::Params
    {
    public:
        virtual ~Params() {}

        int x, y, z;
    };

    virtual void myFunc( Imp::Params& p );
};

目的是我可以拥有多个 Imp 子类都在 myFunc 中执行不同的操作,并接受它们自己的必需参数。然后, Base 提供的接口将被更高级别的函数使用,这些函数只需要具有 Base :: Params Base 。我的问题是确保任何 Imp 提供专门的 myFunc 。为了避免无限递归, Imp 必须实现 myFunc

The intention is that I can have multiple Imp child classes all doing different things in myFunc and accepting their own required parameters. The interface provided by Base is then utilized by higher level functions that only need to have a pointer/reference of type Base::Params and Base. My problem is making sure that any Imp provides a specialized myFunc. To avoid infinite recursion Imp must implement myFunc.

我的第一次尝试是向 CRTP

virtual void myFunc( typename T::Params& p ) = 0;

但这不能用作 Imp 定义 CRTP 时尚未完全定义。 此问题使用 static_assert 这使我想到对 CRTP :: myFunc 中的 static_assert 进行相同的操作。

but that doesn't work as Imp hasn't been fully defined when CRTP is being defined. This question uses a static_assert which made me think of doing the same with the static_assert within CRTP::myFunc. Except I'm not sure what should be the expression in the static assertion for a non-static function.


  1. 我可以使用 static_assert 是否满足我的需求?

  2. 这是确保派生类具有所需功能的最佳/最简洁方法吗?

  3. 我对课堂设计感到很惊讶,并且有一种更好的做事方法吗?

  1. Can I use a static_assert for what I need?
  2. Is that the best/cleanest way to ensure the derived class has the needed function?
  3. Have I got carried away with my class design and there is a better way of doing things?

谢谢。

推荐答案

为什么不只是为函数使用其他名称?如果没有实现,对于 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 Imp 提供。

这篇关于确保从父CRTP类派生的类实现功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆