Topic: Destruction of static members of a template
Author: iltchenko@yahoo.com (Andrei Iltchenko)
Date: Wed, 27 Jun 2001 19:37:23 GMT Raw View
"T.M." <turinga2@aol.com> wrote in message news:<3B38C47E.99816B10@aol.com>...
> The code I'm currently working on includes a template with a static
> attribute. The .h file looks something like this:
>
> template <class T>
> class A {
> public:
> static B b;
> };
>
> template <class T>
> B A<T>::b;
>
>
> I have two .cpp files which instantiate this template on the same
> templatized parameter, e.g. both File1.cpp and File2.cpp create
> instances of A<char> and use A<char>::b.
>
> For the most part, all runs as expected: the code in File1.cpp and
> File2.cpp reference the same ins obtance of A<char>::b. However, the
> destructor for the A<char>::b object is run twice. Apparently, the code
> which runs the ~B() destructor for A<char>::b was compiled into the
> object files for File1.cpp and File2.cpp, and the linker didn't resolve
> the duplicate code.
> Is this standard compliant behavior, or I am being a muffinhead?
> B.
The implementation that you are working with is certainly not totally
standard compliant in regard to ODR and Object lifetime.
The specialization 'A<char>::b' of the statatic data member of the
class template 'A<T>' shall have external linkage and static storage
duration. As a result, in a well-formed program on a conforming
implementation the specialization 'A<char>::b' shall denote the same
object no matter how many implicit instantiations of that object there
are in your program and which translation units the instantiations
appear in. Naturally, a conforming implementation must start and end
the lifetime of the object 'A<char>::b' properly. And the
implementation's calling its destructor twice is classified as
undefined behaviour as per the C++ Standard, thus rendering this
particular detail of the implementation non-compliant.
Regards,
Andrei Iltchenko.
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: "T.M." <turinga2@aol.com>
Date: Tue, 26 Jun 2001 17:23:59 GMT Raw View
The code I'm currently working on includes a template with a static
attribute. The .h file looks something like this:
template <class T>
class A {
public:
static B b;
};
template <class T>
B A<T>::b;
I have two .cpp files which instantiate this template on the same
templatized parameter, e.g. both File1.cpp and File2.cpp create
instances of A<char> and use A<char>::b.
For the most part, all runs as expected: the code in File1.cpp and
File2.cpp reference the same instance of A<char>::b. However, the
destructor for the A<char>::b object is run twice. Apparently, the code
which runs the ~B() destructor for A<char>::b was compiled into the
object files for File1.cpp and File2.cpp, and the linker didn't resolve
the duplicate code.
Is this standard compliant behavior, or I am being a muffinhead?
B.
---
[ 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.research.att.com/~austern/csc/faq.html ]