J
jrwats
I would like to have a class template A that takes a class B as it's
template parameter and has a static instance ofr it. However that
same B class needs to know class A via templatization. See below
template<class ClassWithStaticFunc>
class CallsStaticFunc {
public:
void CallStaticFunc() {
ClassWithStaticFunc::StaticFunc();
}
};
template<class Caller >
class HasStaticFunc {
public:
static void StaticFunc() {
cout << "This function is static!" << endl;
}
static Caller s_Caller;
};
int main() {
// we can't make an instance that works because trying to
//would need infinitely recursive templates
HasStaticFunc<CallsStaticFunc<HasStaticFunc<Calls....> > >
}
Trying to use the "Curiously Recurring template pattern" also does not
work for this problem. I really need HasStaticFunc to have a static
instance of CallsStaticFunc and for CallsStaticFunc to be able to call
any arbitrary HasStaticFunc<CallsStaticFunc>
Note: The below gets done what I need to accomplish, but I REALY want
the StaticClass a template for H. With the below solution, if I add
anymore templates to StaticClass, H would then have to take those in
as parameters as well...
template<class ClassWithStaticFunc>
class CallsStaticFunc {
public:
void CallStaticFunc() {
ClassWithStaticFunc::StaticFunc();
}
};
template parameter and has a static instance ofr it. However that
same B class needs to know class A via templatization. See below
template<class ClassWithStaticFunc>
class CallsStaticFunc {
public:
void CallStaticFunc() {
ClassWithStaticFunc::StaticFunc();
}
};
template<class Caller >
class HasStaticFunc {
public:
static void StaticFunc() {
cout << "This function is static!" << endl;
}
static Caller s_Caller;
};
int main() {
// we can't make an instance that works because trying to
//would need infinitely recursive templates
HasStaticFunc<CallsStaticFunc<HasStaticFunc<Calls....> > >
}
Trying to use the "Curiously Recurring template pattern" also does not
work for this problem. I really need HasStaticFunc to have a static
instance of CallsStaticFunc and for CallsStaticFunc to be able to call
any arbitrary HasStaticFunc<CallsStaticFunc>
Note: The below gets done what I need to accomplish, but I REALY want
the StaticClass a template for H. With the below solution, if I add
anymore templates to StaticClass, H would then have to take those in
as parameters as well...
template<class ClassWithStaticFunc>
class CallsStaticFunc {
public:
void CallStaticFunc() {
ClassWithStaticFunc::StaticFunc();
}
};