【发布时间】:2021-08-09 21:12:14
【问题描述】:
假设有两个基类Base1 和Base2 需要CRTP 模式。
template <typename TDerived>
class Base1 {
};
template <typename TDerived>
class Base2 {
};
现在我想定义一个Derived 类,这样它就可以用一个基类“参数化”。
在 C++ 中定义它的正确方法是什么(如果重要的话是 C++17)?
下面是伪C++代码
template <template <typename> class TBase>
class Derived : public TBase<Derived<TBase>> {}; // Recursion problem here
实际上,这样的派生类是适用于任何基类的扩展。
【问题讨论】:
-
你为什么要这个?
-
@TedLyngmo 这就是所谓的curiously recurring template pattern (CRTP)
-
@TedLyngmo 当然,你是对的。我打字太快了。我会修改原帖。我认为问题仍然存在。
标签: c++ templates inheritance c++17