B. Stroustrup - The C++ Programming Language (794319), страница 5
Текст из файла (страница 5)
It describes formatted and unformatted input and output, error handling, and buffering.Locales describes class locale and its various facets that provide support for thehandling of cultural differences in character sets, formatting of numeric values,formatting of date and time, and more.Numerics describes facilities for numerical computation (such as complex,valarray, random numbers, and generalized numerical algorithms).Concurrency presents the C++ basic memory model and the facilities offered forconcurrent programming without locks.Threads and Tasks presents the classes providing threads-and-locks-style concurrent programming (such as thread, timed_mutex, lock_guard, and try_lock()) andthe support for task-based concurrency (such as future and async()).The C Standard Library documents the C standard library (including printf() andclock()) as incorporated into the C++ standard library.Compatibility discusses the relation between C and C++ and between StandardC++ (also called ISO C++) and the versions of C++ that preceded it.1.1.5 Examples and ReferencesThis book emphasizes program organization rather than the design of algorithms.
Consequently, Iavoid clever or harder-to-understand algorithms. A trivial algorithm is typically better suited toillustrate an aspect of the language definition or a point about program structure. For example, Iuse a Shell sort where, in real code, a quicksort would be better. Often, reimplementation with amore suitable algorithm is an exercise. In real code, a call of a library function is typically moreappropriate than the code used here to illustrate language features.Textbook examples necessarily give a warped view of software development. By clarifying andsimplifying the examples, the complexities that arise from scale disappear.
I see no substitute forwriting realistically sized programs in order to get an impression of what programming and a8Notes to the ReaderChapter 1programming language are really like. This book concentrates on the language features and thestandard-library facilities. These are the basic techniques from which every program is composed.The rules and techniques for such composition are emphasized.The selection of examples reflects my background in compilers, foundation libraries, and simulations. The emphasis reflects my interest in systems programming.
Examples are simplified versions of what is found in real code. The simplification is necessary to keep programming languageand design points from getting lost in details. My ideal is the shortest and clearest example thatillustrates a design principle, a programming technique, a language construct, or a library feature.There are no ‘‘cute’’ examples without counterparts in real code. For purely language-technicalexamples, I use variables named x and y, types called A and B, and functions called f() and g().Where possible, the C++ language and library features are presented in the context of their userather than in the dry manner of a manual. The language features presented and the detail in whichthey are described roughly reflect my view of what is needed for effective use of C++.
The purposeis to give you an idea of how a feature can be used, often in combination with other features. Anunderstanding of every language-technical detail of a language feature or library component is neither necessary nor sufficient for writing good programs. In fact, an obsession with understandingevery little detail is a prescription for awful – overelaborate and overly clever – code. What isneeded is an understanding of design and programming techniques together with an appreciation ofapplication domains.I assume that you have access to online information sources.
The final arbiter of language andstandard-library rules is the ISO C++ standard [C++,2011].References to parts of this book are of the form §2.3.4 (Chapter 2, section 3, subsection 4) and§iso.5.3.1 (ISO C++ standard, §5.3.1). Italics are used sparingly for emphasis (e.g., ‘‘a string literalis not acceptable’’), for first occurrences of important concepts (e.g., polymorphism), and for comments in code examples.To save a few trees and to simplify additions, the hundreds of exercises for this book have beenmoved to the Web. Look for them at www.stroustrup.com.The language and library used in this book are ‘‘pure C++’’ as defined by the C++ standard[C++,2011].
Therefore, the examples should run on every up-to-date C++ implementation. Themajor program fragments in this book were tried using several C++ implementations. Examplesusing features only recently adopted into C++ didn’t compile on every implementation. However, Isee no point in mentioning which implementations failed to compile which examples. Such information would soon be out of date because implementers are working hard to ensure that theirimplementations correctly accept every C++ feature. See Chapter 44 for suggestions on how tocope with older C++ compilers and with code written for C compilers.I use C++11 features freely wherever I find them most appropriate.
For example, I prefer{}-style initializers and using for type aliases. In places, that usage may startle ‘‘old timers.’’ However, being startled is often a good way to start reviewing material. On the other hand, I don’t usenew features just because they are new; my ideal is the most elegant expression of the fundamentalideas – and that may very well be using something that has been in C++ or even in C for ages.Obviously, if you have to use a pre-C++11 compiler (say, because some of your customers havenot yet upgraded to the current standard), you have to refrain from using novel features. However,please don’t assume that ‘‘the old ways’’ are better or simpler just because they are old and familiar.§44.2 summarizes the differences between C++98 and C++11.Section 1.2The Design of C++91.2 The Design of C++The purpose of a programming language is to help express ideas in code.
In that, a programminglanguage performs two related tasks: it provides a vehicle for the programmer to specify actions tobe executed by the machine, and it provides a set of concepts for the programmer to use whenthinking about what can be done. The first purpose ideally requires a language that is ‘‘close to themachine’’ so that all important aspects of a machine are handled simply and efficiently in a waythat is reasonably obvious to the programmer. The C language was primarily designed with this inmind. The second purpose ideally requires a language that is ‘‘close to the problem to be solved’’so that the concepts of a solution can be expressed directly and concisely.
The facilities added to Cto create C++, such as function argument checking, const, classes, constructors and destructors,exceptions, and templates, were primarily designed with this in mind. Thus, C++ is based on theidea of providing both• direct mappings of built-in operations and types to hardware to provide efficient memoryuse and efficient low-level operations, and• affordable and flexible abstraction mechanisms to provide user-defined types with the samenotational support, range of uses, and performance as built-in types.This was initially achieved by applying ideas from Simula to C.
Over the years, further applicationof these simple ideals resulted in a far more general, efficient, and flexible set of facilities. Theresult supports a synthesis of programming styles that can be simultaneously efficient and elegant.The design of C++ has focused on programming techniques dealing with fundamental notionssuch as memory, mutability, abstraction, resource management, expression of algorithms, error handling, and modularity. Those are the most important concerns of a systems programmer and moregenerally of programmers of resource-constrained and high-performance systems.By defining libraries of classes, class hierarchies, and templates, you can write C++ programs ata much higher level than the one presented in this book. For example, C++ is widely used in financial systems, for game development, and for scientific computation (§1.4.5).
For high-level applications programming to be effective and convenient, we need libraries. Using just the bare language features makes almost all programming quite painful. That’s true for every general-purposelanguage. Conversely, given suitable libraries just about any programming task can be pleasant.My standard introduction of C++ used to start:• C++ is a general-purpose programming language with a bias toward systems programming.This is still true. What has changed over the years is an increase in the importance, power, andflexibility of C++’s abstraction mechanisms:• C++ is a general-purpose programming language providing a direct and efficient model ofhardware combined with facilities for defining lightweight abstractions.Or terser:• C++ is a language for developing and using elegant and efficient abstractions.By general-purpose programming language I mean a language designed to support a wide varietyof uses.
C++ has indeed been used for an incredible variety of uses (from microcontrollers to hugedistributed commercial applications), but the key point is that C++ is not deliberately specializedfor any given application area. No language is ideal for every application and every programmer,but the ideal for C++ is to support the widest possible range of application areas well.10Notes to the ReaderChapter 1By systems programming I mean writing code that directly uses hardware resources, has seriousresource constraints, or closely interacts with code that does.
In particular, the implementation ofsoftware infrastructure (e.g., device drivers, communications stacks, virtual machines, operatingsystems, operations systems, programming environments, and foundation libraries) is mostly systems programming. The importance of the ‘‘bias toward systems programming’’ qualification inmy long-standing characterization of C++ is that C++ has not been simplified (compromised) byejecting the facilities aimed at the expert-level use of hardware and systems resources in the hopeof making it more suitable for other application areas.Of course, you can also program in ways that completely hide hardware, use expensive abstractions (e.g., every object on the free store and every operation a virtual function), use inelegant styles(e.g., overabstraction), or use essentially no abstractions (‘‘glorified assembly code’’).
However,many languages can do that, so those are not distinguishing characteristics of C++.The Design and Evolution of C++ book [Stroustrup,1994] (known as D&E) outlines the ideasand design aims of C++ in greater detail, but two principles should be noted:• Leave no room for a lower-level language below C++ (except for assembly code in rarecases). If you can write more efficient code in a lower-level language then that languagewill most likely become the systems programming language of choice.• What you don’t use you don’t pay for. If programmers can hand-write reasonable code tosimulate a language feature or a fundamental abstraction and provide even slightly betterperformance, someone will do so, and many will imitate.