DesignCppTemplates

From MediaWiki

Jump to: navigation, search

In this article we present how C++ Templates can be translated into Java code. In order to do that, we first need to understand what templates are and what they are not. After that we present how Java Generics can help us. Richard Gomes


Contents

Overview

Some developers may find several concepts here not well explained of may find the explanations incomplete or even wrong. This is because this article is intended to Java developers willing to quickly understand what C++ templates are and how to convert them to something more or less equivalent in Java.

In a nutshell, for the impatient and computer skilled, C++ templates are much like C macros or unix m4 macros. In fact, templates are much more powerful than old C macros but it's irrelevant for us at in this article.

Templates are used in several different ways, much like macros in general can be used in different ways. For our discussion in this article, we will focus on the use of templates for the specific purpose of obtaining flexible object orientation and code reusability in C++ programs and how an equivalent Java code could be written.

What templates are

Without going deep into details out of our interest:

  • A template is a certain piece of text which is intended to become another piece of text.
  • There's something like a C++ template preprocessor which works much like the good-and-old C macro preprocessor: it simply expands text and replaces arguments by actual values.
  • The C++ compiler does not generate code from the template directly, but it generates code from the expanded source code obtained from the template text.

What templates are *not*

  • Templates are not classes but may certainly contain a class template (sic!);
  • A class template is neither a class declaration (or definition) nor a class instantiation but may certainly become one.
  • A class template is not the actual class but certainly may become one.
  • It does not make sense to compile a class template alone, without being used by actual code.

All the above statements come from the very basic concept that templates are not classes or even executable code but only some text that can be used somewhere else and eventually slightly modified by the use of parameters.

In particular, when the preprocessor expands a template which contains a class declaration into actual text, what happens is that you obtain a piece of text which is the actual declaration of a certain class. If the preprocessor expands the same template again using other parameters, what happens is that you obtain another piece of text which contatins a declaration of a another class.

Templates and Generics

From the previous explanation, probably you realized that Java Generics is a good candidate for converting templates to something more or less equivalent written in Java.

The main similarities between templates and generics are:

  • The syntax and the general structure is similar;
  • You can define parameters and make use of them in a very similar way;

The main differences are:

  • A generic class compiles and gets some code generate for it only once. A template class does compile alone because it's not actual code. A template class becomes actual code when actual code includes the template.
  • A generic class is a block of executable code which is able to handle different kinds of Objects which share the same shape, same properties or same responsabilities, roughly. A Java programmer can easily define which Objects are supported by supplying parameters which define the class hierarchies supported. A template class is a block of text which is intended to become a snippet of actual code. The C++ compiler verifies the semantics of the template everytime and after the template is expanded and becomes actual code.
  • As a generic class is a single block of executable code which potentially can handle different Objects, the Java programmer must remember this fact and it may be necessary to take care of details of the actual Object being used, in order to avoid ClassCastExceptions, for instance. As a template expands to actual text, a C++ programmer must be aware if the actual expanded text makes sense semantically when applied to the actual data.


Example

(to be done)


Conclusion

Most of cases it's easy to convert C++ templates into generic Java classes. Sometimes you will will find complex cases where you will have to decide how to change the original C++ object model into something else in Java. It happens because every usage of a C++ template creates another incarnation (sic!) of the same logic but applied to different arguments whilst every usage of a certain Java Generic class always creates new instances of that (same) generic class.


Richard Gomes 00:23, 11 March 2008 (UTC)