I hit a snag today... I wanted to define a small templated helper class:
template<class T>
CMyClass
{
public :
CMyClass() { size_t iSize = sizeof(T); } // Allowed.
size_t GetElementSize() const { return sizeof(T); } // C2027.
};
and of course, it wouldn't compile (C2027). My question was, is it possible to get the size of the type? The reason I need this is that the type the object is constructed with could be a number of differently-defined structures, and so I need to get the size of the structure used, at run time.
Through a quick bit of experimentation, because I'm stubborn, it seems that I can use sizeof(T) in the ctor, but not in the non-ctor function - so my question now is... why?!