ZeePedia

C++ Library Overview:Using C++ Library Headers, Iostreams Conventions

<< C++ Library
Characters:Escape Sequences, Multibyte Characters, Wide-Character Encoding >>
img
....................................................................
Chapter 2. C++ Library Overview
Using C++ Library Headers (page 5) · C++ Library Conventions (page 6) ·
Iostreams Conventions (page 7) · Program Startup and Termination (page 7)
All C++ library entities are declared or defined in one or more standard headers.
To make use of a library entity in a program, write an include directive (page 50)
that names the relevant standard header. The Standard C++ library consists of 53
required headers. These 53 C++ library headers (along with the additional
Standard C headers constitute a hosted implementation of the C++ library:
<algorithm> (page 249), <bitset> (page 54), <cassert> (page 59), <cctype> (page
59), <cerrno> (page 59), <cfloat> (page 59), <ciso646> (page 60), <climits> (page
60), <clocale> (page 60), <cmath> (page 60), <complex> (page 61), <csetjmp> (page
72), <csignal> (page 72), <cstdarg> (page 72), <cstddef> (page 72), <cstdio> (page
72), <cstdlib> (page 73), <cstring> (page 73), <ctime> (page 73), <cwchar> (page
74), <cwctype> (page 74), <deque> (page 274), <exception> (page 74), <fstream>
(page 76), <functional> (page 282), <iomanip> (page 85), <ios> (page 86), <iosfwd>
(page 102), <iostream> (page 103), <istream> (page 105), <iterator> (page 293),
<limits> (page 114), <list> (page 310), <locale> (page 119), <map> (page 320),
<memory> (page 336), <new> (page 164), <numeric> (page 345), <ostream> (page
168), <queue> (page 347), <set> (page 353), <sstream> (page 176), <stack> (page
368), <stdexcept> (page 184), <streambuf> (page 185), <string> (page 195),
<strstream> (page 217), <typeinfo> (page 224), <unordered_map> (page 371),
<unordered_set> (page 386), <utility> (page 400), <valarray> (page 225), and
<vector> (page 403).
A freestanding implementation of the C++ library provides only a subset of these
headers: <cstddef> (page 72), <cstdlib> (page 73) (declaring at least the functions
abort, atexit, and exit), <exception> (page 74), <limits> (page 114), <new> (page
164), <typeinfo> (page 224), and <cstdarg> (page 72).
The C++ library headers have two broader subdivisions, iostreams (page 7)
headers and STL (page 1) headers.
Using C++ Library Headers
You include the contents of a standard header by naming it in an include (page 50)
directive, as in:
#include <iostream>
/* include I/O facilities */
You can include the standard headers in any order, a standard header more than
once, or two or more standard headers that define the same macro or the same
type. Do not include a standard header within a declaration. Do not define macros
that have the same names as keywords before you include a standard header.
A C++ library header includes any other C++ library headers it needs to define
needed types. (Always include explicitly any C++ library headers needed in a
translation unit, however, lest you guess wrong about its actual dependencies.) A
Standard C header never includes another standard header. A standard header
declares or defines only the entities described for it in this document.
5
img
..
Every function in the library is declared in a standard header. Unlike in Standard
C, the standard header never provides a masking macro, with the same name as
the function, that masks the function declaration and achieves the same effect.
All names other than operator delete and operator new in the C++ library
headers are defined in the std namespace, or in a namespace nested within the std
namespace. Including a C++ library header does not introduce any library names
into the current namespace. You refer to the name cin (page 104), for example, as
std::cin. Alternatively, you can write the declaration:
using namespace std;
which promotes all library names into the current namespace. If you write this
declaration immediately after all include directives, you can otherwise ignore
namespace considerations in the remainder of the translation unit. Note that macro
names are not subject to the rules for nesting namespaces.
Note that the C Standard headers behave mostly as if they include no namespace
declarations. If you include, for example, <cstdlib> (page 73), you should call
std::abort() to cause abnormal termination, but if you include <stdlib.h>, you
should call abort(). (The C++ Standard is intentionally vague on this topic, so you
should stick with just the usages described here for maximum portability.)
Unless specifically indicated otherwise, you may not define names in the std
namespace, or in a namespace nested within the std namespace.
C++ Library Conventions
The C++ library obeys much the same conventions as the Standard C library, plus
a few more outlined here.
An implementation has certain latitude in how it declares types and functions in
the C++ library:
v Names of functions in the Standard C library may have either extern "C++" or
extern "C" linkage. Include the appropriate Standard C header rather than
declare a library entity inline.
v A member function name in a library class may have additional function
signatures over those listed in this document. You can be sure that a function
call described here behaves as expected, but you cannot reliably take the address
of a library member function. (The type may not be what you expect.)
v A library class may have undocumented (non-virtual) base classes. A class
documented as derived from another class may, in fact, be derived from that
class through other undocumented classes.
v A type defined as a synonym for some integer type may be the same as one of
several different integer types.
v A bitmask type can be implemented as either an integer type or an
enumeration. In either case, you can perform bitwise operations (such as AND
and OR) on values of the same bitmask type. The elements A and B of a bitmask
type are nonzero values such that A & B is zero.
v A library function that has no exception specification can throw an arbitrary
exception, unless its definition clearly restricts such a possibility.
On the other hand, there are some restrictions you can count on:
v The Standard C library uses no masking macros. Only specific function
signatures are reserved, not the names of the functions themselves.
6
Standard C++ Library
img
........................
v A library function name outside a class will not have additional, undocumented,
function signatures. You can reliably take its address.
v Base classes and member functions described as virtual are assuredly virtual,
while those described as non-virtual are assuredly non-virtual.
v Two types defined by the C++ library are always different unless this document
explicitly suggests otherwise.
v Functions supplied by the library, including the default versions of replaceable
functions (page 164), can throw at most those exceptions listed in any exception
specification. No destructors supplied by the library throw exceptions. Functions
in the Standard C library may propagate an exception, as when qsort calls a
comparison function that throws an exception, but they do not otherwise throw
exceptions.
Iostreams Conventions
The iostreams headers support conversions between text and encoded forms, and
input and output to external files (page 17): <fstream> (page 76), <iomanip> (page
85), <ios> (page 86), <iosfwd> (page 102), <iostream> (page 103), <istream> (page
105), <ostream> (page 168), <sstream> (page 176), <streambuf> (page 185), and
<strstream> (page 217).
The simplest use of iostreams requires only that you include the header
<iostream>. You can then extract values from cin (page 104), to read the standard
input. The rules for doing so are outlined in the description of the class
basic_istream (page 106). You can also insert values to cout (page 104), to write the
standard output. The rules for doing so are outlined in the description of the class
basic_ostream (page 169). Format control common to both extractors and insertors
is managed by the class basic_ios (page 88). Manipulating this format information
in the guise of extracting and inserting objects is the province of several
manipulators (page 85).
You can perform the same iostreams operations on files that you open by name,
using the classes declared in <fstream>. To convert between iostreams and objects
of class basic_string (page 197), use the classes declared in <sstream>. And to do
the same with C strings, use the classes declared in <strstream>.
The remaining headers provide support services, typically of direct interest to only
the most advanced users of the iostreams classes.
C++ Program Startup and Termination
A C++ program performs the same operations as does a C program at program
startup and at program termination, plus a few more outlined here.
Before the target environment calls the function main, and after it stores any
constant initial values you specify in all objects that have static duration, the
program executes any remaining constructors for such static objects. The order of
execution is not specified between translation units, but you can nevertheless
assume that some iostreams (page 7) objects are properly initialized for use by
these static constructors. These control text streams:
v cin (page 104) -- for standard input
v cout (page 104) -- for standard output
v cerr (page 104) -- for unbuffered standard error output
v clog (page 104) -- for buffered standard error output
7
Chapter 2. C++ Library Overview
You can also use these objects within the destructors called for static objects,
during program termination.
As with C, returning from main or calling exit calls all functions registered with
atexit in reverse order of registry. An exception thrown from such a registered
function calls terminate().
8
Standard C++ Library