Pointer to a generic type parameter

I

interX

Hi

I'm new in VC++ and have a question to generics. I have a generic
class, which contains an array of the generic type. This array I can
pin and then I would like to get an unmanaged pointer to it. Therefore
I wanted to creat a class member which represents the pointer to the
array. Unfortunately I get the folowring compile error for the code
beneath:

error C3229: 'DataType *' : indirections on a generic type parameter
are not allowed

Is there a posible way to get a this unmanaged pointer? My problem is
that it isn't allowed to creat a pin_ptr<DataType> as a class member
and it takes quite a while to do it every function call. Or, do I I
realy have to do a hack and creat a void* from the array by using
GCHandle::Alloc(...).AddrOfPinnedObject().ToPointer() and then cast it
to a byte* and shift it each time by sizeof(DataType)?

By the way, my generic typs are never reference objects, only value
structs and data types like int, byte, ... .

Thanks for any help!

Thorsten



generic<typename DataType> public ref class RingBuffer
{
array<DataType> ^m_Buffer;
DataType *m_Buffer_Ptr; // Error
}
 
B

Ben Voigt

interX said:
Hi

I'm new in VC++ and have a question to generics. I have a generic
class, which contains an array of the generic type. This array I can
pin and then I would like to get an unmanaged pointer to it. Therefore
I wanted to creat a class member which represents the pointer to the
array. Unfortunately I get the folowring compile error for the code
beneath:

error C3229: 'DataType *' : indirections on a generic type parameter
are not allowed

Generics are required to support all possible data types with the same
MSIL.... this isn't possible for what you are trying to do. Probably you
want a template instead.
Is there a posible way to get a this unmanaged pointer? My problem is
that it isn't allowed to creat a pin_ptr<DataType> as a class member
and it takes quite a while to do it every function call. Or, do I I

Probably you need to find a way to do what you want without pinning... so
much pinning is bad for performance, and trying to pin long-term is even
worse.
realy have to do a hack and creat a void* from the array by using
GCHandle::Alloc(...).AddrOfPinnedObject().ToPointer() and then cast it
to a byte* and shift it each time by sizeof(DataType)?
sizeof (DataType) isn't known for a generic parameter, so neither you nor
the C++ compiler can do pointer arithmetic. Only managed arrays contain the
extra information necessary to reference individual elements.
By the way, my generic typs are never reference objects, only value
structs and data types like int, byte, ... .

But an array is a managed type in .NET, and the garbage collector will move
it around.

Furthermore, even without using pointers, you'll find that generics are
totally useless with value types for doing anything more than containment,
because they don't implement much in the way of interfaces, any sort of math
is totally out of the question.
Thanks for any help!

Thorsten



generic<typename DataType> public ref class RingBuffer
{
array<DataType> ^m_Buffer;
DataType *m_Buffer_Ptr; // Error

interior_ptr<DataType*> might work... but still won't be pinned, and still
couldn't allow pointer arithmetic, because sizeof () isn't known, so I think
the compiler will still forbid it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top