Topic: Calling a destructor for a template class
Author: "litaocheng@gmail.com" <litaocheng@gmail.com>
Date: Tue, 20 Nov 2007 22:03:22 CST Raw View
On Nov 16, 1:05 am, achim.burs...@siemens.com (Bursian Achim) wrote:
> Alberto Ganesh Barbati wrote:
> > As you have already been answered properly, I take the opportunity to
> > show one more way to destroy an object, which is a bit more descriptive
> > and way less verbose when templates are involved:
>
> > template <class T>
> > inline void destroy(T& x) { x.~T(); }
>
> > void C::i()
> > {
> > destroy(listSol);
> > }
>
> Thanks, Ganesh,
> that looks much much better! And this single templated destroy function can be used for
> even the most complicated template classes - cool!
>
> I'll propose that to our development team.
>
> -Achim
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-...@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
just like the stl implemention:
template <class _Tp>
inline void _Destroy(_Tp* __pointer) {
__pointer->~_Tp();
}
in this case, pointer replace with reference.
template <class _Tp>
inline void _Destroy(_Tp& __ref) {
__ref.~_Tp();
}
:)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: achim.bursian@siemens.com (Bursian Achim)
Date: Wed, 14 Nov 2007 22:34:45 GMT Raw View
Today we had some discussions about the right way to explicitely call a destructor (yes,
we have a special memory management system, so this is appropriate)
template<class T> class CSyncList
{
public:
CSyncList();
~CSyncList();
void add( T);
T* listPtr;
};
class C
{
public:
CSyncList<int*> listSol;
void f();
void g();
void h();
};
void C::f() {
listSol.~CSyncList();
}
void C::g() {
listSol.CSyncList<int*>::~CSyncList();
}
void C::h() {
listSol.~CSyncList<int*>();
}
All three variants are accepted by g++ and produce identical code. But are all of them
correct according to the standard?
I guess the one in C::h() is not correct, but I'm not sure.
Thanks for any advice in this matter,
Achim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Wed, 14 Nov 2007 23:00:03 GMT Raw View
Bursian Achim wrote:
> template<class T> class CSyncList
> CSyncList<int*> listSol;
> listSol.~CSyncList();
> listSol.CSyncList<int*>::~CSyncList();
> listSol.~CSyncList<int*>();
>
> All three variants are accepted by g++ and produce identical code.
> But are all of them correct according to the standard?
Yes. 14.6.1 has an extensive discussion of how the name
of a template may be used and what it means. You missed
another possibility too:
listSol.CSyncList<int*>::~CSyncList<int*>();
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 15 Nov 2007 00:11:56 GMT Raw View
Bursian Achim ha scritto:
> Today we had some discussions about the right way to explicitely call a
> destructor (yes, we have a special memory management system, so this is
> appropriate)
>
> template<class T> class CSyncList
> {
> public:
> CSyncList();
> ~CSyncList();
> void add( T);
> T* listPtr;
> };
>
> class C
> {
> public:
> CSyncList<int*> listSol;
> void f();
> void g();
> void h();
> };
>
>
> void C::f() {
> listSol.~CSyncList();
> }
>
> void C::g() {
> listSol.CSyncList<int*>::~CSyncList();
> }
>
> void C::h() {
> listSol.~CSyncList<int*>();
> }
>
> All three variants are accepted by g++ and produce identical code. But
> are all of them correct according to the standard?
> I guess the one in C::h() is not correct, but I'm not sure.
>
As you have already been answered properly, I take the opportunity to
show one more way to destroy an object, which is a bit more descriptive
and way less verbose when templates are involved:
template <class T>
inline void destroy(T& x) { x.~T(); }
void C::i()
{
destroy(listSol);
}
HTH,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: achim.bursian@siemens.com (Bursian Achim)
Date: Thu, 15 Nov 2007 17:05:55 GMT Raw View
Alberto Ganesh Barbati wrote:
> As you have already been answered properly, I take the opportunity to
> show one more way to destroy an object, which is a bit more descriptive
> and way less verbose when templates are involved:
>
> template <class T>
> inline void destroy(T& x) { x.~T(); }
>
> void C::i()
> {
> destroy(listSol);
> }
Thanks, Ganesh,
that looks much much better! And this single templated destroy function can be used for
even the most complicated template classes - cool!
I'll propose that to our development team.
-Achim
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]