Borland Online And The Cobb Group Present:


April, 1995 - Vol. 2 No. 4

Modifying the STL to work with Borland C++ 4.x

In the accompanying article Using Standard Template Library (STL) containers and iterators, we show how you can use the container and iterator classes from the STL. You can use this library instead of using the Borland International Data Structures (BIDS) container and iterator class library that Borland ships with Borland C++ 4.0.

Because Borland didn't implement some of the newer template-related language features of C++ until version 4.0, you can't use the STL with any version of Borland C++ prior to 4.0. However, even if you're using version 4.0 or later, you still need to make some minor changes to the STL header files to make them compatible with Borland C++.

A temporary problem

If you're using Borland C++ 4.0, you need to eliminate the pre-decrement operators that are in the destroy( ) function calls that appear in the vector::erase( ) and vector::pop_back( ) member functions in the VECTOR.H file. You'll need to do this since the Borland C++ 4.0 compiler incorrectly creates a temporary copy of the finish data member before it decrements it.

When the compiler creates a copy of the finish data member before decrementing its value, it leaves the original finish data member alone. Unfortunately, this prevents the erase( ) and pop_back( ) member functions from reducing the reported size of the vector, and it could potentially allow iterators to address deleted objects. If you instead change the code to perform the decrement prior to making the destroy( ) function call, the compiler won't create the temporary copy of the finish data member, and it will decrement the data member correctly.

To begin making this change, open the VECTOR.H file. In this file, locate lines 135 through 138, which contain the following code:

void pop_back( ) { destroy(--finish); }
void erase(iterator position) {
  copy(position + 1, end( ), position);
  destroy(--finish);
}

Change these lines to

void pop_back( ) { 
  --finish;
  destroy(finish);
}
void erase(iterator position) {
  copy(position + 1, end( ), position);
  --finish;
  destroy(finish);
}

and then save these changes.

Algorithm (&) blues

In C, one of the more common uses for macros was to implement min( ) and max( ) functions. In Borland C++, these functions operate as template functions instead. (The min( ) and max( ) functions appear in the file STDLIB.H.) The STL also defines these template functions. Therefore, unless you eliminate either the Borland or STL version, you'll see multiple definition errors for these functions.

Fortunately, the STDLIB.H file contains a #ifdef statement to compile these functions if you don't define the name __MINMAX_DEFINED. (Notice that there are two underscores at the beginning of the name.)

To prevent the compiler from processing the versions of these functions that appear in the STDLIB.H file, we'll make a change to the STL header file ALGOBASE.H that defines most of the base algorithms. To do so, open this file, locate line 60 (the line immediately following the const version of the max( ) template function), and enter

#define __MINMAX_DEFINED

When you save this change, you'll be able to use the STL version of these functions without conflict.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.