B. Stroustrup - The C++ Programming Language (794319), страница 2
Текст из файла (страница 2)
From thepoint of view of a programmer, most of the changes have been improvements. The current ISOstandard C++ (ISO/IEC 14882-2011, usually called C++11) is simply a far better tool for writingquality software than were previous versions. How is it a better tool? What kinds of programmingstyles and techniques does modern C++ support? What language and standard-library features support those techniques? What are the basic building blocks of elegant, correct, maintainable, andefficient C++ code? Those are the key questions answered by this book. Many answers are not thesame as you would find with 1985, 1995, or 2005 vintage C++: progress happens.C++ is a general-purpose programming language emphasizing the design and use of type-rich,lightweight abstractions.
It is particularly suited for resource-constrained applications, such asthose found in software infrastructures. C++ rewards the programmer who takes the time to masterviPrefacetechniques for writing quality code. C++ is a language for someone who takes the task of programming seriously. Our civilization depends critically on software; it had better be quality software.There are billions of lines of C++ deployed. This puts a premium on stability, so 1985 and1995 C++ code still works and will continue to work for decades. However, for all applications,you can do better with modern C++; if you stick to older styles, you will be writing lower-qualityand worse-performing code.
The emphasis on stability also implies that standards-conformingcode you write today will still work a couple of decades from now. All code in this book conformsto the 2011 ISO C++ standard.This book is aimed at three audiences:• C++ programmers who want to know what the latest ISO C++ standard has to offer,• C programmers who wonder what C++ provides beyond C, and• People with a background in application languages, such as Java, C#, Python, and Ruby,looking for something ‘‘closer to the machine’’ – something more flexible, something offering better compile-time checking, or something offering better performance.Naturally, these three groups are not disjoint – a professional software developer masters more thanjust one programming language.This book assumes that its readers are programmers.
If you ask, ‘‘What’s a for-loop?’’ or‘‘What’s a compiler?’’ then this book is not (yet) for you; instead, I recommend my Programming:Principles and Practice Using C++ to get started with programming and C++. Furthermore, Iassume that readers have some maturity as software developers. If you ask ‘‘Why bother testing?’’or say, ‘‘All languages are basically the same; just show me the syntax’’ or are confident that thereis a single language that is ideal for every task, this is not the book for you.What features does C++11 offer over and above C++98? A machine model suitable for moderncomputers with lots of concurrency.
Language and standard-library facilities for doing systemslevel concurrent programming (e.g., using multicores). Regular expression handling, resourcemanagement pointers, random numbers, improved containers (including, hash tables), and more.General and uniform initialization, a simpler for-statement, move semantics, basic Unicode support,lambdas, general constant expressions, control over class defaults, variadic templates, user-definedliterals, and more. Please remember that those libraries and language features exist to support programming techniques for developing quality software. They are meant to be used in combination –as bricks in a building set – rather than to be used individually in relative isolation to solve a specific problem. A computer is a universal machine, and C++ serves it in that capacity.
In particular,C++’s design aims to be sufficiently flexible and general to cope with future problems undreamedof by its designers.viiAcknowledgmentsIn addition to the people mentioned in the acknowledgment sections of the previous editions, Iwould like to thank Pete Becker, Hans-J. Boehm, Marshall Clow, Jonathan Coe, Lawrence Crowl,Walter Daugherty, J.
Daniel Garcia, Robert Harle, Greg Hickman, Howard Hinnant, BrianKernighan, Daniel Krügler, Nevin Liber, Michel Michaud, Gary Powell, Jan Christiaan van Winkel,and Leor Zolman. Without their help this book would have been much poorer.Thanks to Howard Hinnant for answering many questions about the standard library.Andrew Sutton is the author of the Origin library, which was the testbed for much of the discussion of emulating concepts in the template chapters, and of the matrix library that is the topic ofChapter 29. The Origin library is open source and can be found by searching the Web for ‘‘Origin’’and ‘‘Andrew Sutton.’’Thanks to my graduate design class for finding more problems with the ‘‘tour chapters’’ thananyone else.Had I been able to follow every piece of advice of my reviewers, the book would undoubtedlyhave been much improved, but it would also have been hundreds of pages longer.
Every expertreviewer suggested adding technical details, advanced examples, and many useful developmentconventions; every novice reviewer (or educator) suggested adding examples; and most reviewersobserved (correctly) that the book may be too long.Thanks to Princeton University’s Computer Science Department, and especially Prof.
BrianKernighan, for hosting me for part of the sabbatical that gave me time to write this book.Thanks to Cambridge University’s Computer Lab, and especially Prof. Andy Hopper, for hosting me for part of the sabbatical that gave me time to write this book.Thanks to my editor, Peter Gordon, and his production team at Addison-Wesley for their helpand patience.College Station, TexasBjarne StroustrupThis page intentionally left blankPreface to the Third EditionProgramming is understanding.– Kristen NygaardI find using C++ more enjoyable than ever. C++’s support for design and programming hasimproved dramatically over the years, and lots of new helpful techniques have been developed forits use. However, C++ is not just fun.
Ordinary practical programmers have achieved significantimprovements in productivity, maintainability, flexibility, and quality in projects of just about anykind and scale. By now, C++ has fulfilled most of the hopes I originally had for it, and also succeeded at tasks I hadn’t even dreamt of.This book introduces standard C++† and the key programming and design techniques supportedby C++. Standard C++ is a far more powerful and polished language than the version of C++ introduced by the first edition of this book.
New language features such as namespaces, exceptions,templates, and run-time type identification allow many techniques to be applied more directly thanwas possible before, and the standard library allows the programmer to start from a much higherlevel than the bare language.About a third of the information in the second edition of this book came from the first. Thisthird edition is the result of a rewrite of even larger magnitude. It offers something to even the mostexperienced C++ programmer; at the same time, this book is easier for the novice to approach thanits predecessors were.
The explosion of C++ use and the massive amount of experience accumulated as a result makes this possible.The definition of an extensive standard library makes a difference to the way C++ concepts canbe presented. As before, this book presents C++ independently of any particular implementation,and as before, the tutorial chapters present language constructs and concepts in a ‘‘bottom up’’order so that a construct is used only after it has been defined. However, it is much easier to use awell-designed library than it is to understand the details of its implementation. Therefore, the standard library can be used to provide realistic and interesting examples well before a reader can beassumed to understand its inner workings.
The standard library itself is also a fertile source of programming examples and design techniques.This book presents every major C++ language feature and the standard library. It is organizedaround language and library facilities. However, features are presented in the context of their use.† ISO/IEC 14882, Standard for the C++ Programming Language.xPreface to the Third EditionThat is, the focus is on the language as the tool for design and programming rather than on the language in itself.
This book demonstrates key techniques that make C++ effective and teaches thefundamental concepts necessary for mastery. Except where illustrating technicalities, examples aretaken from the domain of systems software. A companion, The Annotated C++ Language Standard, presents the complete language definition together with annotations to make it more comprehensible.The primary aim of this book is to help the reader understand how the facilities offered by C++support key programming techniques.