Re: Pointer to a generic type parameter




"interX" <TK777@xxxxxx> wrote in message
news:1169606820.120930.142350@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.

}



.



Relevant Pages

  • Re: Overiding generic functions
    ... however If i have a serializer for a generic array ... it always calls the generic function even if its an array of ints, ... the compiler needs to decide when your generic SerializeArray ... which negates one of the main advantages of generics in C#. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overiding generic functions
    ... however If i have a serializer for a generic array ... it always calls the generic function even if its an array of ints, ... main advantages of generics in C#. ... method to call it so that the compiler knows at the time it generates the ...
    (microsoft.public.dotnet.languages.csharp)
  • Wasted Efforts (Parametric polymorphism?)
    ... And even if below solves problems that are solved in generics or templates, does that necessarily mean that we can't come up with a better solution.. ... astrarray = array of string; ... procedure add(const item: AnyType var arr: AnyTypeArray); ...
    (borland.public.delphi.non-technical)
  • Re: Overiding generic functions
    ... when I call the function with an int32 it calls the ... it always calls the generic function even if its an array of ints, ... But then you'd have to have a different compiled generic method for each use, which negates one of the main advantages of generics in C#. ... If you're going to have situations where you want to use some specific non-generic method, I think you'll need some specific non-generic array method to call it so that the compiler knows at the time it generates the call what the type is. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Announcing CodeGear RAD Studio 2007
    ... genuine quest to determine where generics can be useful outside of the ... oft quoted example of type safe containers. ... if generics may be useful. ... How about an array copier? ...
    (borland.public.delphi.non-technical)