简体   繁体   English

C++:为模板参数指定基础 class

[英]C++: specifying a base class for a template parameter

I need to design a framework that computes the result of a divide-et-conquer algorithm in parallel.我需要设计一个框架来并行计算分而治之算法的结果。 In order to use the framework, the user needs to specify somehow the procedure that implements the "divide" phase (a function from T to T), the "conquer" phase (a function from D to D) and T and D themselves.为了使用该框架,用户需要以某种方式指定实现“除”阶段(从 T 到 T 的 function)、“征服”阶段(从 D 到 D 的 function)和 T 和 D 自己的过程。

I've thought it would be nice to define two abstract classes, BaseDivide and BaseConquer , which declares a pure virtual method compute with the right types: that way I have a type which implements a well-defined concept (from the point of view of the framework) with the user-definable function included by means of derivation of the abstract classes.我认为定义两个抽象类BaseDivideBaseConquer会很好,它们声明了一个具有正确类型的纯虚方法compute :这样我就有了一个实现了明确定义的概念的类型(从框架)通过抽象类的派生包含用户可定义的 function。

I've thought to use templates to pass the types to the framework, so the user doesn't have to instantiate them in order to use the framework, so something like that:我曾考虑使用模板将类型传递给框架,因此用户不必为了使用框架而实例化它们,所以像这样:

template <typename T, typename D, typename Divide, typename Conquer> 
D compute(T arg);

My problem is that I want that Divide and Conquer to be derived types of BaseDivide and BaseConquer : there is a way to enforce it at compile time?我的问题是我希望 Divide and Conquer 是BaseDivideBaseConquer的派生类型:有一种方法可以在编译时强制执行它吗? Also: do you think I can achieve a similar result with a cleaner design?另外:您认为我可以通过更简洁的设计达到类似的效果吗?

You could create the base classes like this:您可以像这样创建基类:

struct BaseDivide {
    enum EnumDiv { derivedFromBaseDivide = true };
}

template <typename T, typename D, typename Divide, typename Conquer> 
    static_assert(D::derivedFromBaseDivide);
    D compute(T arg);

What is the purpose of the additional Divide and Conquer template parameters?附加的分而治之模板参数的用途是什么? Are you sure you need them?你确定你需要它们吗?

You don't need to use templates for this purpose.您不需要为此目的使用模板。 Instead, you can use pointers to BaseDivide and BaseConquer objects, and polymorphism will do the job for you.相反,您可以使用指向 BaseDivide 和 BaseConquer 对象的指针,多态性将为您完成这项工作。

Use Boost.EnabelIf to trigger SFINAE when your types don't fulfill your requirement.当您的类型不满足您的要求时,使用 Boost.EnabelIf 触发 SFINAE。 Checking if T is derived from U is doen with boost::is_base_of:使用 boost::is_base_of 检查 T 是否从 U 派生:

#include <boost/type_traits/is_base_of.hpp>
#include <boost/enable_if.hpp>

template <typename T, typename D, typename Divide, typename Conquer> 
typename boost::
enable_if_c< boost::is_base_of<BaseDivide,Divide>::value 
          && boost::is_base_of<BaseConquer,Conquer>::value
          ,D
          >::type
compute(T arg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定类的基类的C ++模板参数的用途和缺点? - Uses and drawbacks of C++ template parameters specifying a class' base classes? C ++将派生类作为基类模板参数传递 - C++ Passing a derived class as a base class template parameter C ++模板在参数中指定类型 - C++ template specifying type in the parameter C++:模板成员 function 模板化 class 的特化,但未指定 ZA2F2ED4F8EBC04CBB14C21A2DDC - C++: Template member function specialization of a templated class without specifying the class template parameter C ++模板并以基类和子类作为模板参数进行转换 - C++ templates and casting with base and sub class as template parameter 通过模板的侦听器模式,如何在不指定模板参数的情况下使用模板化类? C ++ - Listener Pattern via Templates, how to use templated class without specifying template parameter? c++ 如何在 C++ 中重载作为参数传递给模板类的基类方法? - How to overload a method of base class passed as a parameter to a template class in C++? C ++模板类函数作为模板参数 - C++ Template class function as template parameter 在派生类中指定基类的C ++顺序 - C++ order of specifying base classes in derived class 通过派生类模板参数指定其中一个基类模板参数。 如何使用基类使用声明 - Specifying one of base class template parameters by derived class template parameter. How to use base class's using declarations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM