Previous | Contents | Index |
DEC C++ currently provides a partial implementation of the C++ Standard Library. The ANSI X3/J16 C++ committee is in the process of standardizing the C++ Standard Library, and when this process is complete, DEC C++ will provide a complete library implementation. In the meantime, the subset provided was selected from among the most stable parts of the library as defined in the ANSI C++ draft.
This release of DEC C++ contains the following components of the C++ Standard Library:
Some of the components in the C++ Standard Library are designed to replace nonstandard components that are currently distributed in the DEC C++ Class Library. DIGITAL will continue to provide the DEC C++ Class Library in its nonstandard form. However, you now have the option of using the new standard components.
The following sections provide more information on the DEC C++
implementation of the Standard Library, including upward compatibility,
compiling and linking, thread safety, and details on each component of
the library.
6.1 Important Compatibility Information
Because the standardization process for the C++ Standard Library is not yet completed, DIGITAL cannot guarantee that this version of the library is compatible with any past or future releases. We ship the run-time portion of the library in object form, not in shareable form, to emphasize this situation.
Therefore, DIGITAL recommends that you do not use this library for any
production code that requires upward compatibility. This release of the
library matches as closely as is feasible the standard described in the
post-Stockholm ANSI C++ draft dated 24 September 1996.
When you use the cxx command to compile
and link programs that use the C++ Standard Library, no special
switches are required. The DEC C++ driver automatically includes the
Standard Library run-time support (-lcxxstd) on the link command, and automatic
template instantiation (-pt) is the
default mode.
For example, to build a program called prog.cxx that uses the Standard Library, you
can simply use the following command:
6.2 How to Build Programs Using the C++ Standard Library
cxx prog.cxx |
Thread Safety
The C++ Standard Library is thread safe if you specify the -D_REENTRANT flag on your cxx command. You can also use the -threads flag to establish thread safety. This
ensures that all internal library data is protected against
simultaneous access from multiple threads.
DEC C++ currently does not support all the necessary language features
to compile a Standard Library that meets the specifications of the ANSI
C++ draft. Therefore, where possible, the DIGITAL implementation of the
Standard Library contains workarounds for these missing language
features.
The following list shows the unsupported ANSI C++ language features and
their workarounds in the DEC C++ Standard Library:
The following data structures and algorithms supplied with the current
STL differ from those specified in the September 1996 ANSI C++ draft:
Additionally, the following are some minor incompatibilities in the
current String Library that correct what DIGITAL believes are mistakes
in the draft:
6.3 Incompatibilities Between the DEC C++ Standard Library and the September 1996 ANSI C++ Draft
Athough the current version of the DEC C++ compiler
offers this support, the Standard Library classes and functions are not
placed in namespace std.
Some member function templates
within the Standard Library have been substituted with member functions
for a given type.
The current STL simulates bool as a typedef of int.
Thus, the DEC C++ STL cannot supply the specialization vector<bool> because it would conflict
with vector<int>. In the interim,
DIGITAL supplies a bit_vector class in
place of vector<bool>.
The DEC C++ compiler does not support wchar_t as a built-in type, therefore the
current String Library does not support wide character types.
The DEC C++ Numeric Limits class does not
provide a specialization for bool or for wchar_t because DEC C++ does not treat them as
built-in types.
DEC C++ does not support the mutable keyword, thus the auto_ptr release() member is not declared const.
6.4 The Standard Template Library
DEC C++ provides an implementation of the Standard Template Library (STL).
The following sections provide information specific to the DEC C++
implementation of the STL. For information on how to program with the
STL, refer to the STL Tutorial and Reference Guide that is
part of the DEC C++ printed documentation set. See Section 6.4.3 for
differences between the STL Tutorial and Reference Guide and
the DEC C++ implementation of the STL.
6.4.1 Examples of Use
The following are some simple examples of using the containers and algorithms in the STL. You should compile these examples with the command:
cxx prog.cxx |
For details on how to build programs that use the STL, see Section 6.2.
Using the vector class
// This example shows how to create a vector, // initialize it with values, sort and print it #include <vector> #include <algorithm> #include <iostream.hxx> int main() { // create a vector of 4 ints vector<int> v(4); // initialize the vector with values v[0]=2; v[1]=0; v[2]=3; v[3]=1; // sort the vector sort(v.begin(),v.end()); // print the sorted vector for (vector<int>::iterator viter=v.begin();viter!=v.end();viter++) cout << *viter << " "; cout << endl; return 0; } |
Using the list class
// This example shows how to create a list, initialize // it with values, find a particular value and print // the element before and after that value #include <list> #include <iostream.hxx> #include <string> int main() { // create a list list<string> l; // add some values to the list l.insert(l.end(),"Stepanov"); l.insert(l.end(),"Koenig"); l.insert(l.end(),"Stroustrup"); l.insert(l.end(),"Lippman"); // find the value "Stroustrup" string value("Stroustrup"); list<string>::iterator liter=find(l.begin(),l.end(), value); // print out the value before and after "Stroustrup" if (liter!=l.end()) cout << "Stroustrup was found after " << *(--liter) << " and before " << *(++(++liter)) << endl; return 0; } |
Using the map class
// This example shows how to create a map, // add some values, and find the number of elements // which match a specified criterion #include <map> #include <iostream.hxx> bool smaller (pair<const char* const, float> p) { // returns true if element costs less than $3.00 return p.second < 3.00; } int main() { // create a map map<const char*,float, less<const char*> > shopmap; // add some elements to the map shopmap["milk"]=1.29; shopmap["steak"]=5.99; shopmap["cornflakes"]=2.69; shopmap["cheese"]=3.42; shopmap["ricekrispies"]=2.25; // count the number of items less than $3.00 int num_items = 0; count_if(shopmap.begin(),shopmap.end(),smaller,num_items); // print the results cout << "number of items less than 3 dollars is: " << num_items << endl; return 0; } |
The following discussion guides you through upgrading DEC C++ Class
Library code to use the STL, specifically replacing the vector and
stack classes that are currently in the vector.hxx header file.
6.4.2.1 Upgrading from the DEC C++ Class Library Vector to the STL Vector
To change your code from using the DEC C++ Class Library vector to the STL vector, consider the following actions:
Nonstandard Vector Function | Replaced with STL Vector Function |
---|---|
elem(int index) |
operator[](size_t index)
(no bounds checking) |
operator[](int index) |
at(size_t index)
(bounds checking) |
setsize(int newsize) | resize(size_t newsize) |
To change your code from using the existing stack to the STL stack, consider the following actions:
Nonstandard Stack | STL Stack |
---|---|
size_used() | Does not exist because the size() function always is equal to the size_used() function. |
full() | Does not exist because the stack always is full. |
pop() |
Does not return the popped element. To simulate DEC C++ Class Library
behavior, first get the element as the return type from the
top()
function and then call the
pop()
function. For example, change
int i=s.pop();
to the following:
int i=s.top(); |
The STL Tutorial and Reference Guide (the Guide) is based on
the STL implementation provided by the Hewlett-Packard Company. Because
the DEC C++ STL incorporates changes from the ANSI C++ draft, the two
implementations differ slightly. Differences also exist because the DEC
C++ compiler does not currently support all of the new language
features specified in the ANSI C++ draft; refer to Section 6.3 for
the list of missing language features. This section discusses the
remaining differences.
6.4.3.1 Header File Names
The header file names shown in the STL Tutorial and Reference Guide differ from the DEC C++ header file names, as shown in the following table:
Names Used in the Guide | Names Supplied by DEC C++ |
---|---|
algo.h | Divided into algorithm and numeric |
deque.h | deque |
function.h | functional |
iterator.h | iterator |
list.h | list |
multiset.h | set |
map.h | map |
multimap.h | map |
set.h | set |
stack.h | Divided into stack and queue |
vector.h | vector |
The STL Tutorial and Reference Guide states that you need to compile the run-time .cpp files that are provided with the Hewlett-Packard implementation, such as random.cpp and tempbuf.cpp.
DEC C++ already provides this run-time support in an object library and
automatically links this library into your application; therefore, you
can ignore the Guide's comments about compiling this support.
6.4.3.3 Guide Examples Need to Be Modified
The STL Tutorial and Reference Guide contains many example programs. Appendix B of the Guide shows a World Wide Web address (active as of this printing) from which you can copy these example programs. Because of the differences outlined in the previous sections, these example programs must be modified to work with the DEC C++ STL. To perform the necessary modifications, DEC C++ provides the following script:
/usr/lib/cmplrs/cxx/update_stl_book_examples.sh |
Before running this script, copy and prepare the example programs. Use the following steps as guidelines:
ftp://ftp.digital.com/pub/Alpha/apps |
http://www.aw.com/cp/musser-saini.html |
diction.zip examples.zip |
diction.gz examples.taz |
unzip diction.zip unzip examples.zip |
gunzip diction.gz gunzip examples.taz tar -xvf examples.tar |
sh /usr/lib/cmplrs/cxx/update_stl_book_examples.sh |
Now that your example programs are ready, compile them with the following command:
cxx -nopt -define_templates -writable_strings -I. ex.cpp |
To link ex17-01, you must compile screen.cpp and shape.cpp and link with the resulting object files.
The header file, bstring.h, which is included with the example programs, is based on the string library as defined by an old version of the ANSI C++ draft. DIGITAL recommends that you do not use this header file and instead use the <string> header provided by DEC C++. |
This section describes functional differences between the DEC C++ STL and the STL Tutorial and Reference Guide by chapter.
Chapter 6
assign(size_type position, const T& value) |
Chapter 18
The DEC C++ STL description of the reverse_iterator type definition differs from the description in §18.9 of the Guide as follows:
typedef reverse_iterator<RandomAccessIterator, T, Reference, Pointer, Distance> self; |
Chapter 19
a.clear() |
a.assign(q1, q2) |
a.assign(n, t) |
Chapter 20
Chapter 22
See also Section 6.5.5 for differences between the Guide string class and the DEC C++ Standard String Library.
Previous | Next | Contents | Index |