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?
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
B itself is a template that takes in another template to instantiate. Here I created a dummy struct to demonstrate this. But this should work with any other templates as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.