Re: Indexer on generic argument



Pavel Minaev wrote:
On Aug 7, 6:47 pm, "Ben Voigt [C++ MVP]" <r...@xxxxxxxxxxxxx> wrote:
generic <typename TList, typename ItemType> where TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;

This line shouldn't have worked -- list1 has stack semantics
(tracking reference), list2 is a tracking handle. You need an
address-of operator to make a handle from a reference.

It works because it's a generic declaration, not a template
declaration. Generic type parameters can only be instantiated with
handles for ref types - so, for a specific instantiation, TList would
be something like List<T>^, and not just plain List<T>.

Hmmm. Then the constraint line makes little sense. A tracking handle
doesn't implement an interface, the type of the referenced object does. I
realize this is connected to the .NET behavior of following different rules
when instantiating generics using ref types vs value types, and there
probably is no perfectly consistent syntax which expresses the behavior.


.



Relevant Pages

  • Re: Indexer on generic argument
    ... ref class MyClass ... This line shouldn't have worked -- list1 has stack semantics (tracking ... reference), ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Indexer on generic argument
    ... ref class MyClass ... This line shouldn't have worked -- list1 has stack semantics (tracking ... reference), ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Passing arguements by reference
    ... but doSomething doesnt change p unless i use "ref"? ... reference, hence both p and q pointing to the same object, not to different ... don't use the ref keyword. ... void Execute() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Documentation suggestions
    ... > Ian> I think it would be very useful if there was reference (not just ... Lang ref as only for "language lawyers". ...
    (comp.lang.python)
  • Re: pass by reference
    ... focus has moved from insisting to use "by ref" in java-context ... independent of any other use of the word "reference" in a language. ... Just because Java-refs can be re-targetted by assignment? ... If Java had an operator to tell an Object ...
    (comp.lang.java.programmer)