简体   繁体   English

具有构造函数转发的类模板

[英]Class template with constructor forwarding

I wondering what was the best way of implementing the following. 我想知道什么是实现以下内容的最佳方法。

I have a number of classes that derive from an abstract class, BaseClass . 我有许多从抽象类BaseClass派生的类。 I want to create a new class ParallelBaseClass that basically performs a parallelization of all children of BaseClass . 我想创建一个新类ParallelBaseClass ,该类基本上对BaseClass的所有子级执行并行BaseClass

The design I was thinking of was this: 我想到的设计是这样的:

template <class D>
class ParallelBaseClass
{
  private:
     std::vector<D> _classes;

   public:

   ParallelBaseClass(int threads, constructor_args)
   {
       for(int i = 0; i < threads; i++)
         _classes.push_back(D(constructor_args));
   }
}

However, I don't know that this is the best design. 但是,我不知道这是最好的设计。 I have two issues: 我有两个问题:

  1. Ensuring that the template type is a descendent of BaseClass 确保模板类型是BaseClass的后代
  2. forwarding constructor arguments as shown above. 转发构造函数参数,如上所示。

Any guidance is greatly appreciated. 任何指导,不胜感激。

Thanks! 谢谢!

Following may help: 以下内容可能会有所帮助:

template <class Base>
class ParallelBaseClass
{
private:
     std::vector<std::unique_ptr<Base>> classes;

 public:

    template<typename Derived, typename ... Ts>
    void Add(Ts&&...args)
    {
        classes.push_back(std::make_unique<Derived>(std::forward<Ts>(args)...));
    }

};

Usage: 用法:

struct B { virtual ~B() = default; };
struct D : B { D(int) {} };

ParallelBaseClass<B> p;

p.Add<D>(42);

If the classes are such that they all have a function (for example execute ) that does something, and ParallelBaseClass should call it on several objects at the same time in different threads, this might be a design: 如果这些类都具有一个可以execute某些功能的函数(例如execute ),并且ParallelBaseClass应该在不同线程中的多个对象上同时调用它,则可能是一种设计:

#include <utility> // for std::forward

class Base {
public:
    virtual void execute() = 0;
}


class Example : public Base {
public:
    void execute() override { .... }
}

template<typename D>
class Parallel : public Base {
private:
    std::vector<D> objects_; // Called objects, since classes refers to the type and not instances of it
public:
    template<typename... Args>
    explicit ParallelBase(int threads, Args&&... args) {
        for(int i = 0; i < threads; ++i)
            objects_.emplace_back( args... );
            // not using std::forward<Args>(args)... here:
            // need to make (multiple) copies instead of moving
    }

    void execute() override {
        for(D& obj : objects_)
            //create thread and run objects_[i].execute()
    }
}

The constructor is a variadic template function that forwards the arguments to D 's constructor. 构造函数是可变参数的模板函数,它将参数转发给D的构造函数。 std::forward forwards than as rvalue reference or as reference when possible. std::forward比作为右值引用或尽可能的引用std::forward转发。 The ... expands the template parameter pack. ...展开模板参数包。 explicit makes sure that an int can't be implicitly converted to a ParallelBase using a construction with zero args arguments. explicit确保使用零args参数的构造将int不能隐式转换为ParallelBase

Here Base is abstract and polymorphic. 这里的Base是抽象的和多态的。 This would correspond to a composition design pattern. 这将对应于构图设计模式。 Parallel (not a base class here) corresponds to a parallelization of the functionality of D , and has an execute implementation itself. Parallel (此处不是基类)对应于D的功能的并行化,并且本身具有execute实现。

Update Fixed move construction. 更新固定的移动构造。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM