[英]CRTP multiple level inheritance instantiate intermediate class with another template parameter
Given a multi level inheritance CRTP class structure like so, where the intermediate class B has another template parameter T, how should I instantiate an object of B?给定一个像这样的多级 inheritance CRTP class 结构,其中中间 class B 有另一个模板参数 T,我应该如何实例化一个 ZA2668CFDE69331C49B2696CFDE69331C45
template<class Derived, class T>
struct A {
void print(T t) { static_cast<Derived*>(this)->print(t); }
};
template<template <typename> class Derived, class T>
struct B : public A<Derived<T>, T> {
void print(T t) { cout << t << " B!\n"; }
};
template<class T>
struct C : public B<C, T> {
void print(T t) { cout << t << " C!\n"; }
};
int main() {
C<int> c;
c.print(5);
// how do I instantiate a B?
}
Here is an example of how you could do this.这是一个如何做到这一点的例子。
template <typename T>
struct ForB { };
B<ForB, int> b;
b.print(5);
A link to try it out.试用链接。 http://cpp.sh/2v5tu
http://cpp.sh/2v5tu
B itself is a template that takes in another template to instantiate. B 本身是一个模板,它接受另一个模板来实例化。 Here I created a dummy struct to demonstrate this.
在这里,我创建了一个虚拟结构来演示这一点。 But this should work with any other templates as well.
但这也适用于任何其他模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.