Sunday, 15 September 2013

C++/CLI Generic Static Class

C++/CLI Generic Static Class

I've been trying to convert some C# code to C++/CLI code (for some
performance reasons, as well as avoiding a bunch of interop issues),
however I've hit a wall. Admittedly, my C++/CLI knowledge is lacking (at
the absolute best).
Trying to convert a generic static class, that we used for size caching,
is proving to be a major headache.
The C# class is as follows;
public static class SizeCache<T>
{
public static int Size = Marshal.SizeOf(typeof(T));
}
And the C++/CLI wrapper (as far as I've gotten) is the following;
generic <typename T>
public ref class SizeCache abstract sealed
{
private:
static SizeCache()
{
Size = Marshal::SizeOf(T::typeid);
}
public:
static int Size;
};
Everything seems to compile fine, however, when I access it via;
int size = SizeCache<T>::Size;
I get compiler errors.
error C2039: 'Size' : is not a member of '`global namespace''
error C2065: 'Size' : undeclared identifier
Unfortunately, I can't see what I'm doing wrong in this case.
The reason for the class in the first place, is to avoid some issues with
the Marshaler (specifically when dealing with generic structure types),
and avoid the performance hit incurred by constantly calling SizeOf on the
same basic types.
What am I doing wrong here?

No comments:

Post a Comment