For example, when your archive library is linked with an application, library objects in the link may be different than those used when linking the library in a prior release.

Following are two examples of building an archive library; one built with

+inst_auto/+inst_close (the prior default), and the other built with the current (compile-time) default:

Building an Archive Library with +inst_auto/+inst_close

Suppose for lib.inst_auto.a, the linker chooses foo2.o to resolve symbol x, and foo3.o to resolve symbol stack <int>, symbols x, y, and stack <int> are each resolved with no duplicates.

lib.inst_auto.a

-------------------------------------------------

foo.o

foo2.o

foo3.o

 

 

stack<int>

x

x

y

y

 

 

-------------------------------------------------

Building an Archive Library with Compile-time Instantiation

Suppose for lib.default.a, the linker chooses foo2.o to resolve symbol x, and foo.o to resolve symbol stack <int>, symbols x, y, and stack <int> are each resolved, but now there’s a duplicate definition of symbol x. This will cause a linker duplicate symbol error. This is really a user error, but was not visible before.

lib.default.a

-------------------------------------------------

foo.o

foo2.o

foo3.o

stack<int>

stack<int>

stack<int>

x

x

y

y

 

 

-------------------------------------------------

NOTE: This example is not meant to account for all cases of changed behavior.

C++ Template Tutorial

You can create class templates and function templates. A template defines a group of classes or functions. A template can have one or more types as parameters. When you use a template, you provide the particular types or constant expressions as actual parameters thereby creating a particular object or function.

Class Templates

A class template defines a family of classes. To declare a class template, you use the keyword template followed by the template’s formal parameters. Class templates can take parameters that are either types or expressions. You define a template class in terms of those parameters. For example, the following is a class template for a simple stack class. The template has two parameters, the type specifier T and the int parameter size. The keyword class in the < > brackets is required to declare any template type parameters. The first parameter T is used for the stack element type. The second parameter is used for the maximum size of the stack.

template<class T, int size> class Stack

{

public: Stack(){top=-1;}

void push(const T& item){thestack[++top]=item;} T& pop(){return thestack[top--];}

private:

T thestack[size];

136 Using HP aC++ Templates

Page 136
Image 136
HP C/aC++ for PA-RISC Software manual ++ Template Tutorial, Class Templates