A
Adam Nielsen
Hi all,
I'm a bit confused about the syntax used to access a nested template
class. Essentially I have a bunch of class types to represent different
types of records in a database, and I want to store some cache data for
each datatype. The simplified code below demonstrates my issue - I
think I understand why it doesn't work as is (the structure only
declares the storage, I need to instantiate it for each template type)
but I'm not sure how to actually go about doing that.
Any pointers would be appreciated!
Thanks,
Adam.
class RecordTypeA { };
class RecordTypeB { };
class Main {
private:
template <typename T>
struct CacheData {
int i;
};
public:
template <typename T>
void set(T t, int x)
{
// Store some type-specific data for record type T
this->CacheData<T>::i = x;
}
};
int main(void)
{
RecordTypeA A;
RecordTypeB B;
Main m;
m.set(A, 1);
m.set(B, 2);
// At this point, I want this to be the case:
// m.CacheData<A>::i == 1
// m.CacheData<B>::i == 2
return 0;
}
I'm a bit confused about the syntax used to access a nested template
class. Essentially I have a bunch of class types to represent different
types of records in a database, and I want to store some cache data for
each datatype. The simplified code below demonstrates my issue - I
think I understand why it doesn't work as is (the structure only
declares the storage, I need to instantiate it for each template type)
but I'm not sure how to actually go about doing that.
Any pointers would be appreciated!
Thanks,
Adam.
class RecordTypeA { };
class RecordTypeB { };
class Main {
private:
template <typename T>
struct CacheData {
int i;
};
public:
template <typename T>
void set(T t, int x)
{
// Store some type-specific data for record type T
this->CacheData<T>::i = x;
}
};
int main(void)
{
RecordTypeA A;
RecordTypeB B;
Main m;
m.set(A, 1);
m.set(B, 2);
// At this point, I want this to be the case:
// m.CacheData<A>::i == 1
// m.CacheData<B>::i == 2
return 0;
}