c++ - C++ 中的延迟评估包装类?

标签 c++ c++11 lazy-evaluation template-meta-programming

什么是实现 CRTP 延迟评估的包装类的好方法?

具体来说,我希望使用类似于 this question 的答案的延迟或惰性评估。并且有兴趣将 CRTP 派生类包装在通用类中,以便我可以执行以下操作:

Wrapper a = 1;      // Some type like Number<int>
Wrapper b = 2;      // Some type like Number<int>
Wrapper c = a + b;  // An expression like Add<Number<int>, Number<int>>

我认为智能指针包装器很有意义,因为我可以为 CRTP 基使用抽象基类并管理底层对象。 到目前为止,我已经实现了一个抽象 Base具有 enable_shared_from_this 的类这样我就可以创建一个 shared_ptr , 和一个 CRTPBase处理模板化表达式和类型的类。

class Base : public std::enable_shared_from_this<Base> {
public:
  virtual ~Base() {}
  // ... other virtual methods.

  // when we don't have the type.
  inline std::shared_ptr<Base> as_ptr() {
    return shared_from_this();
  }
};

template<typename Derived>
class CRTPBase : public Base {
public:
  // ... overridden virtual methods.

  inline Derived &derived() { 
    return static_cast<Derived &>(*this); 
  }

  inline const Derived &derived() const { 
    return static_cast<const Derived &>(*this); 
  }

  // ... other CRTP stuff.
};

然后,导出的表达式返回如下:

template<typename T1, typename T2>
class Add : public CRTPBase<Add<T1, T2>> {
private:
  const T1 &lhs_;
  const T2 &rhs_;

public:
  Add(const T1 &lhs, const T2 &rhs) : lhs_(lhs), rhs_(rhs) {}
}

template<typename T1, typename T2>
inline const Add<T1, T2> operator+(const CRTPBase<T1> &lhs, const CRTPBase<T2> &rhs) {
  return Add<T1, T2>(lhs.derived(), rhs.derived());
}

我想要一个 Wrapper可以采用类似 Number<T> 的类, Matrix<T> Add<T1, T2> 或任何源自 CRTPBase 的东西.

class Wrapper : public CRTPBase<Wrapper> {
private:
  std::shared_ptr<Base> ptr_;

public:
  // rule of zero?

  // example constructor to make a new Number<int>
  explicit inline Wrapper(int value) 
      : ptr_(std::make_shared<Number<int>>(value)) {}

  // what do these look like?
  template<typename T> Wrapper(const CRTPBase<T> &m) { ... }
  template<typename T> Wrapper(CRTPBase<T> &&m) { ... }  
  template<typename T> Wrapper &operator=(const CRTPBase<T> &m) { ... }
  template<typename T> Wrapper &operator=(CRTPBase<T> &&m) { ... }  
};

而且因为包装器也派生自 CRTPBase ,我可以像任何其他派生类型一样将它传递到表达式中。这里的目标是使用包装器,而不是实际类型。

如何将表达式返回的 CRTP 类包装在智能指针中?我对智能指针不是很有信心,我正在寻求帮助以了解在这个包装类中使用它们会是什么样子。

目前,我有这样的东西:

template<typename T> Wrapper(const CRTPBase<T> &&m) 
    : ptr_(std::make_shared<T>(std::move(m.derived()))) {}

哪个有效(我不明白为什么),但似乎是错误的。

最佳答案

我将发布对我有用的方法,使包装类在不默认为多态基类的情况下工作。

来 self 发布的 SO Code Review 问题的答案 here ,建议使用 Sean Parent 的运行时多态性概念,我能够消除 CRTP 多态性基类 Base .

template<typename Derived>
class CRTPBase {
public:
  inline Derived &derived() { 
    return static_cast<Derived &>(*this); 
  }

  inline const Derived &derived() const { 
    return static_cast<const Derived &>(*this); 
  }

  // ... other CRTP stuff.
};

然后我制作了 Add为了安全起见,类使用复制语义。

template<typename T1, typename T2>
class Add : public CRTPBase<Add<T1, T2>> {
private:
  const T1 lhs_;
  const T2 rhs_;

public:
  Add(const T1 &lhs, const T2 &rhs) 
      : lhs_(lhs), 
        rhs_(rhs) {}

  // ...
}

然后,可以创建一个具有类型隐藏多态嵌套类的包装类。

class Wrapper : public CRTPBase<Wrapper> {
private:
  struct Concept {
    virtual ~Concept() = default;

    // ... virtual implementation details.
  };

  template<typename T>
  struct Model final : public Concept {
    T data_;

    Model(T data)
        : data_(std::move(data)) {}

    // ... virtual overrides.
  };

  std::shared_ptr<const Concept> ptr_;

public:
  template<typename T>
  inline Wrapper(const CRTPBase<T> &value) 
    : ptr_(std::make_shared<Model<T>>(value.derived())) {}

  // ... functions to interface with ptr_.
};

这使我能够获得所需的语义,而无需强制携带抽象 CRTP 基类。这意味着我有 Add<Wrapper, Wrapper>类型,但我可以在自己的代码中使用静态多态性,并且运行时代码使用 Sean Parent 所描述的非侵入式运行时多态性形式。

我仍然不知道如何评估包装器中的表达式,主要是因为 Wrapper 中包含的类型现在上课 Concept s 或 Model<T> s,所以我在实际测评的时候还是遇到了问题,所以如果有人可以对此发表评论,将不胜感激:)

关于c++ - C++ 中的延迟评估包装类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375464/

相关文章:

c++ - 如何通过引用或指针访问二维 vector

c++ - DJGPP 的恐惧错误

c++ - 函数模板的这两个定义有什么区别?

lazy-evaluation - Agda 的评估策略在任何地方都有规定吗?

Haskell 键输入内存泄漏

clojure - 为什么 Clojure 的反向函数返回一个非惰性序列?

c++关于在自身中嵌入结构名称的困惑

c# - 使用语句实现与 C# 等效的 C++

c++ - 用于复制/转换对象的一系列函数的模板

C++ vector 实现 - move 构造函数 - move 与前进