FUTURE DIRECTIONS OF C++
An ANSI committee has been formed to write an ANSI standard for C++. They first met in the Spring of 1990 and are expected to complete the standard in about three years. Until the new standard is released, the C++ language is expected to stay fairly stable. However, due to the nature of compiler writers and their desire to slightly improve their offerings over their competitors, you can bet that the language will not remain static during this three year period.
Many small changes have been added during the past year that barely affect the casual programmer, or even the heavy user of the language. You can be sure that the language will evolve slowly and surely into a very usable and reliable language. There are two areas, however, that should be discussed in a little detail because they will add so much to the language in future years. Those two topics are parameterized types and exception handling.
FUTURE DIRECTIONS – PARAMETERIZED TYPES
Many times, when developing a program, you wish to perform some operation on more than one data type. For example you may wish to sort a list of integers, another list of floating point numbers, and a list of alphabetic strings. It seems silly to have to write a separate sort function for each of the three types when all three are sorted in the same logical way. With parameterized types, you will be able to write a single sort routine that is capable of sorting all three of the lists.
This is already available in the Ada language as the generic package or procedure. Because it is available in Ada, there is a software components industry that provides programmers with prewritten and thoroughly debugged software routines that work with many different types. When this is generally available in C++, there will be a components industry for C++ and precoded, debugged and efficient source code will be available off the shelf to perform many of the standard operations. These operations will include such things as sorts, queues, stacks, lists, etc. Bjarne Stroustrup has announced that parameterized types, otherwise known as templates or generics, will be available in a future version of C++. He has presented a paper with details of one way to implement them, but this is only a suggestion, not a specification.
Borland International has included templates in version 3.0 of Borland C++, and hopefully their implementation will be very close to the final definition of templates. The next three example programs will illustrate the use of templates with Borland’s compiler, but may not work with other compilers.
THE FIRST TEMPLATE
The example program named TEMPLAT1.CPP is the first example of the use of a template. This program is so simple it seems silly to even bother with it but it will illustrate the use of the parameterized type.
ANY_TYPE maximum(ANY_TYPE a, ANY_TYPE b)
return (a > b) ? a : b;
int x = 12, y = -7;
float real = 3.1415;
char ch = 'A';
printf("%8d\n", maximum(x, y));
printf("%8d\n", maximum(-34, y));
printf("%8.3f\n", maximum(real, float(y)));
printf("%8.3f\n", maximum(real, float(x)));
printf("%c\n", maximum(ch, 'X'));
// Result of execution
The template is given in lines 4 through 8 with the first line indicating that it is a template with a single type to be replaced, the type ANY_TYPE. This type can be replaced by any type which can be used in the comparison operation in line 7. If you have defined a class, and you have overloaded the operator “>”, then this template can be used with objects of your class. Thus, you do not have to write a maximum function for each type or class in your program.
This function is included automatically for each type it is called with in the program, and the code itself should be very easy to understand.
The diligent student should realize that nearly the same effect can be achieved through use of a macro, except that when a macro is used, the strict type checking is not done. Because of this and because of the availability of the inline method capability in C++, the use of macros is essentially non-existent by experienced C++ programmers.