Article ID: 182046 - Last Review: December 31, 2005 - Revision: 5.0 How to work around bugs in template libraries by using Visual C++This article was previously published under Q182046 Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed
code model that is provided by the Microsoft .NET Framework and the unmanaged
native Microsoft Windows code model. On This PageSUMMARY Occasionally, bugs are found in a template library. Because
all the source code for the library is available, it is tempting to go into the
source code and modify the library. This is not advisable because there may be
unintended side effects from the modifications to the library. In addition,
technical support staff from the library vendor may decline to support a
modified library. This article describes the following methods to work around bugs in template libraries without modifying the library, by using features of the C++ language:
MORE INFORMATION Below are several descriptions of how to work around bugs
in a template library. One or more of these workarounds may be applicable in
any given case. The first three methods discuss specializing a template. To discover all of the specializations required, compile without optimizations (to prevent inlining any of these functions). Then either use DUMPBIN /SYMBOLS or use the /MAP linker option to generate a map file. In either case, you can find all of the specializations of the template class, function, or member used in the application. You need to provide a separate template specialization for each of these. Method 1. Specialize the template function that has the bug.To work around a bug in the template function CopyElements in AfxTempl.h that causes a series of elements to be incorrectly copied, you can specialize CopyElements for each type used in your program. The template declaration in AfxTempl.h is:Method 2. Specialize the template class member that has the bug.To work around a bug in the template class member function deque<T>::_Buyback(), you can specialize this member function for each type of deque in your program. For instance, if you used a deque<MyType> in your program, you can specialize _Buyback() as follows:Method 3. Specialize the template class that has the bug.It is usually simpler to specialize class members that contain bugs. However, there are some circumstances when the whole class must be specialized. For instance, Visual C++ 5.0 doesn't support partial template specialization. This means that class iterator_traits<T*> does not exist, which means you can't use iterator_traits with a pointer type. In this case, you can specialize iterator_traits for each of the pointer types in your program:Method 4. Derive from the template class to extend its functionality.Suppose that you need class CComPtr (defined in Atlbase.h) to have additional members const operator ->>() const and operator const T*() const. To accomplish this, create a template class and derive from it, and provide the additional member functions you need:Method 5. Use a #define/#undef pair around a #include to change a symbol name in a template header that may be causing a conflict.For instance, suppose you had a source file MyProg.cpp: error C2300: 'MyTest' : class does not have a destructor
called '~Ty' APPLIES TO
| Article Translations
|

Back to the top
