带有参数化基类的c++ CRTP
C++ CRTP with parametized base class?
我正在尝试使用CRTP与一个小的变化。我有一个派生类模板,并希望将其应用于多个基类。但这要么是不可能的,要么就是我的语法不对。下面的代码无法编译,但希望能说明我想要实现的目标:
template <class Derived> struct BaseCats { /* ... */ };
template <class Derived> struct BaseDogs { /* ... */ };
// ....
template <class Derived> struct BaseN { /* ... */ };
template <template <class> class Base>
struct Wrapper
:
Base<Wrapper> // compile error - Wrapper is not a complete type
{
Wrapper(int n)
{
// I do not want to rewrite or forward this
// constructor or Wrapper's operators
}
};
typedef Wrapper<BaseCats> Cats;
typedef Wrapper<BaseDogs> Dogs;
// ...
typedef Wrapper<BaseN> MyTypeN;
这能做到吗?
编辑:我想达到什么目的?
我重命名了上面的一些代码,以使用"狗和猫"的比喻。可能存在如下函数:
void BaseCats<Derived>::print() const
{
std::cout << (static_cast<const Derived *>this)->n << " catsn";
}
但是Wrapper
将包含狗和猫共同的构造函数和操作符。一种反向多态性,基类具有专门化。这样做的原因是不必为每个特化重写或转发构造函数和操作符。
你的编译错误可以这样解决:
Base<Wrapper<Base> >
你忘记了模板参数
相关文章:
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- CRTP 单一实例不完整类型或非文本类型
- CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc
- 将具有固定签名的自定义函数名称注入 CRTP
- 如何在MISRA C++之后实施CRTP
- 检测使用 CRTP 的类中的成员函数
- 简化使用 CRTP 模式的类的声明
- C++ CRTP initialization
- 概念可以与 CRTP 习语一起使用吗?
- CRTP - 危险的内存访问?
- 如何断言 CRTP 的函数为最终函数?
- CRTP 模式不会触发完整的模板实例化
- 在 CRTP 中使用嵌套名称说明符
- CRTP 与基类向量
- 派生的成员 通过指向基、static_cast、crtp、删除模板的指针
- 为什么 CRTP 模板C++给出无效参数错误?
- CRTP相对于抽象类的优势
- 具有unique_ptr的 CRTP 会导致段错误
- CRTP 如何使派生类具有基类的容器
- 使用 CRTP 实现单例