Book contents
- Frontmatter
- Contents
- Source Code Listings
- Preface
- 1 Introduction
- 2 General Principles
- 3 Formatting Conventions
- 4 Naming Conventions
- 5 Documentation Conventions
- 6 Programming Principles
- 7 Programming Conventions
- 8 Input and Output
- 9 Packaging Conventions
- 10 Generic Programming
- 11 Object Orientation
- 12 Parallel Processing
- 13 Numerical Types, Kinds, and Operations
- 14 C Interoperability
- 15 Updating Old Programs
- A Source Code
- B Rule List
- Bibliography
- Index
10 - Generic Programming
Published online by Cambridge University Press: 05 December 2011
- Frontmatter
- Contents
- Source Code Listings
- Preface
- 1 Introduction
- 2 General Principles
- 3 Formatting Conventions
- 4 Naming Conventions
- 5 Documentation Conventions
- 6 Programming Principles
- 7 Programming Conventions
- 8 Input and Output
- 9 Packaging Conventions
- 10 Generic Programming
- 11 Object Orientation
- 12 Parallel Processing
- 13 Numerical Types, Kinds, and Operations
- 14 C Interoperability
- 15 Updating Old Programs
- A Source Code
- B Rule List
- Bibliography
- Index
Summary
A common requirement when writing code is the ability to use the identical, or nearly identical, code for different data types. In the case of the public domain LAPACK linear algebra library (see Reference [5]), for instance, procedures support much the same set of operations for each of real, double precision, and complex data types. For example, the real version of a matrix operation routine might be identical to the complex version, except for a few details, primarily:
The data type of the dummy arguments.
The data type of function return values.
The data type of localized temporary variables.
Numeric constants (e.g., 1.0 vs (1.0,0.0)).
In addition to the data type, the kind and rank of arguments may also differ. A routine that processes real data may be identical to its double precision counterpart, thereby making the kinds different. And often routines accepting scalar arguments may need to be expanded to accept array arguments.
A number of techniques are used to make the job of generic programming easier and less error prone than simply replicating and hand-editing code. These techniques can be divided into three general methodologies: reducing the need to replicate code, reducing the number of code changes required to replicate code, and finally, automating the replication of code. The specific techniques we discuss are:
Use parameterized derived types to avoid replication based on kind and len.
Create and use generic names for procedures with interface blocks.
[…]
- Type
- Chapter
- Information
- Modern FortranStyle and Usage, pp. 171 - 182Publisher: Cambridge University PressPrint publication year: 2011