📄 lib_cpp.html
字号:
<HTML><HEAD><TITLE>C++ Library Overview</TITLE></HEAD><BODY><H1><A NAME="C++ Library Overview">C++ Library Overview</A></H1><HR><P><B><A HREF="#Using C++ Library Headers">Using C++ Library Headers</A>· <A HREF="#C++ Library Conventions">C++ Library Conventions</A>· <A HREF="#iostreams">Iostreams Conventions</A>· <A HREF="#C++ Program Startup and Termination">ProgramStartup and Termination</A>· <A HREF="#Exceptions">Exceptions</A></B></P><P>All C++ library entities are declared or defined in one or more<A HREF="lib_over.html#standard headers">standard headers</A>.To make use of a library entity in a program, write an<A HREF="preproc.html#include directive"><I>include</I> directive</A>that names the relevant standard header.The full set of 28<B><A NAME="C++ library headers">C++ library headers</A></B>(along with the additional 15<A HREF="lib_over.html#standard headers">Standard C headers</A>)constitutes a<B><A NAME="hosted implementation">hosted implementation</A></B>of Embedded C++:<CODE><A HREF="cassert.html"><cassert></A></CODE>,<CODE><A HREF="cctype.html"><cctype></A></CODE>,<CODE><A HREF="cerrno.html"><cerrno></A></CODE>,<CODE><A HREF="cfloat.html"><cfloat></A></CODE>,<CODE><A HREF="climits.html"><climits></A></CODE>,<CODE><A HREF="clocale.html"><clocale></A></CODE>,<CODE><A HREF="cmath.html"><cmath></A></CODE>,<CODE><A HREF="complex.html"><complex></A></CODE>,<CODE><A HREF="csetjmp.html"><csetjmp></A></CODE>,<CODE><A HREF="csignal.html"><csignal></A></CODE>,<CODE><A HREF="cstdarg.html"><cstdarg></A></CODE>,<CODE><A HREF="cstddef.html"><cstddef></A></CODE>,<CODE><A HREF="cstdio.html"><cstdio></A></CODE>,<CODE><A HREF="cstdlib.html"><cstdlib></A></CODE>,<CODE><A HREF="cstring.html"><cstring></A></CODE>,<CODE><A HREF="ctime.html"><ctime></A></CODE>,<CODE><A HREF="fstream.html"><fstream></A></CODE>,<CODE><A HREF="iomanip.html"><iomanip></A></CODE>,<CODE><A HREF="ios.html"><ios></A></CODE>,<CODE><A HREF="iosfwd.html"><iosfwd></A></CODE>,<CODE><A HREF="iostream.html"><iostream></A></CODE>,<CODE><A HREF="istream.html"><istream></A></CODE>,<CODE><A HREF="new.html"><new></A></CODE>,<CODE><A HREF="ostream.html"><ostream></A></CODE>,<CODE><A HREF="sstream.html"><sstream></A></CODE>,<CODE><A HREF="streambu.html"><streambuf></A></CODE>,<CODE><A HREF="string2.html"><string></A></CODE>, and<CODE><A HREF="strstrea.html"><strstream></A></CODE></P><P>A <B><A NAME="freestanding implementation">freestanding implementation</A></B>of the C++ library provides only a subset of these headers:<CODE><A HREF="cstddef.html"><cstddef></A></CODE>,<CODE><A HREF="cstdlib.html"><cstdlib></A></CODE>(declaring at least the functions<A HREF="stdlib.html#abort"><CODE>abort</CODE></A>,<A HREF="stdlib.html#atexit"><CODE>atexit</CODE></A>, and<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>),<CODE><A HREF="new.html"><new></A></CODE>, and<CODE><A HREF="cstdarg.html"><cstdarg></A></CODE>.</P><P>The C++ library headers also have a broader subdivision --<A HREF="#iostreams">iostreams</A> headers.</P><H2><A NAME="Using C++ Library Headers">Using C++ Library Headers</A></H2><P>You include the contents of a standard header by naming it in an<A HREF="preproc.html#include directive"><I>include</I> directive</A>,as in:</P><PRE>#include <iostream> /* include I/O facilities */</PRE><P>You can include the standard headers in any order, a standardheader more than once, or two or more standard headers that definethe same macro or the same type.Do not include a standard header within a declaration. Do notdefine macros that have the same names as keywords before you includea standard header.</P><P>A C++ library header includes any other C++ library headersit needs to define needed types. (Always include explicitly anyC++ library headers needed in a translation unit, however, lestyou guess wrong about its actual dependencies.) A Standard C headernever includes another standard header. A standard header declaresor defines only the entities described for it in this document.</P><P>Every function in the library is declared in a standard header.Unlike in Standard C, the standard header never provides a<A HREF="lib_over.html#masking macro">masking macro</A>,with the same name as the function, that masks the functiondeclaration and achieves the same effect.</P><P>The C++ Standard requires that nearly all namesin the C++ library headers be defined in the<B><CODE><A NAME="std namespace">std</A></CODE></B> namespace,or in a namespacenested within the <CODE>std</CODE> namespace.Otherwise, all names are defined in the global namespace.In this <A HREF="index.html#implementation">implementation</A>,however, you can ignore namespaces.</P><H2><A NAME="C++ Library Conventions">C++ Library Conventions</A></H2><P>The C++ library obeys much the same<A HREF="lib_over.html#C Library Conventions">conventions</A>as the Standard C library, plus a few more outlined here.<P>An implementation has certain latitude in how it declares typesand functions in the C++ library:</P><UL><LI>Names of functions in the Standard C library may have either<B><A NAME="extern "C++""><CODE>extern "C++"</CODE></A></B> or<B><A NAME="extern "C""><CODE>extern "C"</CODE></A></B> linkage.Include the appropriate<A HREF="lib_over.html#standard headers">Standard C header</A>rather than declare a library entity inline.</LI><LI>A member function name in a library class may have additionalfunction signatures over those listed in this document. You canbe 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.)</LI><LI>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.</LI><LI>A type defined as a synonym for some integer type may be thesame as one of several different integer types.</LI><LI>A <B><A NAME="bitmask type">bitmask type</A></B> canbe implemented as either an integer type or an enumeration.In either case, you can perform bitwise operations (such as ANDand OR) on values of the same bitmask type. The <I>elements</I><CODE>A</CODE> and <CODE>B</CODE> of a bitmask type are nonzerovalues such that <CODE>A & B</CODE> is zero.</LI><LI>A library function that has no exception specification canthrow an arbitrary exception, unless its definition clearlyrestricts such a possibility.</LI></UL><P>On the other hand, there are some restrictions you can count on:</P><UL><LI>The Standard C library uses no<A NAME="masking macro">masking macros</A>. Only specific functionsignatures are reserved, not the names of the functions themselves.</LI><LI>A library function name outside a class will <I>not</I> haveadditional, undocumented, function signatures. You can reliablytake its address.</LI><LI>Base classes and member functions described as virtual areassuredly virtual, while those described as non-virtual areassuredly non-virtual.</LI><LI>Two types defined by the C++ libraryare always different unless this document explicitly suggestsotherwise.</LI><LI>Functions supplied by the library, including the default versions of<A HREF="new.html#replaceable functions">replaceable functions</A>,can throw <I>at most</I> those exceptions listed in any exceptionspecification.No destructors supplied by the library throw exceptions.Functions in the<A HREF="index.html#Standard C Library">Standard C library</A>may propagate an exception, as when<CODE><A HREF="stdlib.html#qsort">qsort</A></CODE> calls a comparisonfunction that throws an exception, but they do not otherwise throwexceptions.</LI></UL><H2><A NAME="iostreams">Iostreams Conventions</A></H2><P>The <B>iostreams</B> headers support conversionsbetween text and encoded forms, and input and output to external<A HREF="lib_file.html#files">files</A>:<CODE><A HREF="fstream.html"><fstream></A></CODE>,<CODE><A HREF="iomanip.html"><iomanip></A></CODE>,<CODE><A HREF="ios.html"><ios></A></CODE>,<CODE><A HREF="iosfwd.html"><iosfwd></A></CODE>,<CODE><A HREF="iostream.html"><iostream></A></CODE>,<CODE><A HREF="istream.html"><istream></A></CODE>,<CODE><A HREF="ostream.html"><ostream></A></CODE>,<CODE><A HREF="sstream.html"><sstream></A></CODE>,<CODE><A HREF="streambu.html"><streambuf></A></CODE>, and<CODE><A HREF="strstrea.html"><strstream></A></CODE>.</P><P>The simplest use of iostreams requires only that you includethe header <CODE><iostream></CODE>. You can then extract values from<A HREF="iostream.html#cin"><CODE>cin</CODE></A>, to read the<A HREF="lib_over.html#standard input">standard input</A>.The rules for doing so are outlined in the description of the class<CODE><A HREF="istream.html#istream">istream</A></CODE>.You can also insert values to<A HREF="iostream.html#cout"><CODE>cout</CODE></A>, to write the<A HREF="lib_over.html#standard output">standard output</A>.The rules for doing so are outlined in the description of the class<CODE><A HREF="ostream.html#ostream">ostream</A></CODE>.Format control common to both extractors and insertors is managedby the class <CODE><A HREF="ios.html#ios">ios</A></CODE>.Manipulating this format information in the guise of extracting andinserting objects is the province of several<A HREF="iomanip.html">manipulators</A>.</P><P>You can perform the same iostreams operations on files that youopen by name, using the classes declared in<CODE><fstream></CODE>.To convert between iostreams and objects of class<CODE><A HREF="string2.html#string">string</A></CODE>,use the classes declared in <CODE><sstream></CODE>.And to do the same with <A HREF="lib_over.html#C string">C strings</A>,use the classes declared in <CODE><strstream></CODE>.</P><P>The remaining headers provide support services, typically of directinterest to only the most advanced users of the iostreams classes.</P><H2><A NAME="C++ Program Startup and Termination">C++ Program Startup and Termination</A></H2><P>A C++ program performs the same operations as does a C program<A HREF="lib_over.html#program startup">program startup</A> and at<A HREF="lib_over.html#program termination">program termination</A>,plus a few more outlined here.</P><P>Before the target environment calls the function<A HREF="lib_over.html#main"><CODE>main</CODE></A>, and after it storesany constant initial values you specify in all objects that havestatic duration, the program executes any remaining constructorsfor such static objects. The order of execution is not specifiedbetween translation units, but you can nevertheless assume that some<A HREF="#iostreams">iostreams</A> objects are properly initializedfor use by these static constructors. These controltext streams:</P><UL><LI><B><A HREF="iostream.html#cin"><CODE>cin</CODE></A></B> --for <A HREF="lib_over.html#standard input">standard input</A></LI><LI><B><A HREF="iostream.html#cout"><CODE>cout</CODE></A></B> --for <A HREF="lib_over.html#standard output">standard output</A></LI></UL><P>You can also use these objects within the destructors called forstatic objects, during<A HREF="lib_over.html#program termination">program termination</A>.<P>As with C, returningfrom <A HREF="lib_over.html#main"><CODE>main</CODE></A> or calling<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>calls all functions registered with<A HREF="stdlib.html#atexit"><CODE>atexit</CODE></A>in reverse order of registry.</P><H2><A NAME="Exceptions">Exceptions</A></H2><P>In this <A HREF="index.html#implementation">implementation</A>,exception handling can be either enabled or disabled.This document describes all behavior as if exception handling is enabled.If exception handling is disabled, however:</P><UL><LI>Throw specifications in library function declarationsare not actually present.</LI><LI>Catch clauses in library function definitions likewise are notactually present. It is not possible for the program to catch anexception, except in the limited sense outlined below. Hence, thelibrary has no occasion to rethrow an exception.</LI><LI>Rather than throw an exception, as in <CODE>throw ex</CODE>, the libraryactually calls <CODE>ex.<A HREF="#_Raise">_Raise</A>()</CODE>.</LI></UL><P>Here, <CODE>void <A NAME="_Raise">_Raise</A>()</CODE>is a member function of class <CODE>exception</CODE>,the base class for all exceptions thrown by the library. It performs the followingoperations, in order:</P><OL><LI>If a <B><A NAME="raise handler">raise handler</A></B>has been registered by an earlier call to the static member function<CODE>exception::<A NAME="_Set_raise_handler">_Set_raise_handler</A>(void(*)(const exception&)</CODE>, then <CODE>_Raise</CODE> calls theraise handler.</LI><LI><CODE>_Raise</CODE> then calls the protected virtual member function<CODE>void <A NAME="_Doraise">_Doraise</A>()</CODE>, which typically calls<CODE><A HREF="#_Throw">_Throw</A>(*this)</CODE> in any class derivedfrom <CODE>exception</CODE>. (This ensures that the most derived versionof the virtual public member function <CODE>what</CODE> gets calledby <CODE>_Throw</CODE>, as outlined below.)</LI><LI><CODE>_Raise</CODE> then calls <CODE>_Throw(*this)</CODE>.</LI></OL><P>The replaceable global function<CODE>void <A NAME="_Throw">_Throw</A>(const exception& ex)</CODE>never returns to its caller.If the pointer returned by <CODE>ex.what()</CODE> is not a null pointer,the function writes to the<A HREF="lib_over.html#standard error">standard error</A> output streama diagnostic message that includes the<A HREF="charset.html#null-terminated string">null-terminated string</A>designated by the pointer. In any event, the function then calls<CODE><A HREF="stdlib.html#abort">abort</A></CODE>.</P><P>The net effect of all this machinery is to supply several levels of control,in lieu of the normal exception-handling machinery:</P><UL><LI>You can dynamically specify a raise handler that is called whenever thelibrary would normally throw any exceptionderived from class <CODE>exception</CODE>.</LI><LI>You can override <CODE>_Doraise</CODE>, in a class you derivefrom <CODE>exception</CODE>, to get control whenever an object of thatclass would normally be thrown by the library (assuming that any raise handleryou register returns to its caller).</LI><LI>You can define your own version of <CODE>_Throw</CODE>, to statically handletermination on all thrown exceptions as you see fit.</LI></UL><HR><P>See also the<B><A HREF="index.html#Table of Contents">Table of Contents</A></B> and the<B><A HREF="_index.html">Index</A></B>.</P><P><I><A HREF="crit_pjp.html">Copyright</A> © 1992-2002by P.J. Plauger. All rights reserved.</I></P><!--V4.01:1125--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -