no copyright!iostream-migration.3

IOStream Issues when Migrating to the C++ Standard

Abstract

During standardization the interface of the IOStream classes was changed significantly. Thus, there are some issues which will cause problems when porting code using the older interface to a standard C++ platform. This document describes at least some of these migration issues.

Introduction

In the process of standardization, the interface to the IOStream classes was significantly changed. the most notable changes are
  • The names of the header files changed.
  • The classes are now declared in the namespace std.
  • The IOStream classes are now templates.

Although the introduction of templates was actually the biggest change for IOStreams, it will affect most old code only slightly. The most significant problem introduces by this change are forward declaration of stream classes (see below). The other two changes are not specific to the IOStream library but affect the whole C++ standard library.

Forward Declarations of IOStream Classes

To avoid including lots of header, it was common practise to declare the classes istream and ostream where declarations of these classes was sufficient. Especially, in header files, forward declarations are often used when read and write operators are defined for a class. Unfortunately, turning the IOStream classes into templates made these declarations invalid: The types istream and ostream are no longer classes but only typedefs for the classes basic_istream<char> and basic_ostream<char>, respectively.

The problem with these declarations should normally show up during compilation because there will be a conflict between the declaration in the standard headers and the declaration in the user code. However, since the new declarations are now in the namespace std, it is possible that both declarations coexist most of time except at the point where the functions using the declarations are defined. However, some compiler don't report a problem if the correct declaration preceeds the forward declaration but in these cases the forward declaration are apparently harmless.

The correct approach to resolve this problem is to include the new header <iosfwd>. The sole purpose of this header is the declaration of all IOStream template classes (the class ios_base is not declared in this header; this is a new class which was not present in older implementations and thus does not cause any migration problems).

TODO

  • Access to the underlying FILE* or file descriptor.
  • Removed or renamed open modes.

See Also


Copyright © 1999 Dietmar Kuehl, Claas Solutions (dietmar.kuehl@claas-solutions.de)