|
|
|
start date: Sat, 18 Aug 2007 04:25:23 -0000,
posted on: microsoft.public.dotnet.languages.vc
back
| Thread Index |
|
1
vcquestions
|
|
2
Ben Voigt [C++ MVP] am
|
|
3
Ben Voigt [C++ MVP] am
|
|
4
vcquestions
|
|
5
vcquestions
|
generics method?
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
Date:Sat, 18 Aug 2007 04:25:23 -0000
Author:
|
Re: generics method?
"vcquestions" wrote in message
news:1187411123.836537.184740@d55g2000hsg.googlegroups.com...
> 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;
}
>
> Thanks in advance!
> vcq
>
Date:Mon, 20 Aug 2007 08:49:18 -0500
Author:
|
Re: generics method?
On Aug 20, 6:49 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> "vcquestions" wrote in message
>
> news:1187411123.836537.184740@d55g2000hsg.googlegroups.com...
>
>
>
>
>
> > 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;
>
>
>
> }
>
> > Thanks in advance!
> > vcq- Hide quoted text -
>
> - 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.
Date:Mon, 20 Aug 2007 16:51:55 -0000
Author:
|
Re: generics method?
"vcquestions" wrote in message
news:1187628715.731650.238590@r29g2000hsg.googlegroups.com...
> On Aug 20, 6:49 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
>> "vcquestions" wrote in message
>>
>> news:1187411123.836537.184740@d55g2000hsg.googlegroups.com...
>>
>>
>>
>>
>>
>> > 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;
>>
>>
>>
>> }
>>
>> > Thanks in advance!
>> > vcq- Hide quoted text -
>>
>> - 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?
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.
Date:Mon, 20 Aug 2007 12:26:26 -0500
Author:
|
Re: generics method?
On Aug 20, 10:26 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> "vcquestions" wrote in message
>
> news:1187628715.731650.238590@r29g2000hsg.googlegroups.com...
>
>
>
>
>
> > On Aug 20, 6:49 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> >> "vcquestions" wrote in message
>
> >>news:1187411123.836537.184740@d55g2000hsg.googlegroups.com...
>
> >> > 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;
>
> >> }
>
> >> > Thanks in advance!
> >> > vcq- Hide quoted text -
>
> >> - 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?
>
> 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!
Date:Mon, 20 Aug 2007 18:12:38 -0000
Author:
|
|
|