generics method?

  • Thread starter Thread starter vcquestions
  • Start date Start date
V

vcquestions

What's the best approch to having a common method that takes a
collection ( List<> ), goes through its elements and deletes them.

public ref struct Struct1
{
public:
Struct1( );
~Struct1( );
};

public ref struct Struct2
{
public:
Struct2( );
~Struct2( );
};

typedef List<Struct1^>^ StructuresOne;
typedef List<Struct12>^ StructuresTwo;

??method that would accept StructuresTwo or StructuresOne and call
destructor on each object??

Thanks in advance!
vcq
 
vcquestions said:
What's the best approch to having a common method that takes a
collection ( List<> ), goes through its elements and deletes them.

public ref struct Struct1
{
public:
Struct1( );
~Struct1( );
};

public ref struct Struct2
{
public:
Struct2( );
~Struct2( );
};

typedef List<Struct1^>^ StructuresOne;
typedef List<Struct12>^ StructuresTwo;

??method that would accept StructuresTwo or StructuresOne and call
destructor on each object??

No generics needed:

void FreeAll(IEnumerable theList)
{
for each (System::Object^ member in theList)
delete dynamic_cast<IDisposable^>(member);
}

However, using generic would yield a speed improvement for "value
class"/"value struct" members:

generic <typename T> where T : IDisposable
void FreeAll(IEnumerable<T>^ theList)
{
for each (T^ member in theList)
delete member;
}
 
No generics needed:

void FreeAll(IEnumerable theList)
{
for each (System::Object^ member in theList)
delete dynamic_cast<IDisposable^>(member);

}

However, using generic would yield a speed improvement for "value
class"/"value struct" members:

generic <typename T> where T : IDisposable
void FreeAll(IEnumerable<T>^ theList)
{
for each (T^ member in theList)
delete member;



}


- Show quoted text -- Hide quoted text -

- Show quoted text -

Ben, thank you for a thorough answer. Why do we want to cast to the
IDisposable ( in the first approach ) instead of applying delete to
the Object^. I thought delete on the object was a correct thing to do
and if our item is based on the object than the virtual destructor
will be called. Are we not hitting extra overhead with dynamic_cast
here?

Thanks again.
 
vcquestions said:
Ben, thank you for a thorough answer. Why do we want to cast to the
IDisposable ( in the first approach ) instead of applying delete to
the Object^. I thought delete on the object was a correct thing to do
and if our item is based on the object than the virtual destructor
will be called. Are we not hitting extra overhead with dynamic_cast
here?

I'm not sure if delete on an Object will add a runtime check whether it is
IDisposable. In order to call a virtual member function, you normally have
to downcast to at least the type where that member function is introduced,
and "Dispose" is introduced by IDisposable.

The runtime check is necessary when using Object, because not all objects
implement Disposable, so there's no overhead.

That's one of the reasons why the generic method could be faster, because
now there is a constraint and the compiler knows in advance that T
implements Dispose. However a cast to interface probably isn't any faster
than dynamic_cast anyway (cast to base class is very fast). The JIT only
instantiates a generic once for reference types, the same implementation is
shared for all different T unless T is a value struct. So it's not possible
for the compiler to determine the correct offset into the v-table at JIT
time, a runtime cast will be used.
 
I'm not sure if delete on an Object will add a runtime check whether it is
IDisposable. In order to call a virtual member function, you normally have
to downcast to at least the type where that member function is introduced,
and "Dispose" is introduced by IDisposable.

The runtime check is necessary when using Object, because not all objects
implement Disposable, so there's no overhead.

That's one of the reasons why the generic method could be faster, because
now there is a constraint and the compiler knows in advance that T
implements Dispose. However a cast to interface probably isn't any faster
than dynamic_cast anyway (cast to base class is very fast). The JIT only
instantiates a generic once for reference types, the same implementation is
shared for all different T unless T is a value struct. So it's not possible
for the compiler to determine the correct offset into the v-table at JIT
time, a runtime cast will be used.- Hide quoted text -

- Show quoted text -

crystal clear! ;) thank you!
 
Back
Top