小编sbl*_*att的帖子

C++ 11可变参数模板+继承

我想用可变参数模板编写线性叠加的抽象.为此,我想定义一个表示某种形式的operator()的基类型

template <typename Result, typename... Parameters>
class Superposable {
  public:
    typedef Result result_t;
    void operator()(Result& result, const Parameters&...) const = 0;
};
Run Code Online (Sandbox Code Playgroud)

然后从它继承当前的问题,例如像这样

class MyField : public Superposable<double, double, double> {
  public:
    void operator()(double& result, const double& p1, const double& p2) const {
      result = p1 + p2;
    }
};
Run Code Online (Sandbox Code Playgroud)

然后我想编写一个抽象基类,它可以形成线性叠加,并将Superposable派生类作为模板参数来确定operator()的调用签名.我想有类似的东西

template<typename S> // where S must be inherited from Superposable
class Superposition {
  private:
    std::vector< std::shared_ptr<S> > elements;
  public:

     // This is the problem. How do I do this? …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates variadic-templates c++11

3
推荐指数
1
解决办法
2819
查看次数

标签 统计

c++ ×1

c++11 ×1

inheritance ×1

templates ×1

variadic-templates ×1