Addison Wesley Stroustrup The C++ Programming Language Special Edition

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Addison Wesley Stroustrup The C++ Programming Language Special Edition - Presentation Transcript

    1. The C++ Programming Language Third Edition Bjarne Stroustrup AT&T Labs Murray Hill, New Jersey Addison-Wesley An Imprint of Addison Wesley Longman, Inc. Reading, Massachusetts • Harlow, England • Menlo Park, California Berkeley, California • Don Mills, Ontario • Sydney Bonn • Amsterdam • Tokyo • Mexico City
    2. ii Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial capital letters or all capital letters The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information contained herein. The publisher offers discounts on this book when ordered in quantity for special sales. For more information please contact: Corporate & Professional Publishing Group Addison-Wesley Publishing Company One Jacob Way Reading, Massachusetts 01867 Library of Congress Cataloging-in-Publication Data Stroustrup, Bjarne The C++ Programming Language / Bjarne Stroustrup. — 3rd. ed. p. cm. Includes index. ISBN 0-201-88954-4 1. C++ (Computer Programming Language) I. Title QA76.73.C153S77 1997 97-20239 005.13’3—dc21 CIP Copyright © 1997 by AT&T All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. This book was typeset in Times and Courier by the author. ISBN 0-201-88954-4 Printed on recycled paper 1 2 3 4 5 6 7 8 9—CRW—0100999897 First printing, June 1997
    3. Contents Contents iii Preface v Preface to Second Edition vii Preface to First Edition ix Introductory Material 1 1 Notes to the Reader ..................................................................... 3 2 A Tour of C++ ............................................................................. 21 3 A Tour of the Standard Library .................................................. 45 Part I: Basic Facilities 67 4 Types and Declarations ............................................................... 69 5 Pointers, Arrays, and Structures .................................................. 87 6 Expressions and Statements ........................................................ 107 7 Functions ..................................................................................... 143 8 Namespaces and Exceptions ....................................................... 165 9 Source Files and Programs .......................................................... 197
    4. iv Contents Part II: Abstraction Mechanisms 221 10 Classes ........................................................................................ 223 11 Operator Overloading ................................................................. 261 12 Derived Classes ........................................................................... 301 13 Templates .................................................................................... 327 14 Exception Handling .................................................................... 355 15 Class Hierarchies ........................................................................ 389 Part III: The Standard Library 427 16 Library Organization and Containers .......................................... 429 17 Standard Containers .................................................................... 461 18 Algorithms and Function Objects ............................................... 507 19 Iterators and Allocators ............................................................... 549 20 Strings ......................................................................................... 579 21 Streams ........................................................................................ 605 22 Numerics ..................................................................................... 657 Part IV: Design Using C++ 689 23 Development and Design ............................................................ 691 24 Design and Programming ........................................................... 723 25 Roles of Classes .......................................................................... 765 Appendices 791 A The C++ Grammar ...................................................................... 793 B Compatibility .............................................................................. 815 C Technicalities .............................................................................. 827 Index 869
    5. Preface Programming is understanding. – Kristen Nygaard I find using C++ more enjoyable than ever. C++’s support for design and programming has improved dramatically over the years, and lots of new helpful techniques have been developed for its use. However, C++ is not just fun. Ordinary practical programmers have achieved significant improvements in productivity, maintainability, flexibility, and quality in projects of just about any kind and scale. By now, C++ has fulfilled most of the hopes I originally had for it, and also suc- ceeded at tasks I hadn’t even dreamt of. This book introduces standard C++† and the key programming and design techniques supported by C++. Standard C++ is a far more powerful and polished language than the version of C++ intro- duced 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 than was possible before, and the standard library allows the programmer to start from a much higher level than the bare language. About a third of the information in the second edition of this book came from the first. This third edition is the result of a rewrite of even larger magnitude. It offers something to even the most experienced C++ programmer; at the same time, this book is easier for the novice to approach than its predecessors were. The explosion of C++ use and the massive amount of experience accu- mulated as a result makes this possible. The definition of an extensive standard library makes a difference to the way C++ concepts can be 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 a well-designed library than it is to understand the details of its implementation. Therefore, the stan- dard library can be used to provide realistic and interesting examples well before a reader can be assumed to understand its inner workings. The standard library itself is also a fertile source of pro- gramming examples and design techniques. __________________ † ISO/IEC 14882, Standard for the C++ Programming Language.
    6. vi Preface This book presents every major C++ language feature and the standard library. It is organized around language and library facilities. However, features are presented in the context of their use. That is, the focus is on the language as the tool for design and programming rather than on the lan- guage in itself. This book demonstrates key techniques that make C++ effective and teaches the fundamental concepts necessary for mastery. Except where illustrating technicalities, examples are taken from the domain of systems software. A companion, The Annotated C++ Language Stan- dard, presents the complete language definition together with annotations to make it more compre- hensible. The primary aim of this book is to help the reader understand how the facilities offered by C++ support key programming techniques. The aim is to take the reader far beyond the point where he or she gets code running primarily by copying examples and emulating programming styles from other languages. Only a good understanding of the ideas behind the language facilities leads to mastery. Supplemented by implementation documentation, the information provided is sufficient for completing significant real-world projects. The hope is that this book will help the reader gain new insights and become a better programmer and designer. Acknowledgments In addition to the people mentioned in the acknowledgement sections of the first and second edi- tions, I would like to thank Matt Austern, Hans Boehm, Don Caldwell, Lawrence Crowl, Alan Feuer, Andrew Forrest, David Gay, Tim Griffin, Peter Juhl, Brian Kernighan, Andrew Koenig, Mike Mowbray, Rob Murray, Lee Nackman, Joseph Newcomer, Alex Stepanov, David Vandevo- orde, Peter Weinberger, and Chris Van Wyk for commenting on draft chapters of this third edition. Without their help and suggestions, this book would have been harder to understand, contained more errors, been slightly less complete, and probably been a little bit shorter. I would also like to thank the volunteers on the C++ standards committees who did an immense amount of constructive work to make C++ what it is today. It is slightly unfair to single out indi- viduals, but it would be even more unfair not to mention anyone, so I’d like to especially mention .. Mike Ball, Dag Bruck, Sean Corfield, Ted Goldstein, Kim Knuttila, Andrew Koenig, Josée Lajoie, Dmitry Lenkov, Nathan Myers, Martin O’Riordan, Tom Plum, Jonathan Shopiro, John Spicer, Jerry Schwarz, Alex Stepanov, and Mike Vilot, as people who each directly cooperated with me over some part of C++ and its standard library. Murray Hill, New Jersey Bjarne Stroustrup
    7. Preface to the Second Edition The road goes ever on and on. – Bilbo Baggins As promised in the first edition of this book, C++ has been evolving to meet the needs of its users. This evolution has been guided by the experience of users of widely varying backgrounds working in a great range of application areas. The C++ user-community has grown a hundredfold during the six years since the first edition of this book; many lessons have been learned, and many techniques have been discovered and/or validated by experience. Some of these experiences are reflected here. The primary aim of the language extensions made in the last six years has been to enhance C++ as a language for data abstraction and object-oriented programming in general and to enhance it as a tool for writing high-quality libraries of user-defined types in particular. A ‘‘high-quality library,’’ is a library that provides a concept to a user in the form of one or more classes that are convenient, safe, and efficient to use. In this context, safe means that a class provides a specific type-safe interface between the users of the library and its providers; efficient means that use of the class does not impose significant overheads in run-time or space on the user compared with hand- written C code. This book presents the complete C++ language. Chapters 1 through 10 give a tutorial introduc- tion; Chapters 11 through 13 provide a discussion of design and software development issues; and, finally, the complete C++ reference manual is included. Naturally, the features added and resolu- tions made since the original edition are integral parts of the presentation. They include refined overloading resolution, memory management facilities, and access control mechanisms, type-safe linkage, c on st and s ta ti c member functions, abstract classes, multiple inheritance, templates, and co ns t st at ic exception handling. C++ is a general-purpose programming language; its core application domain is systems pro- gramming in the broadest sense. In addition, C++ is successfully used in many application areas that are not covered by this label. Implementations of C++ exist from some of the most modest microcomputers to the largest supercomputers and for almost all operating systems. Consequently, this book describes the C++ language itself without trying to explain a particular implementation, programming environment, or library. This book presents many examples of classes that, though useful, should be classified as ‘‘toys.’’ This style of exposition allows general principles and useful techniques to stand out more
    8. viii Preface to the Second Edition clearly than they would in a fully elaborated program, where they would be buried in details. Most of the useful classes presented here, such as linked lists, arrays, character strings, matrices, graphics classes, associative arrays, etc., are available in ‘‘bulletproof’’ and/or ‘‘goldplated’’ versions from a wide variety of commercial and non-commercial sources. Many of these ‘‘industrial strength’’ classes and libraries are actually direct and indirect descendants of the toy versions found here. This edition provides a greater emphasis on tutorial aspects than did the first edition of this book. However, the presentation is still aimed squarely at experienced programmers and endeavors not to insult their intelligence or experience. The discussion of design issues has been greatly expanded to reflect the demand for information beyond the description of language features and their immediate use. Technical detail and precision have also been increased. The reference man- ual, in particular, represents many years of work in this direction. The intent has been to provide a book with a depth sufficient to make more than one reading rewarding to most programmers. In other words, this book presents the C++ language, its fundamental principles, and the key tech- niques needed to apply it. Enjoy! Acknowledgments In addition to the people mentioned in the acknowledgements section in the preface to the first edi- tion, I would like to thank Al Aho, Steve Buroff, Jim Coplien, Ted Goldstein, Tony Hansen, Lor- raine Juhl, Peter Juhl, Brian Kernighan, Andrew Koenig, Bill Leggett, Warren Montgomery, Mike Mowbray, Rob Murray, Jonathan Shopiro, Mike Vilot, and Peter Weinberger for commenting on draft chapters of this second edition. Many people influenced the development of C++ from 1985 to 1991. I can mention only a few: Andrew Koenig, Brian Kernighan, Doug McIlroy, and Jonathan Shopiro. Also thanks to the many participants of the ‘‘external reviews’’ of the reference manual drafts and to the people who suffered through the first year of X3J16. Murray Hill, New Jersey Bjarne Stroustrup
    9. Preface to the First Edition Language shapes the way we think, and determines what we can think about. – B.L.Whorf C++ is a general purpose programming language designed to make programming more enjoyable for the serious programmer. Except for minor details, C++ is a superset of the C programming lan- guage. In addition to the facilities provided by C, C++ provides flexible and efficient facilities for defining new types. A programmer can partition an application into manageable pieces by defining new types that closely match the concepts of the application. This technique for program construc- tion is often called data abstraction. Objects of some user-defined types contain type information. Such objects can be used conveniently and safely in contexts in which their type cannot be deter- mined at compile time. Programs using objects of such types are often called object based. When used well, these techniques result in shorter, easier to understand, and easier to maintain programs. The key concept in C++ is class. A class is a user-defined type. Classes provide data hiding, guaranteed initialization of data, implicit type conversion for user-defined types, dynamic typing, user-controlled memory management, and mechanisms for overloading operators. C++ provides much better facilities for type checking and for expressing modularity than C does. It also contains improvements that are not directly related to classes, including symbolic constants, inline substitu- tion of functions, default function arguments, overloaded function names, free store management operators, and a reference type. C++ retains C’s ability to deal efficiently with the fundamental objects of the hardware (bits, bytes, words, addresses, etc.). This allows the user-defined types to be implemented with a pleasing degree of efficiency. C++ and its standard libraries are designed for portability. The current implementation will run on most systems that support C. C libraries can be used from a C++ program, and most tools that support programming in C can be used with C++. This book is primarily intended to help serious programmers learn the language and use it for nontrivial projects. It provides a complete description of C++, many complete examples, and many more program fragments.
    10. x Preface to the First Edition Acknowledgments C++ could never have matured without the constant use, suggestions, and constructive criticism of many friends and colleagues. In particular, Tom Cargill, Jim Coplien, Stu Feldman, Sandy Fraser, Steve Johnson, Brian Kernighan, Bart Locanthi, Doug McIlroy, Dennis Ritchie, Larry Rosler, Jerry Schwarz, and Jon Shopiro provided important ideas for development of the language. Dave Pre- sotto wrote the current implementation of the stream I/O library. In addition, hundreds of people contributed to the development of C++ and its compiler by sending me suggestions for improvements, descriptions of problems they had encountered, and compiler errors. I can mention only a few: Gary Bishop, Andrew Hume, Tom Karzes, Victor Milenkovic, Rob Murray, Leonie Rose, Brian Schmult, and Gary Walker. Many people have also helped with the production of this book, in particular, Jon Bentley, Laura Eaves, Brian Kernighan, Ted Kowalski, Steve Mahaney, Jon Shopiro, and the participants in the C++ course held at Bell Labs, Columbus, Ohio, June 26-27, 1985. Murray Hill, New Jersey Bjarne Stroustrup
    11. Introduction This introduction gives an overview of the major concepts and features of the C++ pro- gramming language and its standard library. It also provides an overview of this book and explains the approach taken to the description of the language facilities and their use. In addition, the introductory chapters present some background information about C++, the design of C++, and the use of C++. Chapters 1 Notes to the Reader 2 A Tour of C++ 3 A Tour of the Standard Library
    12. 2 Introduction Introduction ‘‘... and you, Marcus, you have given me many things; now I shall give you this good advice. Be many people. Give up the game of being always Marcus Cocoza. You have worried too much about Marcus Cocoza, so that you have been really his slave and prisoner. You have not done anything without first considering how it would affect Marcus Cocoza’s happiness and prestige. You were always much afraid that Marcus might do a stupid thing, or be bored. What would it really have mattered? All over the world people are doing stupid things ... I should like you to be easy, your lit- tle heart to be light again. You must from now, be more than one, many people, as many as you can think of ...’’ – Karen Blixen (‘‘The Dreamers’’ from ‘‘Seven Gothic Tales’’ written under the pseudonym Isak Dinesen, Random House, Inc. Copyright, Isac Dinesen, 1934 renewed 1961)
    13. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 1 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Notes to the Reader \"The time has come,\" the Walrus said, \"to talk of many things.\" – L.Carroll Structure of this book — how to learn C++ — the design of C++ — efficiency and struc- ture — philosophical note — historical note — what C++ is used for — C and C++ — suggestions for C programmers — suggestions for C++ programmers — thoughts about programming in C++ — advice — references. 1.1 The Structure of This Book This book consists of six parts: Introduction: Chapters 1 through 3 give an overview of the C++ language, the key programming styles it supports, and the C++ standard library. Part I: Chapters 4 through 9 provide a tutorial introduction to C++’s built-in types and the basic facilities for constructing programs out of them. Part II: Chapters 10 through 15 are a tutorial introduction to object-oriented and generic pro- gramming using C++. Part III: Chapters 16 through 22 present the C++ standard library. Part IV: Chapters 23 through 25 discuss design and software development issues. Appendices: Appendices A through E provide language-technical details. Chapter 1 provides an overview of this book, some hints about how to use it, and some background information about C++ and its use. You are encouraged to skim through it, read what appears inter- esting, and return to it after reading other parts of the book. Chapters 2 and 3 provide an overview of the major concepts and features of the C++ program- ming language and its standard library. Their purpose is to motivate you to spend time on funda- mental concepts and basic language features by showing what can be expressed using the complete
    14. 4 Notes to the Reader Chapter 1 C++ language. If nothing else, these chapters should convince you that C++ isn’t (just) C and that C++ has come a long way since the first and second editions of this book. Chapter 2 gives a high- level acquaintance with C++. The discussion focuses on the language features supporting data abstraction, object-oriented programming, and generic programming. Chapter 3 introduces the basic principles and major facilities of the standard library. This allows me to use standard library facilities in the following chapters. It also allows you to use library facilities in exercises rather than relying directly on lower-level, built-in features. The introductory chapters provide an example of a general technique that is applied throughout this book: to enable a more direct and realistic discussion of some technique or feature, I occasion- ally present a concept briefly at first and then discuss it in depth later. This approach allows me to present concrete examples before a more general treatment of a topic. Thus, the organization of this book reflects the observation that we usually learn best by progressing from the concrete to the abstract – even where the abstract seems simple and obvious in retrospect. Part I describes the subset of C++ that supports the styles of programming traditionally done in C or Pascal. It covers fundamental types, expressions, and control structures for C++ programs. Modularity – as supported by namespaces, source files, and exception handling – is also discussed. I assume that you are familiar with the fundamental programming concepts used in Part I. For example, I explain C++’s facilities for expressing recursion and iteration, but I do not spend much time explaining how these concepts are useful. Part II describes C++’s facilities for defining and using new types. Concrete and abstract classes (interfaces) are presented here (Chapter 10, Chapter 12), together with operator overloading (Chapter 11), polymorphism, and the use of class hierarchies (Chapter 12, Chapter 15). Chapter 13 presents templates, that is, C++’s facilities for defining families of types and functions. It demon- strates the basic techniques used to provide containers, such as lists, and to support generic pro- gramming. Chapter 14 presents exception handling, discusses techniques for error handling, and presents strategies for fault tolerance. I assume that you either aren’t well acquainted with object- oriented programming and generic programming or could benefit from an explanation of how the main abstraction techniques are supported by C++. Thus, I don’t just present the language features supporting the abstraction techniques; I also explain the techniques themselves. Part IV goes fur- ther in this direction. Part III presents the C++ standard library. The aim is to provide an understanding of how to use the library, to demonstrate general design and programming techniques, and to show how to extend the library. The library provides containers (such as l is t, v ec to r, and m ap Chapter 16, Chapter 17), li st ve ct or ma p; standard algorithms (such as s or t, f in d, and m er ge Chapter 18, Chapter 19), strings (Chapter 20), so rt fi nd me rg e; Input/Output (Chapter 21), and support for numerical computation (Chapter 22). Part IV discusses issues that arise when C++ is used in the design and implementation of large software systems. Chapter 23 concentrates on design and management issues. Chapter 24 discusses the relation between the C++ programming language and design issues. Chapter 25 presents some ways of using classes in design. Appendix A is C++’s grammar, with a few annotations. Appendix B discusses the relation between C and C++ and between Standard C++ (also called ISO C++ and ANSI C++) and the ver- sions of C++ that preceded it. Appendix C presents some language-technical examples. Appendix D explains the standard library’s facilities supporting internationalization. Appendix E discusses the exception-safety guarantees and requirements of the standard library.
    15. Section 1.1.1 Examples and References 5 1.1.1 Examples and References This book emphasizes program organization rather than the writing of algorithms. Consequently, I avoid clever or harder-to-understand algorithms. A trivial algorithm is typically better suited to illustrate an aspect of the language definition or a point about program structure. For example, I use a Shell sort where, in real code, a quicksort would be better. Often, reimplementation with a more suitable algorithm is an exercise. In real code, a call of a library function is typically more appropriate than the code used here for illustration of language features. Textbook examples necessarily give a warped view of software development. By clarifying and simplifying the examples, the complexities that arise from scale disappear. I see no substitute for writing realistically-sized programs for getting an impression of what programming and a program- ming language are really like. This book concentrates on the language features, the basic tech- niques from which every program is composed, and the rules for composition. The selection of examples reflects my background in compilers, foundation libraries, and simu- lations. Examples are simplified versions of what is found in real code. The simplification is nec- essary to keep programming language and design points from getting lost in details. There are no ‘‘cute’’ examples without counterparts in real code. Wherever possible, I relegated to Appendix C language-technical examples of the sort that use variables named x and y types called A and B and y, B, functions called f and g f() g(). In code examples, a proportional-width font is used for identifiers. For example: #i nc lu de io st re am in cl ud e<i os tr ea m> i nt m ai n() in t ma in { s td :c ou t << \"H el lo n ew w or ld \\n st d: co ut He ll o, ne w wo rl d!\\ n\"; } At first glance, this presentation style will seem ‘‘unnatural’’ to programmers accustomed to seeing code in constant-width fonts. However, proportional-width fonts are generally regarded as better than constant-width fonts for presentation of text. Using a proportional-width font also allows me to present code with fewer illogical line breaks. Furthermore, my experiments show that most peo- ple find the new style more readable after a short while. Where possible, the C++ language and library features are presented in the context of their use rather than in the dry manner of a manual. The language features presented and the detail in which they are described reflect my view of what is needed for effective use of C++. A companion, The Annotated C++ Language Standard, authored by Andrew Koenig and myself, is the complete defi- nition of the language together with comments aimed at making it more accessible. Logically, there ought to be another companion, The Annotated C++ Standard Library. However, since both time and my capacity for writing are limited, I cannot promise to produce that. References to parts of this book are of the form §2.3.4 (Chapter 2, section 3, subsection 4), §B.5.6 (Appendix B, subsection 5.6), and §6.6[10] (Chapter 6, exercise 10). Italics are used spar- ingly for emphasis (e.g., ‘‘a string literal is not acceptable’’), for first occurrences of important con- cepts (e.g., polymorphism), for nonterminals of the C++ grammar (e.g., for-statement), and for com- ments in code examples. Semi-bold italics are used to refer to identifiers, keywords, and numeric values from code examples (e.g., c ou nt er c la ss and 1 71 2). co un te r, cl as s, 17 12
    16. 6 Notes to the Reader Chapter 1 1.1.2 Exercises Exercises are found at the ends of chapters. The exercises are mainly of the write-a-program vari- ety. Always write enough code for a solution to be compiled and run with at least a few test cases. The exercises vary considerably in difficulty, so they are marked with an estimate of their diffi- culty. The scale is exponential so that if a (∗1) exercise takes you ten minutes, a (∗2) might take an hour, and a (∗3) might take a day. The time needed to write and test a program depends more on your experience than on the exercise itself. A (∗1) exercise might take a day if you first have to get acquainted with a new computer system in order to run it. On the other hand, a (∗5) exercise might be done in an hour by someone who happens to have the right collection of programs handy. Any book on programming in C can be used as a source of extra exercises for Part I. Any book on data structures and algorithms can be used as a source of exercises for Parts II and III. 1.1.3 Implementation Note The language used in this book is ‘‘pure C++’’ as defined in the C++ standard [C++,1998]. There- fore, the examples ought to run on every C++ implementation. The major program fragments in this book were tried using several C++ implementations. Examples using features only recently adopted into C++ didn’t compile on every implementation. However, I see 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 their implementations correctly accept every C++ feature. See Appendix B for suggestions on how to cope with older C++ compilers and with code written for C compilers. 1.2 Learning C++ The most important thing to do when learning C++ is to focus on concepts and not get lost in language-technical details. The purpose of learning a programming language is to become a better programmer; that is, to become more effective at designing and implementing new systems and at maintaining old ones. For this, an appreciation of programming and design techniques is far more important than an understanding of details; that understanding comes with time and practice. C++ supports a variety of programming styles. All are based on strong static type checking, and most aim at achieving a high level of abstraction and a direct representation of the programmer’s ideas. Each style can achieve its aims effectively while maintaining run-time and space efficiency. A programmer coming from a different language (say C, Fortran, Smalltalk, Lisp, ML, Ada, Eiffel, Pascal, or Modula-2) should realize that to gain the benefits of C++, they must spend time learning and internalizing programming styles and techniques suitable to C++. The same applies to pro- grammers used to an earlier and less expressive version of C++. Thoughtlessly applying techniques effective in one language to another typically leads to awk- ward, poorly performing, and hard-to-maintain code. Such code is also most frustrating to write because every line of code and every compiler error message reminds the programmer that the lan- guage used differs from ‘‘the old language.’’ You can write in the style of Fortran, C, Smalltalk, etc., in any language, but doing so is neither pleasant nor economical in a language with a different philosophy. Every language can be a fertile source of ideas of how to write C++ programs.
    17. Learning C++ Section 1.2 7 However, ideas must be transformed into something that fits with the general structure and type system of C++ in order to be effective in the different context. Over the basic type system of a lan- guage, only Pyrrhic victories are possible. C++ supports a gradual approach to learning. How you approach learning a new programming language depends on what you already know and what you aim to learn. There is no one approach that suits everyone. My assumption is that you are learning C++ to become a better programmer and designer. That is, I assume that your purpose in learning C++ is not simply to learn a new syn- tax for doing things the way you used to, but to learn new and better ways of building systems. This has to be done gradually because acquiring any significant new skill takes time and requires practice. Consider how long it would take to learn a new natural language well or to learn to play a new musical instrument well. Becoming a better system designer is easier and faster, but not as much easier and faster as most people would like it to be. It follows that you will be using C++ – often for building real systems – before understanding every language feature and technique. By supporting several programming paradigms (Chapter 2), C++ supports productive programming at several levels of expertise. Each new style of program- ming adds another tool to your toolbox, but each is effective on its own and each adds to your effectiveness as a programmer. C++ is organized so that you can learn its concepts in a roughly lin- ear order and gain practical benefits along the way. This is important because it allows you to gain benefits roughly in proportion to the effort expended. In the continuing debate on whether one needs to learn C before C++, I am firmly convinced that it is best to go directly to C++. C++ is safer, more expressive, and reduces the need to focus on low-level techniques. It is easier for you to learn the trickier parts of C that are needed to compen- sate for its lack of higher-level facilities after you have been exposed to the common subset of C and C++ and to some of the higher-level techniques supported directly in C++. Appendix B is a guide for programmers going from C++ to C, say, to deal with legacy code. Several independently developed and distributed implementations of C++ exist. A wealth of tools, libraries, and software development environments are also available. A mass of textbooks, manuals, journals, newsletters, electronic bulletin boards, mailing lists, conferences, and courses are available to inform you about the latest developments in C++, its use, tools, libraries, implemen- tations, etc. If you plan to use C++ seriously, I strongly suggest that you gain access to such sources. Each has its own emphasis and bias, so use at least two. For example, see [Barton,1994], [Booch,1994], [Henricson,1997], [Koenig,1997], [Martin,1995]. 1.3 The Design of C++ Simplicity was an important design criterion: where there was a choice between simplifying the language definition and simplifying the compiler, the former was chosen. However, great impor- tance was attached to retaining a high degree of compatibility with C [Koenig,1989] [Strous- trup,1994] (Appendix B); this precluded cleaning up the C syntax. C++ has no built-in high-level data types and no high-level primitive operations. For example, the C++ language does not provide a matrix type with an inversion operator or a string type with a concatenation operator. If a user wants such a type, it can be defined in the language itself. In fact, defining a new general-purpose or application-specific type is the most fundamental programming
    18. 8 Notes to the Reader Chapter 1 activity in C++. A well-designed user-defined type differs from a built-in type only in the way it is defined, not in the way it is used. The C++ standard library described in Part III provides many examples of such types and their uses. From a user’s point of view, there is little difference between a built-in type and a type provided by the standard library. Features that would incur run-time or memory overheads even when not used were avoided in the design of C++. For example, constructs that would make it necessary to store ‘‘housekeeping information’’ in every object were rejected, so if a user declares a structure consisting of two 16-bit quantities, that structure will fit into a 32-bit register. C++ was designed to be used in a traditional compilation and run-time environment, that is, the C programming environment on the UNIX system. Fortunately, C++ was never restricted to UNIX; it simply used UNIX and C as a model for the relationships between language, libraries, compilers, linkers, execution environments, etc. That minimal model helped C++ to be successful on essen- tially every computing platform. There are, however, good reasons for using C++ in environments that provide significantly more support. Facilities such as dynamic loading, incremental compila- tion, and a database of type definitions can be put to good use without affecting the language. C++ type-checking and data-hiding features rely on compile-time analysis of programs to pre- vent accidental corruption of data. They do not provide secrecy or protection against someone who is deliberately breaking the rules. They can, however, be used freely without incurring run-time or space overheads. The idea is that to be useful, a language feature must not only be elegant; it must also be affordable in the context of a real program. For a systematic and detailed description of the design of C++, see [Stroustrup,1994]. 1.3.1 Efficiency and Structure C++ was developed from the C programming language and, with few exceptions, retains C as a subset. The base language, the C subset of C++, is designed to ensure a very close correspondence between its types, operators, and statements and the objects that computers deal with directly: num- bers, characters, and addresses. Except for the n ew d el et e, t yp ei d, d yn am ic _c as t, and t hr ow oper- ne w, de le te ty pe id dy na mi c_ ca st th ro w ators and the try-block, individual C++ expressions and statements need no run-time support. C++ can use the same function call and return sequences as C – or more efficient ones. When even such relatively efficient mechanisms are too expensive, a C++ function can be substituted inline, so that we can enjoy the notational convenience of functions without run-time overhead. One of the original aims for C was to replace assembly coding for the most demanding systems programming tasks. When C++ was designed, care was taken not to compromise the gains in this area. The difference between C and C++ is primarily in the degree of emphasis on types and struc- ture. C is expressive and permissive. C++ is even more expressive. However, to gain that increase in expressiveness, you must pay more attention to the types of objects. Knowing the types of objects, the compiler can deal correctly with expressions when you would otherwise have had to specify operations in painful detail. Knowing the types of objects also enables the compiler to detect errors that would otherwise persist until testing – or even later. Note that using the type sys- tem to check function arguments, to protect data from accidental corruption, to provide new types, to provide new operators, etc., does not increase run-time or space overheads in C++. The emphasis on structure in C++ reflects the increase in the scale of programs written since C was designed. You can make a small program (say, 1,000 lines) work through brute force even
    19. Section 1.3.1 Efficiency and Structure 9 when breaking every rule of good style. For a larger program, this is simply not so. If the structure of a 100,000-line program is bad, you will find that new errors are introduced as fast as old ones are removed. C++ was designed to enable larger programs to be structured in a rational way so that it would be reasonable for a single person to cope with far larger amounts of code. In addition, the aim was to have an average line of C++ code express much more than the average line of C or Pas- cal code. C++ has by now been shown to over-fulfill these goals. Not every piece of code can be well-structured, hardware-independent, easy-to-read, etc. C++ possesses features that are intended for manipulating hardware facilities in a direct and efficient way without regard for safety or ease of comprehension. It also possesses facilities for hiding such code behind elegant and safe interfaces. Naturally, the use of C++ for larger programs leads to the use of C++ by groups of program- mers. C++’s emphasis on modularity, strongly typed interfaces, and flexibility pays off here. C++ has as good a balance of facilities for writing large programs as any language has. However, as programs get larger, the problems associated with their development and maintenance shift from being language problems to more global problems of tools and management. Part IV explores some of these issues. This book emphasizes techniques for providing general-purpose facilities, generally useful types, libraries, etc. These techniques will serve programmers of small programs as well as pro- grammers of large ones. Furthermore, because all nontrivial programs consist of many semi- independent parts, the techniques for writing such parts serve programmers of all applications. You might suspect that specifying a program by using a more detailed type structure would lead to a larger program source text. With C++, this is not so. A C++ program declaring function argu- ment types, using classes, etc., is typically a bit shorter than the equivalent C program not using these facilities. Where libraries are used, a C++ program will appear much shorter than its C equiv- alent, assuming, of course, that a functioning C equivalent could have been built. 1.3.2 Philosophical Note A programming language serves two related purposes: it provides a vehicle for the programmer to specify actions to be executed, and it provides a set of concepts for the programmer to use when thinking about what can be done. The first purpose ideally requires a language that is ‘‘close to the machine’’ so that all important aspects of a machine are handled simply and efficiently in a way that is reasonably obvious to the programmer. The C language was primarily designed with this in mind. 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 C to create C++ were primarily designed with this in mind. The connection between the language in which we think/program and the problems and solu- tions we can imagine is very close. For this reason, restricting language features with the intent of eliminating programmer errors is at best dangerous. As with natural languages, there are great ben- efits from being at least bilingual. A language provides a programmer with a set of conceptual tools; if these are inadequate for a task, they will simply be ignored. Good design and the absence of errors cannot be guaranteed merely by the presence or the absence of specific language features. The type system should be especially helpful for nontrivial tasks. The C++ class concept has, in fact, proven itself to be a powerful conceptual tool.
    20. 10 Notes to the Reader Chapter 1 1.4 Historical Note I invented C++, wrote its early definitions, and produced its first implementation. I chose and for- mulated the design criteria for C++, designed all its major facilities, and was responsible for the processing of extension proposals in the C++ standards committee. Clearly, C++ owes much to C [Kernighan,1978]. Except for closing a few serious loopholes in the type system (see Appendix B), C is retained as a subset. I also retained C’s emphasis on facili- ties that are low-level enough to cope with the most demanding systems programming tasks. C in turn owes much to its predecessor BCPL [Richards,1980]; in fact, BCPL’s // comment convention was (re)introduced in C++. The other main source of inspiration for C++ was Simula67 [Dahl,1970] [Dahl,1972]; the class concept (with derived classes and virtual functions) was bor- rowed from it. C++’s facility for overloading operators and the freedom to place a declaration wherever a statement can occur resembles Algol68 [Woodward,1974]. Since the original edition of this book, the language has been extensively reviewed and refined. The major areas for revision were overload resolution, linking, and memory management facilities. In addition, several minor changes were made to increase C compatibility. Several generalizations and a few major extensions were added: these included multiple inheritance, s ta ti c member func- st at ic tions, c on st member functions, p ro te ct ed members, templates, exception handling, run-time type co ns t pr ot ec te d identification, and namespaces. The overall theme of these extensions and revisions was to make C++ a better language for writing and using libraries. The evolution of C++ is described in [Strous- trup,1994]. The template facility was primarily designed to support statically typed containers (such as lists, vectors, and maps) and to support elegant and efficient use of such containers (generic program- ming). A key aim was to reduce the use of macros and casts (explicit type conversion). Templates were partly inspired by Ada’s generics (both their strengths and their weaknesses) and partly by Clu’s parameterized modules. Similarly, the C++ exception-handling mechanism was inspired .. partly by Ada [Ichbiah,1979], Clu [Liskov,1979], and ML [Wikstrom,1987]. Other developments in the 1985 to 1995 time span – such as multiple inheritance, pure virtual functions, and name- spaces – were primarily generalizations driven by experience with the use of C++ rather than ideas imported from other languages. Earlier versions of the language, collectively known as ‘‘C with Classes’’ [Stroustrup,1994], have been in use since 1980. The language was originally invented because I wanted to write some event-driven simulations for which Simula67 would have been ideal, except for efficiency consid- erations. ‘‘C with Classes’’ was used for major projects in which the facilities for writing programs that use minimal time and space were severely tested. It lacked operator overloading, references, virtual functions, templates, exceptions, and many details. The first use of C++ outside a research organization started in July 1983. The name C++ (pronounced ‘‘see plus plus’’) was coined by Rick Mascitti in the summer of 1983. The name signifies the evolutionary nature of the changes from C; ‘‘++’’ is the C increment operator. The slightly shorter name ‘‘C+’’ is a syntax error; it has also been used as the name of an unrelated language. Connoisseurs of C semantics find C++ inferior to ++C. The language is not called D, because it is an extension of C, and it does not attempt to remedy problems by removing features. For yet another interpretation of the name C++, see the appendix of [Orwell,1949]. C++ was designed primarily so that my friends and I would not have to program in assembler,
    21. Section 1.4 Historical Note 11 C, or various modern high-level languages. Its main purpose was to make writing good programs easier and more pleasant for the individual programmer. In the early years, there was no C++ paper design; design, documentation, and implementation went on simultaneously. There was no ‘‘C++ project’’ either, or a ‘‘C++ design committee.’’ Throughout, C++ evolved to cope with problems encountered by users and as a result of discussions between my friends, my colleagues, and me. Later, the explosive growth of C++ use caused some changes. Sometime during 1987, it became clear that formal standardization of C++ was inevitable and that we needed to start prepar- ing the ground for a standardization effort [Stroustrup,1994]. The result was a conscious effort to maintain contact between implementers of C++ compilers and major users through paper and elec- tronic mail and through face-to-face meetings at C++ conferences and elsewhere. AT&T Bell Laboratories made a major contribution to this by allowing me to share drafts of revised versions of the C++ reference manual with implementers and users. Because many of these people work for companies that could be seen as competing with AT&T, the significance of this contribution should not be underestimated. A less enlightened company could have caused major problems of language fragmentation simply by doing nothing. As it happened, about a hundred individuals from dozens of organizations read and commented on what became the generally accepted reference manual and the base document for the ANSI C++ standardization effort. Their names can be found in The Annotated C++ Reference Manual [Ellis,1989]. Finally, the X3J16 committee of ANSI was convened in December 1989 at the initiative of Hewlett-Packard. In June 1991, this ANSI (American national) standardization of C++ became part of an ISO (international) standardization effort for C++. From 1990, these joint C++ standards committees have been the main forum for the evolution of C++ and the refinement of its definition. I served on these commit- tees throughout. In particular, as the chairman of the working group for extensions, I was directly responsible for the handling of proposals for major changes to C++ and the addition of new lan- guage features. An initial draft standard for public review was produced in April 1995. The ISO C++ standard (ISO/IEC 14882) was ratified in 1998. C++ evolved hand-in-hand with some of the key classes presented in this book. For example, I designed complex, vector, and stack classes together with the operator overloading mechanisms. String and list classes were developed by Jonathan Shopiro and me as part of the same effort. Jonathan’s string and list classes were the first to see extensive use as part of a library. The string class from the standard C++ library has its roots in these early efforts. The task library described in [Stroustrup,1987] and in §12.7[11] was part of the first ‘‘C with Classes’’ program ever written. I wrote it and its associated classes to support Simula-style simulations. The task library has been revised and reimplemented, notably by Jonathan Shopiro, and is still in extensive use. The stream library as described in the first edition of this book was designed and implemented by me. Jerry Schwarz transformed it into the iostreams library (Chapter 21) using Andrew Koenig’s manipulator technique (§21.4.6) and other ideas. The iostreams library was further refined during standardiza- tion, when the bulk of the work was done by Jerry Schwarz, Nathan Myers, and Norihiro Kumagai. The development of the template facility was influenced by the v ec to r, m ap l is t, and s or t tem- ve ct or ma p, li st so rt plates devised by Andrew Koenig, Alex Stepanov, me, and others. In turn, Alex Stepanov’s work on generic programming using templates led to the containers and algorithms parts of the standard C++ library (§16.3, Chapter 17, Chapter 18, §19.2). The v al ar ra y library for numerical computa- va la rr ay tion (Chapter 22) is primarily the work of Kent Budge.
    22. 12 Notes to the Reader Chapter 1 1.5 Use of C++ C++ is used by hundreds of thousands of programmers in essentially every application domain. This use is supported by about a dozen independent implementations, hundreds of libraries, hun- dreds of textbooks, several technical journals, many conferences, and innumerable consultants. Training and education at a variety of levels are widely available. Early applications tended to have a strong systems programming flavor. For example, several major operating systems have been written in C++ [Campbell,1987] [Rozier,1988] [Hamilton,1993] [Berg,1995] [Parrington,1995] and many more have key parts done in C++. I considered uncom- promising low-level efficiency essential for C++. This allows us to use C++ to write device drivers and other software that rely on direct manipulation of hardware under real-time constraints. In such code, predictability of performance is at least as important as raw speed. Often, so is compactness of the resulting system. C++ was designed so that every language feature is usable in code under severe time and space constraints [Stroustrup,1994,§4.5]. Most applications have sections of code that are critical for acceptable performance. However, the largest amount of code is not in such sections. For most code, maintainability, ease of exten- sion, and ease of testing is key. C++’s support for these concerns has led to its widespread use where reliability is a must and in areas where requirements change significantly over time. Exam- ples are banking, trading, insurance, telecommunications, and military applications. For years, the central control of the U.S. long-distance telephone system has relied on C++ and every 800 call (that is, a call paid for by the called party) has been routed by a C++ program [Kamath,1993]. Many such applications are large and long-lived. As a result, stability, compatibility, and scalabil- ity have been constant concerns in the development of C++. Million-line C++ programs are not uncommon. Like C, C++ wasn’t specifically designed with numerical computation in mind. However, much numerical, scientific, and engineering computation is done in C++. A major reason for this is that traditional numerical work must often be combined with graphics and with computations relying on data structures that don’t fit into the traditional Fortran mold [Budge,1992] [Barton,1994]. Graph- ics and user interfaces are areas in which C++ is heavily used. Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user inter- faces of these systems are C++ programs. In addition, some of the most popular libraries support- ing X for UNIX are written in C++. Thus, C++ is a common choice for the vast number of applica- tions in which the user interface is a major part. All of this points to what may be C++’s greatest strength: its ability to be used effectively for applications that require work in a variety of application areas. It is quite common to find an appli- cation that involves local and wide-area networking, numerics, graphics, user interaction, and data- base access. Traditionally, such application areas have been considered distinct, and they have most often been served by distinct technical communities using a variety of programming lan- guages. However, C++ has been widely used in all of those areas. Furthermore, it is able to coexist with code fragments and programs written in other languages. C++ is widely used for teaching and research. This has surprised some who – correctly – point out that C++ isn’t the smallest or cleanest language ever designed. It is, however – clean enough for successful teaching of basic concepts, – realistic, efficient, and flexible enough for demanding projects,
    23. Use of C++ Section 1.5 13 – available enough for organizations and collaborations relying on diverse development and execution environments, – comprehensive enough to be a vehicle for teaching advanced concepts and techniques, and – commercial enough to be a vehicle for putting what is learned into non-academic use. C++ is a language that you can grow with. 1.6 C and C++ C was chosen as the base language for C++ because it [1] is versatile, terse, and relatively low-level; [2] is adequate for most systems programming tasks; [3] runs everywhere and on everything; and [4] fits into the UNIX programming environment. C has its problems, but a language designed from scratch would have some too, and we know C’s problems. Importantly, working with C enabled ‘‘C with Classes’’ to be a useful (if awkward) tool within months of the first thought of adding Simula-like classes to C. As C++ became more widely used, and as the facilities it provided over and above those of C became more significant, the question of whether to retain compatibility was raised again and again. Clearly some problems could be avoided if some of the C heritage was rejected (see, e.g., [Sethi,1981]). This was not done because [1] there are millions of lines of C code that might benefit from C++, provided that a complete rewrite from C to C++ were unnecessary; [2] there are millions of lines of library functions and utility software code written in C that could be used from/on C++ programs provided C++ were link-compatible with and syntacti- cally very similar to C; [3] there are hundreds of thousands of programmers who know C and therefore need only learn to use the new features of C++ and not relearn the basics; and [4] C++ and C will be used on the same systems by the same people for years, so the differ- ences should be either very large or very small so as to minimize mistakes and confusion. The definition of C++ has been revised to ensure that a construct that is both legal C and legal C++ has the same meaning in both languages (with a few minor exceptions; see §B.2). The C language has itself evolved, partly under the influence of the development of C++ [Rosler,1984]. The ANSI C standard [C,1990] contains a function declaration syntax borrowed from ‘‘C with Classes.’’ Borrowing works both ways. For example, the v oi d* pointer type was vo id invented for ANSI C and first implemented in C++. As promised in the first edition of this book, the definition of C++ has been reviewed to remove gratuitous incompatibilities; C++ is now more compatible with C than it was originally. The ideal was for C++ to be as close to ANSI C as possi- ble – but no closer [Koenig,1989]. One hundred percent compatibility was never a goal because that would compromise type safety and the smooth integration of user-defined and built-in types. Knowing C is not a prerequisite for learning C++. Programming in C encourages many tech- niques and tricks that are rendered unnecessary by C++ language features. For example, explicit type conversion (casting) is less frequently needed in C++ than it is in C (§1.6.1). However, good C programs tend to be C++ programs. For example, every program in Kernighan and Ritchie, The
    24. 14 Notes to the Reader Chapter 1 C Programming Language (2nd Edition) [Kernighan,1988], is a C++ program. Experience with any statically typed language will be a help when learning C++. 1.6.1 Suggestions for C Programmers The better one knows C, the harder it seems to be to avoid writing C++ in C style, thereby losing some of the potential benefits of C++. Please take a look at Appendix B, which describes the dif- ferences between C and C++. Here are a few pointers to the areas in which C++ has better ways of doing something than C has: [1] Macros are almost never necessary in C++. Use c on st (§5.4) or e nu m (§4.8) to define mani- co ns t en um fest constants, i nl in e (§7.1.1) to avoid function-calling overhead, t em pl at es (Chapter 13) to in li ne te mp la te specify families of functions and types, and n am es pa ce (§8.2) to avoid name clashes. na me sp ac es [2] Don’t declare a variable before you need it so that you can initialize it immediately. A declaration can occur anywhere a statement can (§6.3.1), in for-statement initializers (§6.3.3), and in conditions (§6.3.2.1). [3] Don’t use m al lo c(). The n ew operator (§6.2.6) does the same job better, and instead of ma ll oc ne w r ea ll oc re al lo c(), try a v ec to r (§3.8). ve ct or [4] Try to avoid v oi d*, pointer arithmetic, unions, and casts, except deep within the implemen- vo id tation of some function or class. In most cases, a cast is an indication of a design error. If you must use an explicit type conversion, try using one of the ‘‘new casts’’ (§6.2.7) for a more precise statement of what you are trying to do. [5] Minimize the use of arrays and C-style strings. The C++ standard library s tr in g (§3.5) and st ri ng v ec to r (§3.7.1) classes can often be used to simplify programming compared to traditional C ve ct or style. In general, try not to build yourself what has already been provided by the standard library. To obey C linkage conventions, a C++ function must be declared to have C linkage (§9.2.4). Most important, try thinking of a program as a set of interacting concepts represented as classes and objects, instead of as a bunch of data structures with functions twiddling their bits. 1.6.2 Suggestions for C++ Programmers By now, many people have been using C++ for a decade. Many more are using C++ in a single environment and have learned to live with the restrictions imposed by early compilers and first- generation libraries. Often, what an experienced C++ programmer has failed to notice over the years is not the introduction of new features as such, but rather the changes in relationships between features that make fundamental new programming techniques feasible. In other words, what you didn’t think of when first learning C++ or found impractical just might be a superior approach today. You find out only by re-examining the basics. Read through the chapters in order. If you already know the contents of a chapter, you can be through in minutes. If you don’t already know the contents, you’ll have learned something unex- pected. I learned a fair bit writing this book, and I suspect that hardly any C++ programmer knows every feature and technique presented. Furthermore, to use the language well, you need a perspec- tive that brings order to the set of features and techniques. Through its organization and examples, this book offers such a perspective.
    25. Thinking about Programming in C++ Section 1.7 15 1.7 Thinking about Programming in C++ Ideally, you approach the task of designing a program in three stages. First, you gain a clear under- standing of the problem (analysis), then you identify the key concepts involved in a solution (design), and finally you express that solution in a program (programming). However, the details of the problem and the concepts of the solution often become clearly understood only through the effort to express them in a program and trying to get it to run acceptably. This is where the choice of programming language matters. In most applications, there are concepts that are not easily represented as one of the fundamental types or as a function without associated data. Given such a concept, declare a class to represent it in the program. A C++ class is a type. That is, it specifies how objects of its class behave: how they are created, how they can be manipulated, and how they are destroyed. A class may also specify how objects are represented, although in the early stages of the design of a program that should not be the major concern. The key to writing good programs is to design classes so that each cleanly represents a single concept. Often, this means that you must focus on questions such as: How are objects of this class created? Can objects of this class be copied and/or destroyed? What opera- tions can be applied to such objects? If there are no good answers to such questions, the concept probably wasn’t ‘‘clean’’ in the first place. It might then be a good idea to think more about the problem and its proposed solution instead of immediately starting to ‘‘code around’’ the problems. The concepts that are easiest to deal with are the ones that have a traditional mathematical for- malism: numbers of all sorts, sets, geometric shapes, etc. Text-oriented I/O, strings, basic contain- ers, the fundamental algorithms on such containers, and some mathematical classes are part of the standard C++ library (Chapter 3, §16.1.2). In addition, a bewildering variety of libraries supporting general and domain-specific concepts are available. A concept does not exist in a vacuum; there are always clusters of related concepts. Organizing the relationship between classes in a program – that is, determining the exact relationship between the different concepts involved in a solution – is often harder than laying out the individual classes in the first place. The result had better not be a muddle in which every class (concept) depends on every other. Consider two classes, A and B. Relationships such as ‘‘A calls functions from B,’’ ‘‘A creates Bs,’’ and ‘‘A has a B member’’ seldom cause major problems, while relationships such as ‘‘A uses data from B’’ can typically be eliminated. One of the most powerful intellectual tools for managing complexity is hierarchical ordering, that is, organizing related concepts into a tree structure with the most general concept as the root. In C++, derived classes represent such structures. A program can often be organized as a set of trees or directed acyclic graphs of classes. That is, the programmer specifies a number of base classes, each with its own set of derived classes. Virtual functions (§2.5.5, §12.2.6) can often be used to define operations for the most general version of a concept (a base class). When necessary, the interpretation of these operations can be refined for particular special cases (derived classes). Sometimes even a directed acyclic graph seems insufficient for organizing the concepts of a program; some concepts seem to be inherently mutually dependent. In that case, we try to localize cyclic dependencies so that they do not affect the overall structure of the program. If you cannot eliminate or localize such mutual dependencies, then you are most likely in a predicament that no programming language can help you out of. Unless you can conceive of some easily stated rela- tionships between the basic concepts, the program is likely to become unmanageable.
    26. 16 Notes to the Reader Chapter 1 One of the best tools for untangling dependency graphs is the clean separation of interface and implementation. Abstract classes (§2.5.4, §12.3) are C++’s primary tool for doing that. Another form of commonality can be expressed through templates (§2.7, Chapter 13). A class template specifies a family of classes. For example, a list template specifies ‘‘list of T,’’ where ‘‘T’’ can be any type. Thus, a template is a mechanism for specifying how one type is generated given another type as an argument. The most common templates are container classes such as lists, vectors, and associative arrays (maps) and the fundamental algorithms using such containers. It is usually a mistake to express parameterization of a class and its associated functions with a type using inheritance. It is best done using templates. Remember that much programming can be simply and clearly done using only primitive types, data structures, plain functions, and a few library classes. The whole apparatus involved in defin- ing new types should not be used except when there is a real need. The question ‘‘How does one write good programs in C++?’’ is very similar to the question ‘‘How does one write good English prose?’’ There are two answers: ‘‘Know what you want to say’’ and ‘‘Practice. Imitate good writing.’’ Both appear to be as appropriate for C++ as they are for English – and as hard to follow. 1.8 Advice Here is a set of ‘‘rules’’ you might consider while learning C++. As you get more proficient you can evolve them into something suitable for your kind of applications and your style of program- ming. They are deliberately very simple, so they lack detail. Don’t take them too literally. To write a good program takes intelligence, taste, and patience. You are not going to get it right the first time. Experiment! [1] When you program, you create a concrete representation of the ideas in your solution to some problem. Let the structure of the program reflect those ideas as directly as possible: [a] If you can think of ‘‘it’’ as a separate idea, make it a class. [b] If you can think of ‘‘it’’ as a separate entity, make it an object of some class. [c] If two classes have a common interface, make that interface an abstract class. [d] If the implementations of two classes have something significant in common, make that commonality a base class. [e] If a class is a container of objects, make it a template. [f] If a function implements an algorithm for a container, make it a template function imple- menting the algorithm for a family of containers. [g] If a set of classes, templates, etc., are logically related, place them in a common namespace. [2] When you define either a class that does not implement either a mathematical entity like a matrix or a complex number or a low-level type such as a linked list: [a] Don’t use global data (use members). [b] Don’t use global functions. [c] Don’t use public data members. [d] Don’t use friends, except to avoid [a] or [c]. [e] Don’t put a ‘‘type field’’ in a class; use virtual functions. [f] Don’t use inline functions, except as a significant optimization.
    27. Section 1.8 Advice 17 More specific or detailed rules of thumb can be found in the ‘‘Advice’’ section of each chapter. Remember, this advice is only rough rules of thumb, not immutable laws. A piece of advice should be applied only ‘‘where reasonable.’’ There is no substitute for intelligence, experience, common sense, and good taste. I find rules of the form ‘‘never do this’’ unhelpful. Consequently, most advice is phrased as suggestions of what to do, while negative suggestions tend not to be phrased as absolute prohibi- tions. I know of no major feature of C++ that I have not seen put to good use. The ‘‘Advice’’ sec- tions do not contain explanations. Instead, each piece of advice is accompanied by a reference to the appropriate section of the book. Where negative advice is given, that section usually provides a suggested alternative. 1.8.1 References There are few direct references in the text, but here is a short list of books and papers that are men- tioned directly or indirectly. John J. Barton and Lee R. Nackman: Scientific and Engineering C++. [Barton,1994] Addison-Wesley. Reading, Mass. 1994. ISBN 0-201-53393-6. [Berg,1995] William Berg, Marshall Cline, and Mike Girou: Lessons Learned from the OS/400 OO Project. CACM. Vol. 38 No. 10. October 1995. [Booch,1994] Grady Booch: Object-Oriented Analysis and Design. Benjamin/Cummings. Menlo Park, Calif. 1994. ISBN 0-8053-5340-2. [Budge,1992] Kent Budge, J. S. Perry, and A. C. Robinson: High-Performance Scientific Computation using C++. Proc. USENIX C++ Conference. Portland, Oregon. August 1992. [C,1990] X3 Secretariat: Standard – The C Language. X3J11/90-013. ISO Standard ISO/IEC 9899. Computer and Business Equipment Manufacturers Association. Washington, DC, USA. [C++,1998] X3 Secretariat: International Standard – The C++ Language. X3J16-14882. Information Technology Council (NSITC). Washington, DC, USA. [Campbell,1987] Roy Campbell, et al.: The Design of a Multiprocessor Operating System. Proc. USENIX C++ Conference. Santa Fe, New Mexico. November 1987. [Coplien,1995] James O. Coplien and Douglas C. Schmidt (editors): Pattern Languages of Program Design. Addison-Wesley. Reading, Mass. 1995. ISBN 0-201- 60734-4. [Dahl,1970] O-J. Dahl, B. Myrhaug, and K. Nygaard: SIMULA Common Base Language. Norwegian Computing Center S-22. Oslo, Norway. 1970. [Dahl,1972] O-J. Dahl and C. A. R. Hoare: Hierarchical Program Construction in Struc- tured Programming. Academic Press, New York. 1972. Margaret A. Ellis and Bjarne Stroustrup: The Annotated C++ Reference Man- [Ellis,1989] ual. Addison-Wesley. Reading, Mass. 1990. ISBN 0-201-51459-1. [Gamma,1995] Erich Gamma, et al.: Design Patterns. Addison-Wesley. Reading, Mass. 1995. ISBN 0-201-63361-2. [Goldberg,1983] A. Goldberg and D. Robson: SMALLTALK-80 – The Language and Its Imple- mentation. Addison-Wesley. Reading, Mass. 1983.
    28. 18 Notes to the Reader Chapter 1 [Griswold,1970] R. E. Griswold, et al.: The Snobol4 Programming Language. Prentice-Hall. Englewood Cliffs, New Jersey. 1970. [Griswold,1983] R. E. Griswold and M. T. Griswold: The ICON Programming Language. Prentice-Hall. Englewood Cliffs, New Jersey. 1983. [Hamilton,1993] G. Hamilton and P. Kougiouris: The Spring Nucleus: A Microkernel for Objects. Proc. 1993 Summer USENIX Conference. USENIX. [Henricson,1997] Mats Henricson and Erik Nyquist: Industrial Strength C++: Rules and Recom- mendations. Prentice-Hall. Englewood Cliffs, New Jersey. 1997. ISBN 0- 13-120965-5. [Ichbiah,1979] Jean D. Ichbiah, et al.: Rationale for the Design of the ADA Programming Lan- guage. SIGPLAN Notices. Vol. 14 No. 6. June 1979. [Kamath,1993] Yogeesh H. Kamath, Ruth E. Smilan, and Jean G. Smith: Reaping Benefits with Object-Oriented Technology. AT&T Technical Journal. Vol. 72 No. 5. September/October 1993. [Kernighan,1978] Brian W. Kernighan and Dennis M. Ritchie: The C Programming Language. Prentice-Hall. Englewood Cliffs, New Jersey. 1978. [Kernighan,1988] Brian W. Kernighan and Dennis M. Ritchie: The C Programming Language (Second Edition). Prentice-Hall. Englewood Cliffs, New Jersey. 1988. ISBN 0-13-110362-8. Andrew Koenig and Bjarne Stroustrup: C++: As close to C as possible – but no [Koenig,1989] closer. The C++ Report. Vol. 1 No. 7. July 1989. Andrew Koenig and Barbara Moo: Ruminations on C++. Addison Wesley [Koenig,1997] Longman. Reading, Mass. 1997. ISBN 0-201-42339-1. [Knuth,1968] Donald Knuth: The Art of Computer Programming. Addison-Wesley. Read- ing, Mass. [Liskov,1979] Barbara Liskov et al.: Clu Reference Manual. MIT/LCS/TR-225. MIT Cam- bridge. Mass. 1979. Robert C. Martin: Designing Object-Oriented C++ Applications Using the [Martin,1995] Booch Method. Prentice-Hall. Englewood Cliffs, New Jersey. 1995. ISBN 0-13-203837-4. [Orwell,1949] George Orwell: 1984. Secker and Warburg. London. 1949. [Parrington,1995] Graham Parrington et al.: The Design and Implementation of Arjuna. Com- puter Systems. Vol. 8 No. 3. Summer 1995. [Richards,1980] Martin Richards and Colin Whitby-Strevens: BCPL – The Language and Its Compiler. Cambridge University Press, Cambridge. England. 1980. ISBN 0-521-21965-5. [Rosler,1984] L. Rosler: The Evolution of C – Past and Future. AT&T Bell Laboratories Technical Journal. Vol. 63 No. 8. Part 2. October 1984. [Rozier,1988] M. Rozier, et al.: CHORUS Distributed Operating Systems. Computing Sys- tems. Vol. 1 No. 4. Fall 1988. [Sethi,1981] Ravi Sethi: Uniform Syntax for Type Expressions and Declarations. Software Practice & Experience. Vol. 11. 1981. [Stepanov,1994] Alexander Stepanov and Meng Lee: The Standard Template Library. HP Labs Technical Report HPL-94-34 (R. 1). August, 1994.
    29. Section 1.8.1 References 19 [Stroustrup,1986] Bjarne Stroustrup: The C++ Programming Language. Addison-Wesley. Reading, Mass. 1986. ISBN 0-201-12078-X. [Stroustrup,1987] Bjarne Stroustrup and Jonathan Shopiro: A Set of C Classes for Co-Routine Style Programming. Proc. USENIX C++ Conference. Santa Fe, New Mexico. November 1987. [Stroustrup,1991] Bjarne Stroustrup: The C++ Programming Language (Second Edition). Addison-Wesley. Reading, Mass. 1991. ISBN 0-201-53992-6. [Stroustrup,1994] Bjarne Stroustrup: The Design and Evolution of C++. Addison-Wesley. Read- ing, Mass. 1994. ISBN 0-201-54330-3. [Tarjan,1983] Robert E. Tarjan: Data Structures and Network Algorithms. Society for Indus- trial and Applied Mathematics. Philadelphia, Penn. 1983. ISBN 0-898- 71187-8. [Unicode,1996] The Unicode Consortium: The Unicode Standard, Version 2.0. Addison- Wesley Developers Press. Reading, Mass. 1996. ISBN 0-201-48345-9. [UNIX,1985] UNIX Time-Sharing System: Programmer’s Manual. Research Version, Tenth Edition. AT&T Bell Laboratories, Murray Hill, New Jersey. February 1985. Gregory V. Wilson and Paul Lu (editors): Parallel Programming Using C++. [Wilson,1996] The MIT Press. Cambridge. Mass. 1996. ISBN 0-262-73118-5. .. .. ˚ [Wikstrom,1987] Ake Wikstrom: Functional Programming Using ML. Prentice-Hall. Engle- wood Cliffs, New Jersey. 1987. [Woodward,1974] P. M. Woodward and S. G. Bond: Algol 68-R Users Guide. Her Majesty’s Sta- tionery Office. London. England. 1974. References to books relating to design and larger software development issues can be found at the end of Chapter 23.
    30. 20 Notes to the Reader Chapter 1
    31. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 2 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ A Tour of C++ The first thing we do, let´s kill all the language lawyers. – Henry VI, part II What is C++? — programming paradigms — procedural programming — modularity — separate compilation — exception handling — data abstraction — user-defined types — concrete types — abstract types — virtual functions — object-oriented programming — generic programming — containers — algorithms — language and programming — advice. 2.1 What is C++? [tour.intro] C++ is a general-purpose programming language with a bias towards systems programming that – is a better C, – supports data abstraction, – supports object-oriented programming, and – supports generic programming. This chapter explains what this means without going into the finer details of the language defini- tion. Its purpose is to give you a general overview of C++ and the key techniques for using it, not to provide you with the detailed information necessary to start programming in C++. If you find some parts of this chapter rough going, just ignore those parts and plow on. All will be explained in detail in later chapters. However, if you do skip part of this chapter, do yourself a favor by returning to it later. Detailed understanding of language features – even of all features of a language – cannot com- pensate for lack of an overall view of the language and the fundamental techniques for using it.
    32. A Tour of C++ 22 Chapter 2 2.2 Programming Paradigms [tour.paradigm] Object-oriented programming is a technique for programming – a paradigm for writing ‘‘good’’ programs for a set of problems. If the term ‘‘object-oriented programming language’’ means any- thing, it must mean a programming language that provides mechanisms that support the object- oriented style of programming well. There is an important distinction here. A language is said to support a style of programming if it provides facilities that make it convenient (reasonably easy, safe, and efficient) to use that style. A language does not support a technique if it takes exceptional effort or skill to write such pro- grams; it merely enables the technique to be used. For example, you can write structured programs in Fortran77 and object-oriented programs in C, but it is unnecessarily hard to do so because these languages do not directly support those techniques. Support for a paradigm comes not only in the obvious form of language facilities that allow direct use of the paradigm, but also in the more subtle form of compile-time and/or run-time checks against unintentional deviation from the paradigm. Type checking is the most obvious example of this; ambiguity detection and run-time checks are also used to extend linguistic support for para- digms. Extra-linguistic facilities such as libraries and programming environments can provide fur- ther support for paradigms. One language is not necessarily better than another because it possesses a feature the other does not. There are many examples to the contrary. The important issue is not so much what features a language possesses, but that the features it does possess are sufficient to support the desired pro- gramming styles in the desired application areas: [1] All features must be cleanly and elegantly integrated into the language. [2] It must be possible to use features in combination to achieve solutions that would otherwise require extra, separate features. [3] There should be as few spurious and ‘‘special-purpose’’ features as possible. [4] A feature’s implementation should not impose significant overheads on programs that do not require it. [5] A user should need to know only about the subset of the language explicitly used to write a program. The first principle is an appeal to aesthetics and logic. The next two are expressions of the ideal of minimalism. The last two can be summarized as ‘‘what you don’t know won’t hurt you.’’ C++ was designed to support data abstraction, object-oriented programming, and generic pro- gramming in addition to traditional C programming techniques under these constraints. It was not meant to force one particular programming style upon all users. The following sections consider some programming styles and the key language mechanisms supporting them. The presentation progresses through a series of techniques starting with procedu- ral programming and leading up to the use of class hierarchies in object-oriented programming and generic programming using templates. Each paradigm builds on its predecessors, each adds some- thing new to the C++ programmer’s toolbox, and each reflects a proven design approach. The presentation of language features is not exhaustive. The emphasis is on design approaches and ways of organizing programs rather than on language details. At this stage, it is far more important to gain an idea of what can be done using C++ than to understand exactly how it can be achieved.
    33. Section 2.3 Procedural Programming 23 2.3 Procedural Programming [tour.proc] The original programming paradigm is: Decide which procedures you want; use the best algorithms you can find. The focus is on the processing – the algorithm needed to perform the desired computation. Lan- guages support this paradigm by providing facilities for passing arguments to functions and return- ing values from functions. The literature related to this way of thinking is filled with discussion of ways to pass arguments, ways to distinguish different kinds of arguments, different kinds of func- tions (e.g., procedures, routines, and macros), etc. A typical example of ‘‘good style’’ is a square-root function. Given a double-precision floating-point argument, it produces a result. To do this, it performs a well-understood mathemati- cal computation: d ou bl e s qr t(d ou bl e a rg do ub le sq rt do ub le ar g) { // code for calculating a square root } v oi d f vo id f() { d ou bl e r oo t2 = s qr t(2 ; do ub le ro ot 2 sq rt 2) // ... } Curly braces, { }, express grouping in C++. Here, they indicate the start and end of the function bodies. The double slash, //, begins a comment that extends to the end of the line. The keyword v oi d indicates that a function does not return a value. vo id From the point of view of program organization, functions are used to create order in a maze of algorithms. The algorithms themselves are written using function calls and other language facili- ties. The following subsections present a thumb-nail sketch of C++’s most basic facilities for expressing computation. 2.3.1 Variables and Arithmetic [tour.var] Every name and every expression has a type that determines the operations that may be performed on it. For example, the declaration i nt i nc h; in t in ch specifies that i nc h is of type i nt that is, i nc h is an integer variable. in ch in t; in ch A declaration is a statement that introduces a name into the program. It specifies a type for that name. A type defines the proper use of a name or an expression. C++ offers a variety of fundamental types, which correspond directly to hardware facilities. For example:
    34. A Tour of C++ 24 Chapter 2 b oo l bo ol // Boolean, possible values are true and false c ha r ch ar // character, for example, ’a’, ’z’, and ’9’ i nt in t // integer, for example, 1, 42, and 1216 d ou bl e do ub le // double-precision floating-point number, for example, 3.14 and 299793.0 A c ha r variable is of the natural size to hold a character on a given machine (typically a byte), and ch ar an i nt variable is of the natural size for integer arithmetic on a given machine (typically a word). in t The arithmetic operators can be used for any combination of these types: // plus, both unary and binary + // minus, both unary and binary - // multiply * // divide / // remainder % So can the comparison operators: // equal == // not equal != // less than < // greater than > // less than or equal <= // greater than or equal >= In assignments and in arithmetic operations, C++ performs all meaningful conversions between the basic types so that they can be mixed freely: vo id so me _f un ct io n() v oi d s om e_ fu nc ti on // function that doesn’t return a value { d ou bl e d = 2 2; do ub le 2.2 // initialize floating-point number i nt i = 7 in t 7; // initialize integer d = d i; d+i // assign sum to d i = d i; d*i // assign product to i } As in C, = is the assignment operator and == tests equality. 2.3.2 Tests and Loops [tour.loop] C++ provides a conventional set of statements for expressing selection and looping. For example, here is a simple function that prompts the user and returns a Boolean indicating the response: b oo l a cc ep t() bo ol ac ce pt { c ou t << \"D o y ou w an t t o p ro ce ed (y o r n \\n co ut Do yo u wa nt to pr oc ee d y or n)?\\ n\"; // write question c ha r a ns we r = 0 ch ar an sw er 0; c in >> a ns we r; ci n an sw er // read answer i f (a ns we r == ´y if an sw er y´) r et ur n t ru e; re tu rn tr ue r et ur n f al se re tu rn fa ls e; }
    35. Section 2.3.2 Tests and Loops 25 The << operator (‘‘put to’’) is used as an output operator; c ou t is the standard output stream. The co ut >> operator (‘‘get from’’) is used as an input operator; c in is the standard input stream. The type of ci n the right-hand operand of >> determines what input is accepted and is the target of the input opera- tion. The \\ n character at the end of the output string represents a newline. \\n The example could be slightly improved by taking an ‘n’ answer into account: b oo l a cc ep t2 bo ol ac ce pt 2() { c ou t << \"D o y ou w an t t o p ro ce ed (y o r n \\n co ut Do yo u wa nt to pr oc ee d y or n)?\\ n\"; // write question c ha r a ns we r = 0 ch ar an sw er 0; c in >> a ns we r; ci n an sw er // read answer s wi tc h (a ns we r) { sw it ch an sw er c as e ´y ca se y´: r et ur n t ru e; re tu rn tr ue c as e ´n ca se n´: r et ur n f al se re tu rn fa ls e; d ef au lt de fa ul t: c ou t << \"I ll t ak e t ha t f or a n o.\\ n\"; co ut I´l l ta ke th at fo r no \\n r et ur n f al se re tu rn fa ls e; } } A switch-statement tests a value against a set of constants. The case constants must be distinct, and if the value tested does not match any of them, the d ef au lt is chosen. The programmer need not de fa ul t provide a d ef au lt de fa ul t. Few programs are written without loops. In this case, we might like to give the user a few tries: b oo l a cc ep t3 bo ol ac ce pt 3() { i nt t ri es = 1 in t tr ie s 1; w hi le (t ri es < 4 { wh il e tr ie s 4) c ou t << \"D o y ou w an t t o p ro ce ed (y o r n \\n co ut Do yo u wa nt to pr oc ee d y or n)?\\ n\"; // write question c ha r a ns we r = 0 ch ar an sw er 0; c in >> a ns we r; ci n an sw er // read answer s wi tc h (a ns we r) { sw it ch an sw er c as e ´y ca se y´: r et ur n t ru e; re tu rn tr ue c as e ´n ca se n´: r et ur n f al se re tu rn fa ls e; d ef au lt de fa ul t: c ou t << \"S or ry I d on t u nd er st an d t ha t.\\ n\"; co ut So rr y, do n´t un de rs ta nd th at \\n t ri es = t ri es + 1 tr ie s tr ie s 1; } } c ou t << \"I ll t ak e t ha t f or a n o.\\ n\"; co ut I´l l ta ke th at fo r no \\n r et ur n f al se re tu rn fa ls e; } The while-statement executes until its condition becomes f al se fa ls e.
    36. A Tour of C++ 26 Chapter 2 2.3.3 Pointers and Arrays [tour.ptr] An array can be declared like this: c ha r v 10 ; ch ar v[1 0] // array of 10 characters Similarly, a pointer can be declared like this: c ha r* p // pointer to character ch ar p; In declarations, [] means ‘‘array of’’ and * means ‘‘pointer to.’’ All arrays have 0 as their lower bound, so v has ten elements, v 0]...v 9]. A pointer variable can hold the address of an object of v[0 v[9 the appropriate type: p = &v 3]; v[3 // p points to v’s fourth element Unary & is the address-of operator. Consider copying ten elements from one array to another: v oi d a no th er _f un ct io n() vo id an ot he r_ fu nc ti on { i nt v 1[1 0]; in t v1 10 i nt v 2[1 0]; in t v2 10 // ... f or (i nt i 0; i 10 ++i v 1[i v2 i]; fo r in t i=0 i<1 0; i) v1 i]=v 2[i } This for-statement can be read as ‘‘set i to zero, while i is less than 1 0, copy the i element and 10 ith increment i When applied to an integer variable, the increment operator ++ simply adds 1 i.’’ 1. 2.4 Modular Programming [tour.module] Over the years, the emphasis in the design of programs has shifted from the design of procedures and toward the organization of data. Among other things, this reflects an increase in program size. A set of related procedures with the data they manipulate is often called a module. The program- ming paradigm becomes: Decide which modules you want; partition the program so that data is hidden within modules. This paradigm is also known as the data-hiding principle. Where there is no grouping of proce- dures with related data, the procedural programming style suffices. Also, the techniques for design- ing ‘‘good procedures’’ are now applied for each procedure in a module. The most common exam- ple of a module is the definition of a stack. The main problems that have to be solved are: [1] Provide a user interface for the stack (e.g., functions p us h() and p op pu sh po p()). [2] Ensure that the representation of the stack (e.g., an array of elements) can be accessed only through this user interface. [3] Ensure that the stack is initialized before its first use.
    37. Section 2.4 Modular Programming 27 C++ provides a mechanism for grouping related data, functions, etc., into separate namespaces. For example, the user interface of a S ta ck module could be declared and used like this: St ac k n am es pa ce S ta ck { na me sp ac e St ac k // interface v oi d p us h(c ha r); vo id pu sh ch ar c ha r p op ; ch ar po p() } v oi d f vo id f() { S ta ck :p us h(´c ; St ac k: pu sh c´) i f (S ta ck :p op if St ac k: po p() != ´c c´) e rr or im po ss ib le ; er ro r(\"i mp os si bl e\") } The S ta ck : qualification indicates that the p us h() and p op St ac k: pu sh po p() are those from the S ta ck name- St ac k space. Other uses of those names will not interfere or cause confusion. The definition of the S ta ck could be provided in a separately-compiled part of the program: St ac k n am es pa ce S ta ck { na me sp ac e St ac k // implementation c on st i nt m ax _s iz e = 2 00 co ns t in t ma x_ si ze 20 0; ch ar v[m ax _s iz e] c ha r v ma x_ si ze ; i nt t op = 0 in t to p 0; v oi d p us h(c ha r c { /* check for overflow and push c */ } vo id pu sh ch ar c) c ha r p op { /* check for underflow and pop */ } ch ar po p() } The key point about this S ta ck module is that the user code is insulated from the data representation St ac k of S ta ck by the code implementing S ta ck :p us h() and S ta ck :p op St ac k St ac k: pu sh St ac k: po p(). The user doesn’t need to know that the S ta ck is implemented using an array, and the implementation can be changed without St ac k affecting user code. Because data is only one of the things one might want to ‘‘hide,’’ the notion of data hiding is trivially extended to the notion of information hiding; that is, the names of functions, types, etc., can also be made local to a module. Consequently, C++ allows any declaration to be placed in a namespace (§8.2). This S ta ck module is one way of representing a stack. The following sections use a variety of St ac k stacks to illustrate different programming styles. 2.4.1 Separate Compilation [tour.comp] C++ supports C’s notion of separate compilation. This can be used to organize a program into a set of semi-independent fragments. Typically, we place the declarations that specify the interface to a module in a file with a name indicating its intended use. Thus, n am es pa ce S ta ck { na me sp ac e St ac k // interface v oi d p us h(c ha r); vo id pu sh ch ar c ha r p op ; ch ar po p() } would be placed in a file s ta ck h, and users will include that file, called a header file, like this: st ac k.h
    38. A Tour of C++ 28 Chapter 2 #i nc lu de \"s ta ck h\" in cl ud e st ac k.h // get the interface v oi d f vo id f() { S ta ck :p us h(´c ; St ac k: pu sh c´) i f (S ta ck :p op if St ac k: po p() != ´c c´) e rr or im po ss ib le ; er ro r(\"i mp os si bl e\") } To help the compiler ensure consistency, the file providing the implementation of the S ta ck module St ac k will also include the interface: #i nc lu de \"s ta ck h\" in cl ud e st ac k.h // get the interface n am es pa ce S ta ck { na me sp ac e St ac k // representation c on st i nt m ax _s iz e = 2 00 co ns t in t ma x_ si ze 20 0; ch ar v[m ax _s iz e] c ha r v ma x_ si ze ; i nt t op = 0 in t to p 0; } v oi d S ta ck :p us h(c ha r c { /* check for overflow and push c */ } vo id St ac k: pu sh ch ar c) c ha r S ta ck :p op { /* check for underflow and pop */ } ch ar St ac k: po p() The user code goes in a third file, say u se r.c The code in u se r.c and s ta ck c shares the stack us er c. us er c st ac k.c interface information presented in s ta ck h, but the two files are otherwise independent and can be st ac k.h separately compiled. Graphically, the program fragments can be represented like this: stack.h: . S ta ck i nt er fa ce St ac k in te rf ac e user.c: stack.c: . . . . #i nc lu de \" st ac k. h\" in cl ud e \"s ta ck .h \" #i nc lu de \" st ac k. h\" in cl ud e \"s ta ck .h \" u se s ta ck us e st ac k d ef in e s ta ck de fi ne st ac k Separate compilation is an issue in all real programs. It is not simply a concern in programs that present facilities, such as a S ta ck as modules. Strictly speaking, using separate compilation isn’t a St ac k, language issue; it is an issue of how best to take advantage of a particular language implementation. However, it is of great practical importance. The best approach is to maximize modularity, repre- sent that modularity logically through language features, and then exploit the modularity physically through files for effective separate compilation (Chapter 8, Chapter 9). 2.4.2 Exception Handling [tour.except] When a program is designed as a set of modules, error handling must be considered in light of these modules. Which module is responsible for handling what errors? Often, the module that detects an error doesn’t know what action to take. The recovery action depends on the module that invoked
    39. Section 2.4.2 Exception Handling 29 the operation rather than on the module that found the error while trying to perform the operation. As programs grow, and especially when libraries are used extensively, standards for handling errors (or, more generally, ‘‘exceptional circumstances’’) become important. Consider again the S ta ck example. What ought to be done when we try to p us h() one too St ac k pu sh many characters? The writer of the S ta ck module doesn’t know what the user would like to be St ac k done in this case, and the user cannot consistently detect the problem (if the user could, the over- flow wouldn’t happen in the first place). The solution is for the S ta ck implementer to detect the St ac k overflow and then tell the (unknown) user. The user can then take appropriate action. For exam- ple: n am es pa ce S ta ck { na me sp ac e St ac k // interface v oi d p us h(c ha r); vo id pu sh ch ar c ha r p op ; ch ar po p() c la ss O ve rf lo w { }; // type representing overflow exceptions cl as s Ov er fl ow } When detecting an overflow, S ta ck :p us h() can invoke the exception-handling code; that is, St ac k: pu sh ‘‘throw an O ve rf lo w exception:’’ Ov er fl ow v oi d S ta ck :p us h(c ha r c vo id St ac k: pu sh ch ar c) { i f (t op == m ax _s iz e) t hr ow O ve rf lo w(); if to p ma x_ si ze th ro w Ov er fl ow // push c } The t hr ow transfers control to a handler for exceptions of type S ta ck :O ve rf lo w in some function th ro w St ac k: Ov er fl ow that directly or indirectly called S ta ck :p us h(). To do that, the implementation will unwind the St ac k: pu sh function call stack as needed to get back to the context of that caller. Thus, the t hr ow acts as a mul- th ro w tilevel r et ur n. For example: re tu rn v oi d f vo id f() { // ... t ry { // exceptions here are handled by the handler defined below tr y w hi le (t ru e) S ta ck :p us h(´c ; wh il e tr ue St ac k: pu sh c´) } c at ch (S ta ck :O ve rf lo w) { ca tc h St ac k: Ov er fl ow // oops: stack overflow; take appropriate action } // ... } The w hi le loop will try to loop forever. Therefore, the c at ch wh il e ca tc h-clause providing a handler for S ta ck :O ve rf lo w will be entered after some call of S ta ck :p us h() causes a t hr ow St ac k: Ov er fl ow St ac k: pu sh th ro w. Use of the exception-handling mechanisms can make error handling more regular and readable. See §8.3 and Chapter 14 for further discussion and details.
    40. A Tour of C++ 30 Chapter 2 2.5 Data Abstraction [tour.da] Modularity is a fundamental aspect of all successful large programs. It remains a focus of all design discussions throughout this book. However, modules in the form described previously are not sufficient to express complex systems cleanly. Here, I first present a way of using modules to provide a form of user-defined types and then show how to overcome some problems with that approach by defining user-defined types directly. 2.5.1 Modules Defining Types [tour.types] Programming with modules leads to the centralization of all data of a type under the control of a type manager module. For example, if we wanted many stacks – rather than the single one pro- vided by the S ta ck module above – we could define a stack manager with an interface like this: St ac k n am es pa ce S ta ck { na me sp ac e St ac k s tr uc t R ep st ru ct Re p; // definition of stack layout is elsewhere t yp ed ef R ep s ta ck ty pe de f Re p& st ac k; s ta ck c re at e(); st ac k cr ea te // make a new stack v oi d d es tr oy st ac k s ; vo id de st ro y(s ta ck s) // delete s v oi d p us h(s ta ck s c ha r c ; vo id pu sh st ac k s, ch ar c) // push c onto s c ha r p op st ac k s ; ch ar po p(s ta ck s) // pop s } The declaration s tr uc t R ep st ru ct Re p; says that R ep is the name of a type, but it leaves the type to be defined later (§5.7). The declaration Re p t yp ed ef R ep s ta ck ty pe de f Re p& st ac k; gives the name s ta ck to a ‘‘reference to R ep (details in §5.5). The idea is that a stack is identified st ac k Re p’’ by its S ta ck :s ta ck and that further details are hidden from users. St ac k: st ac k A S ta ck :s ta ck acts much like a variable of a built-in type: St ac k: st ac k s tr uc t B ad _p op { }; st ru ct Ba d_ po p v oi d f vo id f() { S ta ck :s ta ck s 1 = S ta ck :c re at e(); St ac k: st ac k s1 St ac k: cr ea te // make a new stack S ta ck :s ta ck s 2 = S ta ck :c re at e(); St ac k: st ac k s2 St ac k: cr ea te // make another new stack S ta ck :p us h(s 1,´c ; St ac k: pu sh s1 c´) S ta ck :p us h(s 2,´k ; St ac k: pu sh s2 k´) c´) t hr ow B ad _p op ; i f (S ta ck :p op s1 != ´c if St ac k: po p(s 1) th ro w Ba d_ po p() k´) t hr ow B ad _p op ; i f (S ta ck :p op s2 != ´k if St ac k: po p(s 2) th ro w Ba d_ po p() S ta ck :d es tr oy s1 ; St ac k: de st ro y(s 1) S ta ck :d es tr oy s2 ; St ac k: de st ro y(s 2) }
    41. Section 2.5.1 Modules Defining Types 31 We could implement this S ta ck in several ways. It is important that a user doesn’t need to know St ac k how we do it. As long as we keep the interface unchanged, a user will not be affected if we decide to re-implement S ta ck St ac k. An implementation might preallocate a few stack representations and let S ta ck :c re at e() hand St ac k: cr ea te out a reference to an unused one. S ta ck :d es tr oy St ac k: de st ro y() could then mark a representation ‘‘unused’’ so that S ta ck :c re at e() can recycle it: St ac k: cr ea te n am es pa ce S ta ck { na me sp ac e St ac k // representation c on st i nt m ax _s iz e = 2 00 co ns t in t ma x_ si ze 20 0; s tr uc t R ep { st ru ct Re p ch ar v[m ax _s iz e] c ha r v ma x_ si ze ; i nt t op in t to p; }; c on st i nt m ax = 1 6; // maximum number of stacks co ns t in t ma x 16 R ep s ta ck s[m ax ; Re p st ac ks ma x] // preallocated stack representations b oo l u se d[m ax ; bo ol us ed ma x] // used[i] is true if stacks[i] is in use } v oi d S ta ck :p us h(s ta ck s c ha r c { /* check s for overflow and push c */ } vo id St ac k: pu sh st ac k s, ch ar c) c ha r S ta ck :p op st ac k s { /* check s for underflow and pop */ } ch ar St ac k: po p(s ta ck s) S ta ck :s ta ck S ta ck :c re at e() St ac k: st ac k St ac k: cr ea te { // pick an unused Rep, mark it used, initialize it, and return a reference to it } v oi d S ta ck :d es tr oy st ac k s { /* mark s unused */ } vo id St ac k: de st ro y(s ta ck s) What we have done is to wrap a set of interface functions around the representation type. How the resulting ‘‘stack type’’ behaves depends partly on how we defined these interface functions, partly on how we presented the representation type to the users of S ta ck and partly on the design of the St ac ks, representation type itself. This is often less than ideal. A significant problem is that the presentation of such ‘‘fake types’’ to the users can vary greatly depending on the details of the representation type – and users ought to be insulated from knowledge of the representation type. For example, had we chosen to use a more elaborate data structure to identify a stack, the rules for assignment and initialization of S ta ck :s ta ck would have changed dramatically. This may indeed be desirable at times. How- St ac k: st ac ks ever, it shows that we have simply moved the problem of providing convenient S ta ck from the St ac ks S ta ck module to the S ta ck :s ta ck representation type. St ac k St ac k: st ac k More fundamentally, user-defined types implemented through a module providing access to an implementation type don’t behave like built-in types and receive less and different support than do built-in types. For example, the time that a S ta ck :R ep can be used is controlled through St ac k: Re p S ta ck :c re at e() and S ta ck :d es tr oy rather than by the usual language rules. St ac k: cr ea te St ac k: de st ro y()
    42. A Tour of C++ 32 Chapter 2 2.5.2 User-Defined Types [tour.udt] C++ attacks this problem by allowing a user to directly define types that behave in (nearly) the same way as built-in types. Such a type is often called an abstract data type. I prefer the term user-defined type. A more reasonable definition of abstract data type would require a mathemati- cal ‘‘abstract’’ specification. Given such a specification, what are called types here would be con- crete examples of such truly abstract entities. The programming paradigm becomes: Decide which types you want; provide a full set of operations for each type. Where there is no need for more than one object of a type, the data-hiding programming style using modules suffices. Arithmetic types such as rational and complex numbers are common examples of user-defined types. Consider: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex do ub le r d ou bl e i { r e=r i m=i } co mp le x(d ou bl e r, do ub le i) re r; im i; // construct complex from two scalars c om pl ex do ub le r { r e=r i m=0 } co mp le x(d ou bl e r) re r; im 0; // construct complex from one scalar c om pl ex co mp le x() { r e = i m = 0 } re im 0; // default complex: (0,0) f ri en d fr ie nd c om pl ex co mp le x o pe ra to r+(c om pl ex c om pl ex ; op er at or co mp le x, co mp le x) f ri en d fr ie nd c om pl ex co mp le x o pe ra to r-(c om pl ex c om pl ex ; op er at or co mp le x, co mp le x) // binary f ri en d fr ie nd c om pl ex co mp le x o pe ra to r-(c om pl ex ; op er at or co mp le x) // unary f ri en d fr ie nd c om pl ex co mp le x o pe ra to r*(c om pl ex c om pl ex ; op er at or co mp le x, co mp le x) f ri en d fr ie nd c om pl ex co mp le x o pe ra to r/(c om pl ex c om pl ex ; op er at or co mp le x, co mp le x) f ri en d b oo l o pe ra to r==(c om pl ex c om pl ex ; fr ie nd bo ol op er at or co mp le x, co mp le x) // equal f ri en d b oo l o pe ra to r!=(c om pl ex c om pl ex ; fr ie nd bo ol op er at or co mp le x, co mp le x) // not equal // ... }; The declaration of class (that is, user-defined type) c om pl ex specifies the representation of a com- co mp le x plex number and the set of operations on a complex number. The representation is private; that is, r e and i m are accessible only to the functions specified in the declaration of class c om pl ex Such re im co mp le x. functions can be defined like this: c om pl ex o pe ra to r+(c om pl ex a 1, c om pl ex a 2) co mp le x op er at or co mp le x a1 co mp le x a2 { r et ur n c om pl ex a1 re a2 re a1 im a2 im ; re tu rn co mp le x(a 1.r e+a 2.r e,a 1.i m+a 2.i m) } A member function with the same name as its class is called a constructor. A constructor defines a way to initialize an object of its class. Class c om pl ex provides three constructors. One makes a co mp le x c om pl ex from a d ou bl e, another takes a pair of d ou bl es, and the third makes a c om pl ex with a co mp le x do ub le do ub le co mp le x default value. Class c om pl ex can be used like this: co mp le x
    43. Section 2.5.2 User-Defined Types 33 v oi d f co mp le x z vo id f(c om pl ex z) { c om pl ex a = 2 3; co mp le x 2.3 c om pl ex b = 1 a; co mp le x 1/a c om pl ex c = a b*c om pl ex 1,2 3); co mp le x a+b co mp le x(1 2.3 // ... i f (c != b c = -(b a)+2 b; if c b) b/a 2*b } The compiler converts operators involving c om pl ex numbers into appropriate function calls. For co mp le x example, c b means o pe ra to r!=(c b) and 1 a means o pe ra to r/(c om pl ex 1),a c!=b op er at or c,b 1/a op er at or co mp le x(1 a). Most, but not all, modules are better expressed as user-defined types. 2.5.3 Concrete Types [tour.concrete] User-defined types can be designed to meet a wide variety of needs. Consider a user-defined S ta ck St ac k type along the lines of the c om pl ex type. To make the example a bit more realistic, this S ta ck type co mp le x St ac k is defined to take its number of elements as an argument: c la ss S ta ck { cl as s St ac k c ha r* v ch ar v; i nt t op in t to p; i nt m ax _s iz e; in t ma x_ si ze p ub li c: pu bl ic c la ss U nd er fl ow { }; cl as s Un de rf lo w // used as exception c la ss O ve rf lo w { }; cl as s Ov er fl ow // used as exception c la ss B ad _s iz e { }; cl as s Ba d_ si ze // used as exception S ta ck in t s ; St ac k(i nt s) // constructor ~S ta ck ; St ac k() // destructor v oi d p us h(c ha r c ; vo id pu sh ch ar c) c ha r p op ; ch ar po p() }; The constructor S ta ck in t) will be called whenever an object of the class is created. This takes St ac k(i nt care of initialization. If any cleanup is needed when an object of the class goes out of scope, a com- plement to the constructor – called the destructor – can be declared: S ta ck :S ta ck in t s St ac k: St ac k(i nt s) // constructor { t op = 0 to p 0; i f (1 00 00 s) t hr ow B ad _s iz e(); if 10 00 0<s th ro w Ba d_ si ze m ax _s iz e = s ma x_ si ze s; v = n ew c ha r[s ; ne w ch ar s] // allocate elements on the free store (heap, dynamic store) } S ta ck :~S ta ck St ac k: St ac k() // destructor { d el et e[] v de le te v; // free the elements for possible reuse of their space (§6.2.6) }
    44. A Tour of C++ 34 Chapter 2 The constructor initializes a new S ta ck variable. To do so, it allocates some memory on the free St ac k store (also called the heap or dynamic store) using the n ew operator. The destructor cleans up by ne w freeing that memory. This is all done without intervention by users of S ta ck The users simply St ac ks. create and use S ta ck much as they would variables of built-in types. For example: St ac ks S ta ck s _v ar 1(1 0); St ac k s_ va r1 10 // global stack with 10 elements v oi d f St ac k& s _r ef i nt i vo id f(S ta ck s_ re f, in t i) // reference to Stack { S ta ck s _v ar 2(i ; St ac k s_ va r2 i) // local stack with i elements S ta ck s _p tr = n ew S ta ck 20 ; // pointer to Stack allocated on free store St ac k* s_ pt r ne w St ac k(2 0) s _v ar 1.p us h(´a ; s_ va r1 pu sh a´) s _v ar 2.p us h(´b ; s_ va r2 pu sh b´) s _r ef pu sh c´); s_ re f.p us h(´c s _p tr pu sh d´); s_ pt r->p us h(´d // ... } This S ta ck type obeys the same rules for naming, scope, allocation, lifetime, copying, etc., as does St ac k a built-in type such as i nt and c ha r. in t ch ar Naturally, the p us h() and p op member functions must also be defined somewhere: pu sh po p() v oi d S ta ck :p us h(c ha r c vo id St ac k: pu sh ch ar c) { i f (t op == m ax _s iz e) t hr ow O ve rf lo w(); if to p ma x_ si ze th ro w Ov er fl ow v to p] = c v[t op c; t op = t op + 1 to p to p 1; } c ha r S ta ck :p op ch ar St ac k: po p() { i f (t op == 0 t hr ow U nd er fl ow ; if to p 0) th ro w Un de rf lo w() t op = t op - 1 to p to p 1; r et ur n v to p]; re tu rn v[t op } Types such as c om pl ex and S ta ck are called concrete types, in contrast to abstract types, where the co mp le x St ac k interface more completely insulates a user from implementation details. 2.5.4 Abstract Types [tour.abstract] One property was lost in the transition from S ta ck as a ‘‘fake type’’ implemented by a module St ac k (§2.5.1) to a proper type (§2.5.3). The representation is not decoupled from the user interface; rather, it is a part of what would be included in a program fragment using S ta ck The representa- St ac ks. tion is private, and therefore accessible only through the member functions, but it is present. If it changes in any significant way, a user must recompile. This is the price to pay for having concrete types behave exactly like built-in types. In particular, we cannot have genuine local variables of a type without knowing the size of the type’s representation. For types that don’t change often, and where local variables provide much-needed clarity and efficiency, this is acceptable and often ideal. However, if we want to completely isolate users of a
    45. Section 2.5.4 Abstract Types 35 stack from changes to its implementation, this last S ta ck is insufficient. Then, the solution is to St ac k decouple the interface from the representation and give up genuine local variables. First, we define the interface: c la ss S ta ck { cl as s St ac k p ub li c: pu bl ic c la ss U nd er fl ow { }; cl as s Un de rf lo w // used as exception c la ss O ve rf lo w { }; cl as s Ov er fl ow // used as exception v ir tu al v oi d p us h(c ha r c = 0 vi rt ua l vo id pu sh ch ar c) 0; v ir tu al c ha r p op vi rt ua l ch ar po p() = 0 0; }; The word v ir tu al means ‘‘may be redefined later in a class derived from this one’’ in Simula and vi rt ua l C++. A class derived from S ta ck provides an implementation for the S ta ck interface. The curious St ac k St ac k =0 syntax says that some class derived from S ta ck must define the function. Thus, this S ta ck can 0 St ac k St ac k serve as the interface to any class that implements its p us h() and p op functions. pu sh po p() This S ta ck could be used like this: St ac k v oi d f St ac k& s _r ef vo id f(S ta ck s_ re f) { s _r ef pu sh c´); s_ re f.p us h(´c i f (s _r ef po p() != ´c c´) t hr ow b ad _s ta ck ; if s_ re f.p op th ro w ba d_ st ac k() } Note how f f() uses the S ta ck interface in complete ignorance of implementation details. A class St ac k that provides the interface to a variety of other classes is often called a polymorphic type. Not surprisingly, the implementation could consist of everything from the concrete class S ta ck St ac k that we left out of the interface S ta ck St ac k: c la ss A rr ay _s ta ck : p ub li c S ta ck { cl as s Ar ra y_ st ac k pu bl ic St ac k // Array_stack implements Stack c ha r* p ch ar p; i nt m ax _s iz e; in t ma x_ si ze i nt t op in t to p; p ub li c: pu bl ic A rr ay _s ta ck in t s ; Ar ra y_ st ac k(i nt s) ~A rr ay _s ta ck ; Ar ra y_ st ac k() v oi d p us h(c ha r c ; vo id pu sh ch ar c) c ha r p op ; ch ar po p() }; The ‘‘:p ub li c’’ can be read as ‘‘is derived from,’’ ‘‘implements,’’ and ‘‘is a subtype of.’’ pu bl ic For a function like f f() to use a S ta ck in complete ignorance of implementation details, some St ac k other function will have to make an object on which it can operate. For example: v oi d g vo id g() { A rr ay _s ta ck a s(2 00 ; Ar ra y_ st ac k as 20 0) f as ; f(a s) }
    46. A Tour of C++ 36 Chapter 2 Since f doesn’t know about A rr ay _s ta ck but only knows the S ta ck interface, it will work just as f() Ar ra y_ st ac ks St ac k well for a different implementation of a S ta ck For example: St ac k. cl as s Li st _s ta ck pu bl ic St ac k c la ss L is t_ st ac k : p ub li c S ta ck { // List_stack implements Stack l is t<c ha r> l c; li st ch ar lc // (standard library) list of characters (§3.7.3) p ub li c: pu bl ic Li st _s ta ck L is t_ st ac k() { } vo id pu sh ch ar c) lc pu sh _f ro nt c) v oi d p us h(c ha r c { l c.p us h_ fr on t(c ; } c ha r p op ; ch ar po p() }; ch ar Li st _s ta ck po p() c ha r L is t_ st ac k::p op { c ha r x = l c.f ro nt ; ch ar lc fr on t() // get first element l c.p op _f ro nt ; lc po p_ fr on t() // remove first element r et ur n x re tu rn x; } lc pu sh _f ro nt c) Here, the representation is a list of characters. The l c.p us h_ fr on t(c adds c as the first element of l c, the call l c.p op _f ro nt removes the first element, and l c.f ro nt denotes l c’s first element. lc lc po p_ fr on t() lc fr on t() lc Li st _s ta ck A function can create a L is t_ st ac k and have f use it: f() v oi d h vo id h() { Li st _s ta ck ls L is t_ st ac k l s; f ls ; f(l s) } 2.5.5 Virtual Functions [tour.virtual] How is the call s _s et po p() in f s_ se t.p op f() resolved to the right function definition? When f f() is called Li st _s ta ck po p() must be called. When f from h h(), L is t_ st ac k::p op f() is called from g g(), A rr ay _s ta ck :p op Ar ra y_ st ac k: po p() must be called. To achieve this resolution, a S ta ck object must contain St ac k information to indicate the function to be called at run-time. A common implementation technique is for the compiler to convert the name of a v ir tu al function into an index into a table of pointers to vi rt ua l functions. That table is usually called ‘‘a virtual function table’’ or simply, a v tb l. Each class with vt bl virtual functions has its own v tb l identifying its virtual functions. This can be represented graphi- vt bl cally like this: A rr ay _s ta ck o bj ec t: Ar ra y_ st ac k ob je ct : v.tb l: vt bl : . A rr ay _s ta ck pu sh . Ar ra y_ st ac k::p us h() p m ax _s iz e A rr ay _s ta ck po p() ma x_ si ze Ar ra y_ st ac k::p op t op to p Li st _s ta ck ob je ct : L is t_ st ac k o bj ec t: v.tb l: vt bl : . Li st _s ta ck pu sh . L is t_ st ac k::p us h() lc lc Li st _s ta ck po p() L is t_ st ac k::p op
    47. Section 2.5.5 Virtual Functions 37 The functions in the v tb l allow the object to be used correctly even when the size of the object and vt bl the layout of its data are unknown to the caller. All the caller needs to know is the location of the v tb l in a S ta ck and the index used for each virtual function. This virtual call mechanism can be vt bl St ac k made essentially as efficient as the ‘‘normal function call’’ mechanism. Its space overhead is one pointer in each object of a class with virtual functions plus one v tb l for each such class. vt bl 2.6 Object-Oriented Programming [tour.oop] Data abstraction is fundamental to good design and will remain a focus of design throughout this book. However, user-defined types by themselves are not flexible enough to serve our needs. This section first demonstrates a problem with simple user-defined data types and then shows how to overcome that problem by using class hierarchies. 2.6.1 Problems with Concrete Types [tour.problems] A concrete type, like a ‘‘fake type’’ defined through a module, defines a sort of black box. Once the black box has been defined, it does not really interact with the rest of the program. There is no way of adapting it to new uses except by modifying its definition. This situation can be ideal, but it can also lead to severe inflexibility. Consider defining a type S ha pe for use in a graphics system. Sh ap e Assume for the moment that the system has to support circles, triangles, and squares. Assume also that we have c la ss P oi nt /* ... */ }; cl as s Po in t{ c la ss C ol or /* ... */ }; cl as s Co lo r{ The /* and */ specify the beginning and end, respectively, of a comment. This comment notation can be used for multi-line comments and comments that end before the end of a line. We might define a shape like this: e nu m K in d { c ir cl e, t ri an gl e, s qu ar e }; en um Ki nd ci rc le tr ia ng le sq ua re // enumeration (§4.8) c la ss S ha pe { cl as s Sh ap e K in d k Ki nd k; // type field P oi nt c en te r; Po in t ce nt er C ol or c ol Co lo r co l; // ... p ub li c: pu bl ic v oi d d ra w(); vo id dr aw v oi d r ot at e(i nt ; vo id ro ta te in t) // ... }; The ‘‘type field’’ k is necessary to allow operations such as d ra w() and r ot at e() to determine dr aw ro ta te what kind of shape they are dealing with (in a Pascal-like language, one might use a variant record with tag k The function d ra w() might be defined like this: k). dr aw
    48. A Tour of C++ 38 Chapter 2 v oi d S ha pe :d ra w() vo id Sh ap e: dr aw { s wi tc h (k { sw it ch k) c as e c ir cl e: ca se ci rc le // draw a circle b re ak br ea k; c as e t ri an gl e: ca se tr ia ng le // draw a triangle b re ak br ea k; c as e s qu ar e: ca se sq ua re // draw a square b re ak br ea k; } } This is a mess. Functions such as d ra w() must ‘‘know about’’ all the kinds of shapes there are. dr aw Therefore, the code for any such function grows each time a new shape is added to the system. If we define a new shape, every operation on a shape must be examined and (possibly) modified. We are not able to add a new shape to a system unless we have access to the source code for every operation. Because adding a new shape involves ‘‘touching’’ the code of every important operation on shapes, doing so requires great skill and potentially introduces bugs into the code that handles other (older) shapes. The choice of representation of particular shapes can get severely cramped by the requirement that (at least some of) their representation must fit into the typically fixed-sized framework presented by the definition of the general type S ha pe Sh ap e. 2.6.2 Class Hierarchies [tour.hierarchies] The problem is that there is no distinction between the general properties of every shape (that is, a shape has a color, it can be drawn, etc.) and the properties of a specific kind of shape (a circle is a shape that has a radius, is drawn by a circle-drawing function, etc.). Expressing this distinction and taking advantage of it defines object-oriented programming. Languages with constructs that allow this distinction to be expressed and used support object-oriented programming. Other languages don’t. The inheritance mechanism (borrowed for C++ from Simula) provides a solution. First, we specify a class that defines the general properties of all shapes: c la ss S ha pe { cl as s Sh ap e P oi nt c en te r; Po in t ce nt er C ol or c ol Co lo r co l; // ... p ub li c: pu bl ic P oi nt w he re { r et ur n c en te r; } Po in t wh er e() re tu rn ce nt er v oi d m ov e(P oi nt t o) { c en te r = t o; /* ... */ d ra w(); } vo id mo ve Po in t to ce nt er to dr aw v ir tu al v oi d d ra w() = 0 vi rt ua l vo id dr aw 0; v ir tu al v oi d r ot at e(i nt a ng le = 0 vi rt ua l vo id ro ta te in t an gl e) 0; // ... };
    49. Section 2.6.2 Class Hierarchies 39 As in the abstract type S ta ck in §2.5.4, the functions for which the calling interface can be defined St ac k – but where the implementation cannot be defined yet – are v ir tu al In particular, the functions vi rt ua l. d ra w() and r ot at e() can be defined only for specific shapes, so they are declared v ir tu al dr aw ro ta te vi rt ua l. Given this definition, we can write general functions manipulating vectors of pointers to shapes: vo id ro ta te _a ll ve ct or Sh ap e*>& v i nt a ng le // rotate v’s elements angle degrees v oi d r ot at e_ al l(v ec to r<S ha pe v, in t an gl e) { f or (i nt i = 0 i v.s iz e(); ++i v i]->r ot at e(a ng le ; fo r in t 0; i<v si ze i) v[i ro ta te an gl e) } To define a particular shape, we must say that it is a shape and specify its particular properties (including the virtual functions): c la ss C ir cl e : p ub li c S ha pe { cl as s Ci rc le pu bl ic Sh ap e i nt r ad iu s; in t ra di us p ub li c: pu bl ic v oi d d ra w() { /* ... */ } vo id dr aw v oi d r ot at e(i nt {} // yes, the null function vo id ro ta te in t) }; In C++, class C ir cl e is said to be derived from class S ha pe and class S ha pe is said to be a base of Ci rc le Sh ap e, Sh ap e class C ir cl e. An alternative terminology calls C ir cl e and S ha pe subclass and superclass, respec- Ci rc le Ci rc le Sh ap e tively. The derived class is said to inherit members from its base class, so the use of base and derived classes is commonly referred to as inheritance. The programming paradigm is: Decide which classes you want; provide a full set of operations for each class; make commonality explicit by using inheritance. Where there is no such commonality, data abstraction suffices. The amount of commonality between types that can be exploited by using inheritance and virtual functions is the litmus test of the applicability of object-oriented programming to a problem. In some areas, such as interactive graphics, there is clearly enormous scope for object-oriented programming. In other areas, such as classical arithmetic types and computations based on them, there appears to be hardly any scope for more than data abstraction, and the facilities needed for the support of object-oriented programming seem unnecessary. Finding commonality among types in a system is not a trivial process. The amount of common- ality to be exploited is affected by the way the system is designed. When a system is designed – and even when the requirements for the system are written – commonality must be actively sought. Classes can be designed specifically as building blocks for other types, and existing classes can be examined to see if they exhibit similarities that can be exploited in a common base class. For attempts to explain what object-oriented programming is without recourse to specific pro- gramming language constructs, see [Kerr,1987] and [Booch,1994] in §23.6. Class hierarchies and abstract classes (§2.5.4) complement each other instead of being mutually exclusive (§12.5). In general, the paradigms listed here tend to be complementary and often
    50. A Tour of C++ 40 Chapter 2 mutually supportive. For example, classes and modules contain functions, while modules contain classes and functions. The experienced designer applies a variety of paradigms as need dictates. 2.7 Generic Programming [tour.generic] Someone who wants a stack is unlikely always to want a stack of characters. A stack is a general concept, independent of the notion of a character. Consequently, it ought to be represented inde- pendently. More generally, if an algorithm can be expressed independently of representation details and if it can be done so affordably and without logical contortions, it ought to be done so. The programming paradigm is: Decide which algorithms you want; parameterize them so that they work for a variety of suitable types and data structures. 2.7.1 Containers [tour.containers] We can generalize a stack-of-characters type to a stack-of-anything type by making it a template and replacing the specific type c ha r with a template parameter. For example: ch ar t em pl at e<c la ss T c la ss S ta ck { te mp la te cl as s T> cl as s St ac k Tv T* v; i nt m ax _s iz e; in t ma x_ si ze i nt t op in t to p; p ub li c: pu bl ic c la ss U nd er fl ow { }; cl as s Un de rf lo w c la ss O ve rf lo w { }; cl as s Ov er fl ow S ta ck in t s ; St ac k(i nt s) // constructor ~S ta ck ; St ac k() // destructor v oi d p us h(T ; vo id pu sh T) T p op ; po p() }; The t em pl at e<c la ss T prefix makes T a parameter of the declaration it prefixes. te mp la te cl as s T> The member functions might be defined similarly: t em pl at e<c la ss T v oi d S ta ck T>::p us h(T c te mp la te cl as s T> vo id St ac k<T pu sh T c) { i f (t op == m ax _s iz e) t hr ow O ve rf lo w(); if to p ma x_ si ze th ro w Ov er fl ow v to p] = c v[t op c; t op = t op + 1 to p to p 1; }
    51. Section 2.7.1 Containers 41 t em pl at e<c la ss T T S ta ck T>::p op te mp la te cl as s T> St ac k<T po p() { i f (t op == 0 t hr ow U nd er fl ow ; if to p 0) th ro w Un de rf lo w() t op = t op - 1 to p to p 1; r et ur n v to p]; re tu rn v[t op } Given these definitions, we can use stacks like this: S ta ck ch ar s c; St ac k<c ha r> sc // stack of characters S ta ck co mp le x> s cp lx St ac k<c om pl ex sc pl x; // stack of complex numbers S ta ck l is t<i nt > s li St ac k< li st in t> sl i; // stack of list of integers v oi d f vo id f() { s c.p us h(´c ; sc pu sh c´) c´) t hr ow B ad _p op ; i f (s c.p op != ´c if sc po p() th ro w Ba d_ po p() s cp lx pu sh co mp le x(1 2)); sc pl x.p us h(c om pl ex 1,2 co mp le x(1 2)) t hr ow B ad _p op ; i f (s cp lx po p() != c om pl ex 1,2 if sc pl x.p op th ro w Ba d_ po p() } Similarly, we can define lists, vectors, maps (that is, associative arrays), etc., as templates. A class holding a collection of elements of some type is commonly called a container class, or simply a container. Templates are a compile-time mechanism so that their use incurs no run-time overhead com- pared to ‘‘hand-written code.’’ 2.7.2 Generic Algorithms [tour.algorithms] The C++ standard library provides a variety of containers, and users can write their own (Chapter 3, Chapter 17, Chapter 18). Thus, we find that we can apply the generic programming paradigm once more to parameterize algorithms by containers. For example, we want to sort, copy, and search v ec to rs, l is ts, and arrays without having to write s or t(), c op y(), and s ea rc h() functions for each ve ct or li st so rt co py se ar ch container. We also don’t want to convert to a specific data structure accepted by a single sort func- tion. Therefore, we must find a generalized way of defining our containers that allows us to manip- ulate one without knowing exactly which kind of container it is. One approach, the approach taken for the containers and non-numerical algorithms in the C++ standard library (§3.8, Chapter 18) is to focus on the notion of a sequence and manipulate sequences through iterators. Here is a graphical representation of the notion of a sequence: begin end ..... . . elements: ... . . . . ..... A sequence has a beginning and an end. An iterator refers to an element, and provides an operation that makes the iterator refer to the next element of the sequence. The end of a sequence is an
    52. A Tour of C++ 42 Chapter 2 iterator that refers one beyond the last element of the sequence. The physical representation of ‘‘the end’’ may be a sentinel element, but it doesn’t have to be. In fact, the point is that this notion of sequences covers a wide variety of representations, including lists and arrays. We need some standard notation for operations such as ‘‘access an element through an iterator’’ and ‘‘make the iterator refer to the next element.’’ The obvious choices (once you get the idea) are to use the dereference operator * to mean ‘‘access an element through an iterator’’ and the incre- ment operator ++ to mean ‘‘make the iterator refer to the next element.’’ Given that, we can write code like this: t em pl at e<c la ss I n, c la ss O ut v oi d c op y(I n f ro m, I n t oo _f ar O ut t o) te mp la te cl as s In cl as s Ou t> vo id co py In fr om In to o_ fa r, Ou t to { w hi le (f ro m != t oo _f ar { wh il e fr om to o_ fa r) *t o = *f ro m; // copy element pointed to to fr om ++t o; to // next input ++f ro m; fr om // next output } } This copies any container for which we can define iterators with the right syntax and semantics. C++’s built-in, low-level array and pointer types have the right operations for that, so we can write c ha r v c1 20 0]; // array of 200 characters ch ar vc 1[2 00 c ha r v c2 50 0]; // array of 500 characters ch ar vc 2[5 00 v oi d f vo id f() { c op y(&v c1 0],&v c1 20 0],&v c2 0]); co py vc 1[0 vc 1[2 00 vc 2[0 } This copies v c1 from its first element until its last into v c2 starting at v c2 first element. vc 1 vc 2 vc 2’s All standard library containers (§16.3, Chapter 17) support this notion of iterators and sequences. Two template parameters I n and O ut are used to indicate the types of the source and the target In Ou t instead of a single argument. This was done because we often want to copy from one kind of con- tainer into another. For example: c om pl ex a c[2 00 ; co mp le x ac 20 0] v oi d g ve ct or co mp le x>& v c, l is t<c om pl ex l c) vo id g(v ec to r<c om pl ex vc li st co mp le x>& lc { c op y(&a c[0 ,&a c[2 00 ,l c.b eg in co py ac 0] ac 20 0] lc be gi n()); c op y(l c.b eg in ,l c.e nd ,v c.b eg in co py lc be gi n() lc en d() vc be gi n()); } This copies the array to the l is t and the l is t to the v ec to r. For a standard container, b eg in li st li st ve ct or be gi n() is an iterator pointing to the first element.
    53. Section 2.8 Postscript 43 2.8 Postscript [tour.post] No programming language is perfect. Fortunately, a programming language does not have to be perfect to be a good tool for building great systems. In fact, a general-purpose programming lan- guage cannot be perfect for all of the many tasks to which it is put. What is perfect for one task is often seriously flawed for another because perfection in one area implies specialization. Thus, C++ was designed to be a good tool for building a wide variety of systems and to allow a wide variety of ideas to be expressed directly. Not everything can be expressed directly using the built-in features of a language. In fact, that isn’t even the ideal. Language features exist to support a variety of programming styles and tech- niques. Consequently, the task of learning a language should focus on mastering the native and natural styles for that language – not on the understanding of every little detail of all the language features. In practical programming, there is little advantage in knowing the most obscure language fea- tures or for using the largest number of features. A single language feature in isolation is of little interest. Only in the context provided by techniques and by other features does the feature acquire meaning and interest. Thus, when reading the following chapters, please remember that the real purpose of examining the details of C++ is to be able to use them in concert to support good pro- gramming style in the context of sound designs. 2.9 Advice [tour.advice] [1] Don’t panic! All will become clear in time; §2.1. [2] You don’t have to know every detail of C++ to write good programs; §1.7. [3] Focus on programming techniques, not on language features; §2.1. .
    54. A Tour of C++ 44 Chapter 2
    55. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 3 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ A Tour of the Standard Library Why waste time learning when ignorance is instantaneous? – Hobbes Standard libraries — output — strings — input — vectors — range checking — lists — maps — container overview — algorithms — iterators — I/O iterators — traversals and predicates — algorithms using member functions — algorithm overview — complex numbers — vector arithmetic— standard library overview — advice. 3.1 Introduction [tour2.lib] No significant program is written in just a bare programming language. First, a set of supporting libraries are developed. These then form the basis for further work. Continuing Chapter 2, this chapter gives a quick tour of key library facilities to give you an idea what can be done using C++ and its standard library. Useful library types, such as s tr in g, v ec to r, st ri ng ve ct or l is t, and m ap are presented as well as the most common ways of using them. Doing this allows me li st ma p, to give better examples and to set better exercises in the following chapters. As in Chapter 2, you are strongly encouraged not to be distracted or discouraged by an incomplete understanding of details. The purpose of this chapter is to give you a taste of what is to come and to convey an understanding of the simplest uses of the most useful library facilities. A more detailed introduc- tion to the standard library is given in §16.1.2. The standard library facilities described in this book are part of every complete C++ implemen- tation. In addition to the standard C++ library, most implementations offer ‘‘graphical user inter- face’’ systems, often referred to as GUIs or window systems, for interaction between a user and a program. Similarly, most application development environments provide ‘‘foundation libraries’’ that support corporate or industrial ‘‘standard’’ development and/or execution environments. I do not describe such systems and libraries. The intent is to provide a self-contained description of C++
    56. 46 A Tour of the Standard Library Chapter 3 as defined by the standard and to keep the examples portable, except where specifically noted. Nat- urally, a programmer is encouraged to explore the more extensive facilities available on most sys- tems, but that is left to exercises. 3.2 Hello, world! [tour2.hello] The minimal C++ program is i nt m ai n() { } in t ma in It defines a function called m ai n, which takes no arguments and does nothing. ma in Every C++ program must have a function named m ai n(). The program starts by executing that ma in function. The i nt value returned by m ai n(), if any, is the program’s return value to ‘‘the system.’’ in t ma in If no value is returned, the system will receive a value indicating successful completion. A nonzero value from m ai n() indicates failure. ma in Typically, a program produces some output. Here is a program that writes out H el lo w or ld He ll o, wo rl d!: #i nc lu de <i os tr ea m> in cl ud e io st re am i nt m ai n() in t ma in { s td :c ou t << \"H el lo w or ld \\n st d: co ut He ll o, wo rl d!\\ n\"; } The line #i nc lu de <i os tr ea m> instructs the compiler to include the declarations of the standard in cl ud e io st re am stream I/O facilities as found in i os tr ea m. Without these declarations, the expression io st re am s td :c ou t << \"H el lo w or ld \\n st d: co ut He ll o, wo rl d!\\ n\" would make no sense. The operator << (‘‘put to’’) writes its second argument onto its first. In this case, the string literal \"H el lo w or ld \\n is written onto the standard output stream s td :c ou t. A He ll o, wo rl d!\\ n\" st d: co ut string literal is a sequence of characters surrounded by double quotes. In a string literal, the back- slash character \\ followed by another character denotes a single special character. In this case, \\ n is \\n the newline character, so that the characters written are H el lo w or ld followed by a newline. He ll o, wo rl d! 3.3 The Standard Library Namespace [tour2.name] The standard library is defined in a namespace (§2.4, §8.2) called s td That is why I wrote st d. s td :c ou t rather than plain c ou t. I was being explicit about using the s ta nd ar d c ou t, rather than st d: co ut co ut st an da rd co ut some other c ou t. co ut Every standard library facility is provided through some standard header similar to <i os tr ea m>. io st re am For example: #i nc lu de st ri ng in cl ud e<s tr in g> #i nc lu de li st in cl ud e<l is t> This makes the standard s tr in g and l is t available. To use them, the s td : prefix can be used: st ri ng li st st d:
    57. Section 3.3 The Standard Library Namespace 47 s td :s tr in g s = \"F ou r l eg s G oo d; t wo l eg s B aa ad st d: st ri ng Fo ur le gs Go od tw o le gs Ba aa d!\"; s td :l is t<s td :s tr in g> s lo ga ns st d: li st st d: st ri ng sl og an s; For simplicity, I will rarely use the s td : prefix explicitly in examples. Neither will I always st d: #i nc lu de the necessary headers explicitly. To compile and run the program fragments here, you in cl ud e must #i nc lu de the appropriate headers (as listed in §3.7.5, §3.8.6, and Chapter 16). In addition, in cl ud e you must either use the s td : prefix or make every name from s td global (§8.2.3). For example: st d: st d #i nc lu de st ri ng in cl ud e<s tr in g> // make the standard string facilities accessible u si ng n am es pa ce s td us in g na me sp ac e st d; // make std names available without std:: prefix s tr in g s = \"I gn or an ce i s b li ss st ri ng Ig no ra nc e is bl is s!\"; // ok: string is std::string It is generally in poor taste to dump every name from a namespace into the global namespace. However, to keep short the program fragments used to illustrate language and library features, I omit repetitive #i nc lu de and s td : qualifications. In this book, I use the standard library almost in cl ud es st d: exclusively, so if a name from the standard library is used, it either is a use of what the standard offers or part of an explanation of how the standard facility might be defined. 3.4 Output [tour2.ostream] The iostream library defines output for every built-in type. Further, it is easy to define output of a user-defined type. By default, values output to c ou t are converted to a sequence of characters. For co ut example, v oi d f vo id f() { c ou t << 1 0; co ut 10 } will place the character 1 followed by the character 0 on the standard output stream. So will v oi d g vo id g() { i nt i = 1 0; in t 10 c ou t << i co ut i; } Output of different types can be combined in the obvious way: v oi d h in t i vo id h(i nt i) { c ou t << \"t he v al ue o f i i s \"; co ut th e va lu e of is c ou t << i co ut i; c ou t << ´\\ n´; co ut \\n } If i has the value 1 0, the output will be 10 t he v al ue o f i i s 1 0 th e va lu e of is 10
    58. 48 A Tour of the Standard Library Chapter 3 A character constant is a character enclosed in single quotes. Note that a character constant is out- put as a character rather than as a numerical value. For example, v oi d k vo id k() { c ou t << ´a co ut a´; c ou t << ´b co ut b´; c ou t << ´c co ut c´; } will output a bc ab c. People soon tire of repeating the name of the output stream when outputting several related items. Fortunately, the result of an output expression can itself be used for further output. For example: v oi d h 2(i nt i vo id h2 in t i) { c ou t << \"t he v al ue o f i i s \" << i << ´\\ n´; co ut th e va lu e of is \\n } This is equivalent to h h(). Streams are explained in more detail in Chapter 21. 3.5 Strings [tour2.string] The standard library provides a s tr in g type to complement the string literals used earlier. The st ri ng s tr in g type provides a variety of useful string operations, such as concatenation. For example: st ri ng s tr in g s 1 = \"H el lo st ri ng s1 He ll o\"; s tr in g s 2 = \"w or ld st ri ng s2 wo rl d\"; v oi d m 1() vo id m1 { s tr in g s 3 = s 1 + \", \" + s 2 + \"!\\ n\"; st ri ng s3 s1 s2 \\n c ou t << s 3; co ut s3 } Here, s 3 is initialized to the character sequence s3 H el lo w or ld He ll o, wo rl d! followed by a newline. Addition of strings means concatenation. You can add strings, string liter- als, and characters to a string. In many applications, the most common form of concatenation is adding something to the end of a string. This is directly supported by the += operation. For example: v oi d m 2(s tr in g& s 1, s tr in g& s 2) vo id m2 st ri ng s1 st ri ng s2 { s 1 = s 1 + ´\\ n´; // append newline s1 s1 \\n s 2 += ´\\ n´; s2 \\n // append newline }
    59. Section 3.5 Strings 49 The two ways of adding to the end of a string are semantically equivalent, but I prefer the latter because it is more concise and likely to be more efficiently implemented. Naturally, s tr in gs can be compared against each other and against string literals. For example: st ri ng s tr in g i nc an ta ti on st ri ng in ca nt at io n; v oi d r es po nd co ns t s tr in g& a ns we r) vo id re sp on d(c on st st ri ng an sw er { i f (a ns we r == i nc an ta ti on { if an sw er in ca nt at io n) // perform magic } e ls e i f (a ns we r == \"y es { el se if an sw er ye s\") // ... } // ... } The standard library string class is described in Chapter 20. Among other useful features, it pro- vides the ability to manipulate substrings. For example: s tr in g n am e = \"N ie ls S tr ou st ru p\"; st ri ng na me Ni el s St ro us tr up v oi d m 3() vo id m3 { s tr in g s = n am e.s ub st r(6 10 ; st ri ng na me su bs tr 6,1 0) // s = \"Stroustrup\" n am e.r ep la ce 0,5 Ni ch ol as ; na me re pl ac e(0 5,\"N ic ho la s\") // name becomes \"Nicholas Stroustrup\" } The s ub st r() operation returns a string that is a copy of the substring indicated by its arguments. su bs tr The first argument is an index into the string (a position), and the second argument is the length of the desired substring. Since indexing starts from 0 s gets the value S tr ou st ru p. 0, St ro us tr up The r ep la ce operation replaces a substring with a value. In this case, the substring starting at re pl ac e() 0 with length 5 is N ie ls it is replaced by N ic ho la s. Thus, the final value of n am e is N ic ho la s Ni el s; Ni ch ol as na me Ni ch ol as S tr ou st ru p. Note that the replacement string need not be the same size as the substring that it is St ro us tr up replacing. 3.5.1 C-Style Strings [tour2.cstring] A C-style string is a zero-terminated array of characters (§5.2.2). As shown, we can easily enter a C-style string into a s tr in g. To call functions that take C-style strings, we need to be able to extract st ri ng the value of a s tr in g in the form of a C-style string. The c _s tr st ri ng c_ st r() function does that (§20.4.1). For example, we can print the n am e using the C output function p ri nt f() (§21.8) like this: na me pr in tf v oi d f vo id f() { s\\ n\",n am e.c _s tr p ri nt f(\"n am e: %s \\n na me c_ st r()); pr in tf na me }
    60. 50 A Tour of the Standard Library Chapter 3 3.6 Input [tour2.istream] The standard library offers i st re am for input. Like o st re am i st re am deal with character string is tr ea ms os tr ea ms, is tr ea ms representations of built-in types and can easily be extended to cope with user-defined types. The operator >> (‘‘get from’’) is used as an input operator; c in is the standard input stream. ci n The type of the right-hand operand of >> determines what input is accepted and what is the target of the input operation. For example, v oi d f vo id f() { i nt i in t i; c in >> i // read an integer into i ci n i; d ou bl e d do ub le d; c in >> d // read a double-precision, floating-point number into d ci n d; } reads a number, such as 1 23 4, from the standard input into the integer variable i and a floating- 12 34 point number, such as 1 2.3 4e 5, into the double-precision, floating-point variable d 12 34 e5 d. Here is an example that performs inch-to-centimeter and centimeter-to-inch conversions. You input a number followed by a character indicating the unit: centimeters or inches. The program then outputs the corresponding value in the other unit: i nt m ai n() in t ma in { c on st f lo at f ac to r = 2 54 // 1 inch equals 2.54 cm co ns t fl oa t fa ct or 2.5 4; f lo at x i n, c m; fl oa t x, in cm c ha r c h = 0 ch ar ch 0; c ou t << \"e nt er l en gt h: \"; co ut en te r le ng th c in >> x ci n x; // read a floating-point number c in >> c h; ci n ch // read a suffix s wi tc h (c h) { sw it ch ch c as e ´i ca se i´: // inch in = x in x; c m = x fa ct or cm x*f ac to r; b re ak br ea k; c as e ´c ca se c´: // cm i n = x fa ct or in x/f ac to r; cm = x cm x; b re ak br ea k; d ef au lt de fa ul t: in = cm = 0 in cm 0; b re ak br ea k; } c ou t << i n << \" i n = \" << c m << \" c m\\ n\"; co ut in in cm cm \\n } The switch-statement tests a value against a set of constants. The break-statements are used to exit
    61. Section 3.6 Input 51 the switch-statement. The case constants must be distinct. If the value tested does not match any of them, the d ef au lt is chosen. The programmer need not provide a d ef au lt de fa ul t de fa ul t. Often, we want to read a sequence of characters. A convenient way of doing that is to read into a s tr in g. For example: st ri ng i nt m ai n() in t ma in { s tr in g s tr st ri ng st r; c ou t << \"P le as e e nt er y ou r n am e\\ n\"; co ut Pl ea se en te r yo ur na me \\n c in >> s tr ci n st r; c ou t << \"H el lo \" << s tr << \"!\\ n\"; co ut He ll o, st r \\n } If you type in E ri c Er ic the response is H el lo E ri c! He ll o, Er ic By default, a whitespace character (§5.2.2) such as a space terminates the read, so if you enter E ri c B lo od ax e Er ic Bl oo da xe pretending to be the ill-fated king of York, the response is still H el lo E ri c! He ll o, Er ic You can read a whole line using the g et li ne function. For example: ge tl in e() i nt m ai n() in t ma in { s tr in g s tr st ri ng st r; c ou t << \"P le as e e nt er y ou r n am e\\ n\"; co ut Pl ea se en te r yo ur na me \\n g et li ne ci n,s tr ; ge tl in e(c in st r) c ou t << \"H el lo \" << s tr << \"!\\ n\"; co ut He ll o, st r \\n } With this program, the input E ri c B lo od ax e Er ic Bl oo da xe yields the desired output: H el lo E ri c B lo od ax e! He ll o, Er ic Bl oo da xe The standard strings have the nice property of expanding to hold what you put in them, so if you enter a couple of megabytes of semicolons, the program will echo pages of semicolons back at you – unless your machine or operating system runs out of some critical resource first.
    62. 52 A Tour of the Standard Library Chapter 3 3.7 Containers [tour2.stl] Much computing involves creating collections of various forms of objects and then manipulating such collections. Reading characters into a string and printing out the string is a simple example. A class with the main purpose of holding objects is commonly called a container. Providing suit- able containers for a given task and supporting them with useful fundamental operations are impor- tant steps in the construction of any program. To illustrate the standard library’s most useful containers, consider a simple program for keep- ing names and telephone numbers. This is the kind of program for which different approaches appear ‘‘simple and obvious’’ to people of different backgrounds. 3.7.1 Vector [tour2.vector] For many C programmers, a built-in array of (name,number) pairs would seem to be a suitable starting point: s tr uc t E nt ry { st ru ct En tr y s tr in g n am e; st ri ng na me i nt n um be r; in t nu mb er }; E nt ry p ho ne _b oo k[1 00 0]; En tr y ph on e_ bo ok 10 00 v oi d p ri nt _e nt ry in t i vo id pr in t_ en tr y(i nt i) // simple use { c ou t << p ho ne _b oo k[i na me << ´ ´ << p ho ne _b oo k[i nu mb er << ´\\ n´; co ut ph on e_ bo ok i].n am e ph on e_ bo ok i].n um be r \\n } However, a built-in array has a fixed size. If we choose a large size, we waste space; if we choose a smaller size, the array will overflow. In either case, we will have to write low-level memory- management code. The standard library provides a v ec to r (§16.3) that takes care of that: ve ct or v ec to r<E nt ry p ho ne _b oo k(1 00 0); ve ct or En tr y> ph on e_ bo ok 10 00 v oi d p ri nt _e nt ry in t i vo id pr in t_ en tr y(i nt i) // simple use, exactly as for array { c ou t << p ho ne _b oo k[i na me << ´ ´ << p ho ne _b oo k[i nu mb er << ´\\ n´; co ut ph on e_ bo ok i].n am e ph on e_ bo ok i].n um be r \\n } v oi d a dd _e nt ri es in t n // increase size by n vo id ad d_ en tr ie s(i nt n) { p ho ne _b oo k.r es iz e(p ho ne _b oo k.s iz e()+n ; ph on e_ bo ok re si ze ph on e_ bo ok si ze n) } The v ec to r member function s iz e() gives the number of elements. ve ct or si ze Note the use of parentheses in the definition of p ho ne _b oo k. We made a single object of type ph on e_ bo ok v ec to r<E nt ry and supplied its initial size as an initializer. This is very different from declaring a ve ct or En tr y> built-in array: v ec to r<E nt ry b oo k(1 00 0); ve ct or En tr y> bo ok 10 00 // vector of 1000 elements v ec to r<E nt ry b oo ks 10 00 ; ve ct or En tr y> bo ok s[1 00 0] // 1000 empty vectors
    63. Section 3.7.1 Vector 53 Should you make the mistake of using [] where you meant () when declaring a v ec to r, your com- ve ct or piler will almost certainly catch the mistake and issue an error message when you try to use the v ec to r. ve ct or A v ec to r is a single object that can be assigned. For example: ve ct or v oi d f ve ct or En tr y>& v vo id f(v ec to r<E nt ry v) { v ec to r<E nt ry v 2 = p ho ne _b oo k; ve ct or En tr y> v2 ph on e_ bo ok v = v 2; v2 // ... } Assigning a v ec to r involves copying its elements. Thus, after the initialization and assignment in ve ct or f f(), v and v 2 each holds a separate copy of every E nt ry in the phone book. When a v ec to r holds v2 En tr y ve ct or many elements, such innocent-looking assignments and initializations can be prohibitively expen- sive. Where copying is undesirable, references or pointers should be used. 3.7.2 Range Checking [tour2.range] The standard library v ec to r does not provide range checking by default (§16.3.3). For example: ve ct or v oi d f vo id f() { i nt i = p ho ne _b oo k[1 00 1].n um be r; // 1001 is out of range in t ph on e_ bo ok 10 01 nu mb er // ... } The initialization is likely to place some random value in i rather than giving an error. This is undesirable, so I will use a simple range-checking adaptation of v ec to r, called V ec in the following ve ct or Ve c, chapters. A V ec is like a v ec to r, except that it throws an exception of type o ut _o f_ ra ng e if a sub- ou t_ of _r an ge Ve c ve ct or script is out of range. Techniques for implementing types such as V ec and for using exceptions effectively are dis- Ve c cussed in §11.12, §8.3, and Chapter 14. However, the definition here is sufficient for the examples in this book: t em pl at e<c la ss T c la ss V ec : p ub li c v ec to r<T { te mp la te cl as s T> cl as s Ve c pu bl ic ve ct or T> p ub li c: pu bl ic V ec Ve c() : v ec to r<T ve ct or T>() { } V ec in t s : v ec to r<T s) { } Ve c(i nt s) ve ct or T>(s T o pe ra to r[](i nt i { r et ur n a t(i ; } T& op er at or in t i) re tu rn at i) // range-checked c on st T o pe ra to r[](i nt i c on st { r et ur n a t(i ; } co ns t T& op er at or in t i) co ns t re tu rn at i) // range-checked }; The a t() operation is a v ec to r subscript operation that throws an exception of type o ut _o f_ ra ng e ou t_ of _r an ge at ve ct or if its argument is out of the v ec to r’s range (§16.3.3). ve ct or Returning to the problem of keeping names and telephone numbers, we can now use a V ec to Ve c ensure that out-of-range accesses are caught. For example: V ec En tr y> p ho ne _b oo k(1 00 0); Ve c<E nt ry ph on e_ bo ok 10 00
    64. 54 A Tour of the Standard Library Chapter 3 v oi d p ri nt _e nt ry in t i vo id pr in t_ en tr y(i nt i) // simple use, exactly as for vector { c ou t << p ho ne _b oo k[i na me << ´ ´ << p ho ne _b oo k[i nu mb er << ´\\ n´; co ut ph on e_ bo ok i].n am e ph on e_ bo ok i].n um be r \\n } An out-of-range access will throw an exception that the user can catch. For example: v oi d f vo id f() { t ry { tr y 0; i<1 00 00 i++) p ri nt _e nt ry i); f or (i nt i = 0 i 10 00 0; i fo r in t pr in t_ en tr y(i } c at ch (o ut _o f_ ra ng e) { ca tc h ou t_ of _r an ge c ou t << \"r an ge e rr or \\n co ut ra ng e er ro r\\ n\"; } } The exception will be thrown, and then caught, when p ho ne _b oo k[i is tried with i 10 00 ph on e_ bo ok i] i==1 00 0. If the user doesn’t catch this kind of exception, the program will terminate in a well-defined manner rather than proceeding or failing in an undefined manner. One way to minimize surprises from exceptions is to use a m ai n() with a try-block as its body: ma in i nt m ai n() in t ma in t ry { tr y // your code } c at ch (o ut _o f_ ra ng e) { ca tc h ou t_ of _r an ge c er r << \"r an ge e rr or \\n ce rr ra ng e er ro r\\ n\"; } c at ch (...) { ca tc h c er r << \"u nk no wn e xc ep ti on t hr ow n\\ n\"; ce rr un kn ow n ex ce pt io n th ro wn \\n } This provides default exception handlers so that if we fail to catch some exception, an error mes- sage is printed on the standard error-diagnostic output stream c er r (§21.2.1). ce rr 3.7.3 List [tour2.list] Insertion and deletion of phone book entries could be common. Therefore, a list could be more appropriate than a vector for representing a simple phone book. For example: l is t<E nt ry p ho ne _b oo k; li st En tr y> ph on e_ bo ok When we use a list, we tend not to access elements using subscripting the way we commonly do for vectors. Instead, we might search the list looking for an element with a given value. To do this, we take advantage of the fact that a l is t is a sequence as described in §3.8: li st v oi d p ri nt _e nt ry co ns t s tr in g& s vo id pr in t_ en tr y(c on st st ri ng s) { t yp ed ef l is t<E nt ry :c on st _i te ra to r L I; ty pe de f li st En tr y>: co ns t_ it er at or LI
    65. Section 3.7.3 List 55 f or (L I i = p ho ne _b oo k.b eg in ; i != p ho ne _b oo k.e nd ; ++i { fo r LI ph on e_ bo ok be gi n() ph on e_ bo ok en d() i) E nt ry e = *i // reference used as shorthand En tr y& i; i f (s == e na me c ou t << e na me << ´ ´ << e nu mb er << ´\\ n´; if s e.n am e) co ut e.n am e e.n um be r \\n } } The search for s starts at the beginning of the list and proceeds until either s is found or the end is reached. Every standard library container provides the functions b eg in and e nd be gi n() en d(), which return an iterator to the first and to one-past-the-last element, respectively (§16.3.2). Given an iterator i i, the next element is ++i Given an iterator i the element it refers to is *i i. i, i. A user need not know the exact type of the iterator for a standard container. That iterator type is part of the definition of the container and can be referred to by name. When we don’t need to mod- ify an element of the container, c on st _i te ra to r is the type we want. Otherwise, we use the plain co ns t_ it er at or i te ra to r type (§16.3.1). it er at or Adding elements to a l is t is easy: li st v oi d a dd _e nt ry En tr y& e l is t<E nt ry :i te ra to r i vo id ad d_ en tr y(E nt ry e, li st En tr y>: it er at or i) { p ho ne _b oo k.p us h_ fr on t(e ; ph on e_ bo ok pu sh _f ro nt e) // add at beginning p ho ne _b oo k.p us h_ ba ck e); ph on e_ bo ok pu sh _b ac k(e // add at end p ho ne _b oo k.i ns er t(i e); ph on e_ bo ok in se rt i,e // add before the element ‘i’ refers to } 3.7.4 Map [tour2.map] Writing code to look up a name in a list of (name,number) pairs is really quite tedious. In addition, a linear search is quite inefficient for all but the shortest lists. Other data structures directly support insertion, deletion, and searching based on values. In particular, the standard library provides the m ap type (§17.4.1). A m ap is a container of pairs of values. For example: ma p ma p m ap st ri ng in t> p ho ne _b oo k; ma p<s tr in g,i nt ph on e_ bo ok In other contexts, a m ap is known as an associative array or a dictionary. ma p When indexed by a value of its first type (called the key) a m ap returns the corresponding value ma p of the second type (called the value or the mapped type). For example: v oi d p ri nt _e nt ry co ns t s tr in g& s vo id pr in t_ en tr y(c on st st ri ng s) { i f (i nt i = p ho ne _b oo k[s c ou t << s << ´ ´ << i << ´\\ n´; if in t ph on e_ bo ok s]) co ut \\n } If no match was found for the key s a default value is returned from the p ho ne _b oo k. The default s, ph on e_ bo ok value for an integer type in a m ap is 0 Here, I assume that 0 isn’t a valid telephone number. ma p 0. 3.7.5 Standard Containers [tour2.stdcontainer] A m ap a l is t, and a v ec to r can each be used to represent a phone book. However, each has ma p, li st ve ct or strengths and weaknesses. For example, subscripting a v ec to r is cheap and easy. On the other ve ct or hand, inserting an element between two elements tends to be expensive. A l is t has exactly the li st
    66. 56 A Tour of the Standard Library Chapter 3 opposite properties. A m ap resembles a l is t of (key,value) pairs except that it is optimized for find- ma p li st ing values based on keys. The standard library provides some of the most general and useful container types to allow the programmer to select a container that best serves the needs of an application: _________________________________________________________________ ________________________________________________________________ _ _ ________________________________________________________________ Standard Container Summary ve ct or <T >  v ec to r< T> A variable-sized vector (§16.3)  li st <T >  l is t< T>  A doubly-linked list (§17.2.2)  q ue ue <T >  qu eu e< T> A queue (§17.3.2)  s ta ck <T >  st ac k< T> A stack (§17.3.1)  d eq ue <T >  de qu e< T> A double-ended queue (§17.2.3)   pr io ri ty _q ue ue <T >  p ri or it y_ qu eu e< T> A queue sorted by value (§17.3.3)   s et <T >  se t< T> A set (§17.4.3)  m ul ti se t< T> mu lt is et <T > A set in which a value can occur many times (§17.4.4)   m ap <k ey ,v al >  ma p< ke y, va l> An associative array (§17.4.1)  m ul ti ma p< ke y, va l> A map in which a key can occur many times (§17.4.2)  _mu lt im ap <k ey ,v al > ________________________________________________________________ The standard containers are presented in §16.2, §16.3, and Chapter 17. The containers are defined in namespace s td and presented in headers <v ec to r>, <l is t>, <m ap etc. (§16.2). st d ve ct or li st ma p>, The standard containers and their basic operations are designed to be similar from a notational point of view. Furthermore, the meanings of the operations are equivalent for the various contain- pu sh _b ac k() can ers. In general, basic operations apply to every kind of container. For example, p us h_ ba ck be used (reasonably efficiently) to add elements to the end of a v ec to r as well as for a l is t, and ve ct or li st every container has a s iz e() member function that returns its number of elements. si ze This notational and semantic uniformity enables programmers to provide new container types that can be used in a very similar manner to the standard ones. The range-checked vector, V ec Ve c ha sh _m ap (§3.7.2), is an example of that. Chapter 17 demonstrates how a h as h_ ma p can be added to the framework. The uniformity of container interfaces also allows us to specify algorithms indepen- dently of individual container types. 3.8 Algorithms [tour2.algorithms] A data structure, such as a list or a vector, is not very useful on its own. To use one, we need oper- ations for basic access such as adding and removing elements. Furthermore, we rarely just store objects in a container. We sort them, print them, extract subsets, remove elements, search for objects, etc. Consequently, the standard library provides the most common algorithms for contain- ers in addition to providing the most common container types. For example, the following sorts a v ec to r and places a copy of each unique v ec to r element on a l is t: ve ct or ve ct or li st v oi d f ve ct or En tr y>& v e, l is t<E nt ry l e) vo id f(v ec to r<E nt ry ve li st En tr y>& le { s or t(v e.b eg in ,v e.e nd so rt ve be gi n() ve en d()); un iq ue _c op y(v e.b eg in u ni qu e_ co py ve be gi n(),v e.e nd ,l e.b eg in ve en d() le be gi n()); }
    67. Section 3.8 Algorithms 57 The standard algorithms are described in Chapter 18. They are expressed in terms of sequences of elements (§2.7.2). A sequence is represented by a pair of iterators specifying the first element and the one-beyond-the-last element. In the example, s or t() sorts the sequence from v e.b eg in so rt ve be gi n() to v e.e nd ve en d() – which just happens to be all the elements of a v ec to r. For writing, you need only to ve ct or specify the first element to be written. If more than one element is written, the elements following that initial element will be overwritten. If we wanted to add the new elements to the end of a container, we could have written: v oi d f ve ct or En tr y>& v e, l is t<E nt ry l e) vo id f(v ec to r<E nt ry ve li st En tr y>& le { s or t(v e.b eg in ,v e.e nd so rt ve be gi n() ve en d()); un iq ue _c op y(v e.b eg in ve en d() ba ck _i ns er te r(l e)) u ni qu e_ co py ve be gi n(),v e.e nd ,b ac k_ in se rt er le ; // append to le } ba ck _i ns er te r() adds elements at the end of a container, extending the container to make room A b ac k_ in se rt er for them (§19.2.4). C programmers will appreciate that the standard containers plus ba ck _i ns er te r()s eliminate the need to use error-prone, explicit C-style memory management b ac k_ in se rt er ba ck _i ns er te r() when appending can lead to using r ea ll oc re al lo c() (§16.3.5). Forgetting to use a b ac k_ in se rt er errors. For example: v oi d f li st En tr y>& v e, v ec to r<E nt ry vo id f(l is t<E nt ry ve ve ct or En tr y>& l e) le { c op y(v e.b eg in ,v e.e nd ,l e); co py ve be gi n() ve en d() le // error: le not an iterator c op y(v e.b eg in ,v e.e nd ,l e.e nd co py ve be gi n() ve en d() le en d()); // bad: writes beyond the end c op y(v e.b eg in ,v e.e nd ,l e.b eg in co py ve be gi n() ve en d() le be gi n()); // overwrite elements } 3.8.1 Use of Iterators [tour2.iteruse] When you first encounter a container, a few iterators referring to useful elements can be obtained; b eg in be gi n() and e nd en d() are the best examples of this. In addition, many algorithms return iterators. For example, the standard algorithm f in d looks for a value in a sequence and returns an iterator to fi nd the element found. Using f in d, we can write a function that counts the number of occurrences of a fi nd character in a s tr in g: st ri ng i nt c ou nt co ns t s tr in g& s c ha r c in t co un t(c on st st ri ng s, ch ar c) { s tr in g::c on st _i te ra to r i = f in d(s be gi n(),s en d(),c ; st ri ng co ns t_ it er at or fi nd s.b eg in s.e nd c) i nt n = 0 in t 0; w hi le (i != s en d()) { wh il e i s.e nd n; ++n i = f in d(i 1,s en d(),c ; fi nd i+1 s.e nd c) } r et ur n n re tu rn n; } The f in d algorithm returns an iterator to the first occurrence of a value in a sequence or the one- fi nd past-the-end iterator. Consider what happens for a simple call of c ou nt co un t:
    68. 58 A Tour of the Standard Library Chapter 3 v oi d f vo id f() { s tr in g m = \"M ar y h ad a l it tl e l am b\"; st ri ng Ma ry ha d li tt le la mb i nt a _c ou nt = c ou nt m,´a ; in t a_ co un t co un t(m a´) } The first call to f in d() finds the ´a in M ar y. Thus, the iterator points to that character and not to fi nd a´ Ma ry s en d(), so we enter the loop. In the loop, we start the search at i 1; that is, we start one past s.e nd i+1 where we found the ´a We then loop finding the other three ´a a´. a´s. That done, f in d() reaches fi nd the end and returns s en d() so that the condition i s.e nd fails and we exit the loop. s.e nd i!=s en d() That call of c ou nt could be graphically represented like this: co un t() ..... . . Ma r y had a l i t t l e l amb . . . . ..... The arrows indicate the initial, intermediate, and final values of the iterator i i. Naturally, the f in d algorithm will work equivalently on every standard container. Conse- fi nd quently, we could generalize the c ou nt function in the same way: co un t() t em pl at e<c la ss C c la ss T i nt c ou nt co ns t C v T v al te mp la te cl as s C, cl as s T> in t co un t(c on st C& v, va l) { t yp en am e C :c on st _i te ra to r i = f in d(v be gi n(),v en d(),v al ; // \"typename;\" see §C.13.5 ty pe na me C: co ns t_ it er at or fi nd v.b eg in v.e nd va l) i nt n = 0 in t 0; w hi le (i != v en d()) { wh il e i v.e nd n; ++n ++i // skip past the element we just found i; i = f in d(i v.e nd ,v al ; fi nd i,v en d() va l) } r et ur n n re tu rn n; } This works, so we can say: v oi d f li st co mp le x>& l c, v ec to r<s tr in g>& v c, s tr in g s vo id f(l is t<c om pl ex lc ve ct or st ri ng vc st ri ng s) { i nt i 1 = c ou nt lc co mp le x(1 3)); in t i1 co un t(l c,c om pl ex 1,3 i nt i 2 = c ou nt vc Ch ry si pp us ; in t i2 co un t(v c,\"C hr ys ip pu s\") i nt i 3 = c ou nt s,´x ; in t i3 co un t(s x´) } However, we don’t have to define a c ou nt template. Counting occurrences of an element is so gen- co un t erally useful that the standard library provides that algorithm. To be fully general, the standard library c ou nt takes a sequence as its argument, rather than a container, so we would say: co un t
    69. Section 3.8.1 Use of Iterators 59 v oi d f li st co mp le x>& l c, v ec to r<s tr in g>& v s, s tr in g s vo id f(l is t<c om pl ex lc ve ct or st ri ng vs st ri ng s) { i nt i 1 = c ou nt lc be gi n(),l c.e nd ,c om pl ex 1,3 ; in t i1 co un t(l c.b eg in lc en d() co mp le x(1 3)) i nt i 2 = c ou nt vs be gi n(),v s.e nd ,\"D io ge ne s\"); in t i2 co un t(v s.b eg in vs en d() Di og en es i nt i 3 = c ou nt s.b eg in ,s en d(),´x ; in t i3 co un t(s be gi n() s.e nd x´) } The use of a sequence allows us to use c ou nt for a built-in array and also to count parts of a con- co un t tainer. For example: v oi d g ch ar c s[], i nt s z) vo id g(c ha r cs in t sz { i nt i 1 = c ou nt cs 0],&c s[s z],´z ; in t i1 co un t(&c s[0 cs sz z´) // ’z’s in array i nt i 2 = c ou nt cs 0],&c s[s z/2 ,´z ; in t i2 co un t(&c s[0 cs sz 2] z´) // ’z’s in first half of array } 3.8.2 Iterator Types [tour2.iter] What are iterators really? Any particular iterator is an object of some type. There are, however, many different iterator types because an iterator needs to hold the information necessary for doing its job for a particular container type. These iterator types can be as different as the containers and the specialized needs they serve. For example, a v ec to r’s iterator is most likely an ordinary pointer ve ct or because a pointer is quite a reasonable way of referring to an element of a v ec to r: ve ct or iterator: p vector: P i e t He i n Alternatively, a v ec to r iterator could be implemented as a pointer to the v ec to r plus an index: ve ct or ve ct or iterator: (start == p, position == 3) ............. vector: P i e t He i n Using such an iterator would allow range checking (§19.3). A list iterator must be something more complicated than a simple pointer to an element because an element of a list in general does not know where the next element of that list is. Thus, a list iter- ator might be a pointer to a link:
    70. 60 A Tour of the Standard Library Chapter 3 iterator: p list: link link link link ... elements: P i e t What is common for all iterators is their semantics and the naming of their operations. For exam- ple, applying ++ to any iterator yields an iterator that refers to the next element. Similarly, * yields the element to which the iterator refers. In fact, any object that obeys a few simple rules like these is an iterator (§19.2.1). Furthermore, users rarely need to know the type of a specific iterator; each container ‘‘knows’’ its iterator types and makes them available under the conventional names i te ra - it er a- t or and c on st _i te ra to r. For example, l is t<E nt ry :i te ra to r is the general iterator type for to r co ns t_ it er at or li st En tr y>: it er at or l is t<E nt ry I rarely have to worry about the details of how that type is defined. li st En tr y>. 3.8.3 Iterators and I/O [tour2.ioiterators] Iterators are a general and useful concept for dealing with sequences of elements in containers. However, containers are not the only place where we find sequences of elements. For example, an input stream produces a sequence of values and we write a sequence of values to an output stream. Consequently, the notion of iterators can be usefully applied to input and output. To make an o st re am _i te ra to r, we need to specify which stream will be used and the type of os tr ea m_ it er at or objects written to it. For example, we can define an iterator that refers to the standard output stream, c ou t: co ut o st re am _i te ra to r<s tr in g> o o(c ou t); os tr ea m_ it er at or st ri ng oo co ut The effect of assigning to *o o is to write the assigned value to c ou t. For example: oo co ut i nt m ai n() in t ma in { // meaning cout << \"Hello, \" *o o = \"H el lo \"; oo He ll o, ++o o; oo // meaning cout << \"world!\\n\" *o o = \"w or ld \\n oo wo rl d!\\ n\"; } This is yet another way of writing the canonical message to standard output. The ++o o is done to oo mimic writing into an array through a pointer. This way wouldn’t be my first choice for that simple task, but the utility of treating output as a write-only container will soon be obvious – if it isn’t already. Similarly, an i st re am _i te ra to r is something that allows us to treat an input stream as a read- is tr ea m_ it er at or only container. Again, we must specify the stream to be used and the type of values expected: i st re am _i te ra to r<s tr in g> i i(c in ; is tr ea m_ it er at or st ri ng ii ci n) Because input iterators invariably appear in pairs representing a sequence, we must provide an
    71. Section 3.8.3 Iterators and I/O 61 i st re am _i te ra to r to indicate the end of input. This is the default i st re am _i te ra to r: is tr ea m_ it er at or is tr ea m_ it er at or i st re am _i te ra to r<s tr in g> e os is tr ea m_ it er at or st ri ng eo s; We could now read H el lo w or ld from input and write it out again like this: He ll o, wo rl d! i nt m ai n() in t ma in { s tr in g s 1 = *i i; st ri ng s1 ii ++i i; ii s tr in g s 2 = *i i; st ri ng s2 ii c ou t << s 1 << ´ ´ << s 2 << ´\\ n´; co ut s1 s2 \\n } Actually, i st re am _i te ra to rs and o st re am _i te ra to rs are not meant to be used directly. Instead, they is tr ea m_ it er at or os tr ea m_ it er at or are typically provided as arguments to algorithms. For example, we can write a simple program to read a file, sort the words read, eliminate duplicates, and write the result to another file: i nt m ai n() in t ma in { s tr in g f ro m, t o; st ri ng fr om to c in >> f ro m >> t o; ci n fr om to // get source and target file names i fs tr ea m i s(f ro m.c _s tr if st re am is fr om c_ st r()); // input stream (c_str(); see §3.5) i st re am _i te ra to r<s tr in g> i i(i s); is tr ea m_ it er at or st ri ng ii is // input iterator for stream i st re am _i te ra to r<s tr in g> e os is tr ea m_ it er at or st ri ng eo s; // input sentinel v ec to r<s tr in g> b ii eo s); ve ct or st ri ng b(i i,e os // b is a vector initialized from input s or t(b be gi n(),b en d()); so rt b.b eg in b.e nd // sort the buffer o fs tr ea m o s(t o.c _s tr of st re am os to c_ st r()); // output stream o st re am _i te ra to r<s tr in g> o o(o s,\"\\ n\"); os tr ea m_ it er at or st ri ng oo os \\n // output iterator for stream un iq ue _c op y(b be gi n() b.e nd u ni qu e_ co py b.b eg in ,b en d(),o o); oo // copy buffer to output, // discard replicated values r et ur n !i s.e of re tu rn is eo f() && !o s; os // return error state (§3.2, §21.3.3) } An i fs tr ea m is an i st re am that can be attached to a file, and an o fs tr ea m is an o st re am that can be if st re am is tr ea m of st re am os tr ea m attached to a file. The o st re am _i te ra to r’s second argument is used to delimit output values. os tr ea m_ it er at or 3.8.4 Traversals and Predicates [tour2.traverse] Iterators allow us to write loops to iterate through a sequence. However, writing loops can be tedious, so the standard library provides ways for a function to be called for each element of a sequence. Consider writing a program that reads words from input and records the frequency of their occurrence. The obvious representation of the strings and their associated frequencies is a m ap ma p: m ap st ri ng in t> h is to gr am ma p<s tr in g,i nt hi st og ra m; The obvious action to be taken for each string to record its frequency is:
    72. 62 A Tour of the Standard Library Chapter 3 v oi d r ec or d(c on st s tr in g& s vo id re co rd co ns t st ri ng s) { h is to gr am s]++; hi st og ra m[s // record frequency of ‘‘s’’ } Once the input has been read, we would like to output the data we have gathered. The m ap consists ma p of a sequence of (string,int) pairs. Consequently, we would like to call v oi d p ri nt co ns t p ai r<c on st s tr in g,i nt r vo id pr in t(c on st pa ir co ns t st ri ng in t>& r) { c ou t << r fi rs t << ´ ´ << r se co nd << ´\\ n´; co ut r.f ir st r.s ec on d \\n } for each element in the map (the first element of a p ai r is called f ir st and the second element is pa ir fi rs t, called s ec on d). The first element of the p ai r is a c on st s tr in g rather than a plain s tr in g because all se co nd pa ir co ns t st ri ng st ri ng m ap keys are constants. ma p Thus, the main program becomes: i nt m ai n() in t ma in { i st re am _i te ra to r<s tr in g> i i(c in ; is tr ea m_ it er at or st ri ng ii ci n) i st re am _i te ra to r<s tr in g> e os is tr ea m_ it er at or st ri ng eo s; f or _e ac h(i i,e os re co rd ; fo r_ ea ch ii eo s,r ec or d) f or _e ac h(h is to gr am be gi n(),h is to gr am en d(),p ri nt ; fo r_ ea ch hi st og ra m.b eg in hi st og ra m.e nd pr in t) } Note that we don’t need to sort the m ap to get the output in order. A m ap keeps its elements ma p ma p ordered so that an iteration traverses the m ap in (increasing) order. ma p Many programming tasks involve looking for something in a container rather than simply doing something to every element. For example, the f in d algorithm (§18.5.2) provides a convenient way fi nd of looking for a specific value. A more general variant of this idea looks for an element that fulfills a specific requirement. For example, we might want to search a m ap for the first value larger than ma p 4 2. A m ap is a sequence of (key,value) pairs, so we search that list for a p ai r<c on st s tr in g,i nt 42 ma p pa ir co ns t st ri ng in t> where the i nt is greater than 4 2: in t 42 bo ol gt _4 2(c on st pa ir co ns t st ri ng in t>& r b oo l g t_ 42 co ns t p ai r<c on st s tr in g,i nt r) { r et ur n r se co nd 42 re tu rn r.s ec on d>4 2; } v oi d f ma p<s tr in g,i nt vo id f(m ap st ri ng in t>& m m) { t yp ed ef m ap st ri ng in t>::c on st _i te ra to r M I; ty pe de f ma p<s tr in g,i nt co ns t_ it er at or MI fi nd _i f(m be gi n() m.e nd gt _4 2) M I i = f in d_ if m.b eg in ,m en d(),g t_ 42 ; MI // ... } Alternatively, we could count the number of words with a frequency higher than 42:
    73. Section 3.8.4 Traversals and Predicates 63 v oi d g co ns t m ap st ri ng in t>& m vo id g(c on st ma p<s tr in g,i nt m) { i nt c 42 = c ou nt _i f(m be gi n(),m en d(),g t_ 42 ; gt _4 2) in t c4 2 co un t_ if m.b eg in m.e nd // ... } gt _4 2(), that is used to control the algorithm is called a predicate. A predicate A function, such as g t_ 42 is called for each element and returns a Boolean value, which the algorithm uses to perform its fi nd _i f() searches until its predicate returns t ru e to indicate that an intended action. For example, f in d_ if tr ue element of interest has been found. Similarly, c ou nt _i f() counts the number of times its predicate co un t_ if is t ru e. tr ue The standard library provides a few useful predicates and some templates that are useful for cre- ating more (§18.4.2). 3.8.5 Algorithms Using Member Functions [tour2.memp] Many algorithms apply a function to elements of a sequence. For example, in §3.8.4 f or _e ac h(i i,e os re co rd ; fo r_ ea ch ii eo s,r ec or d) calls r ec or d() to read strings from input. re co rd Often, we deal with containers of pointers and we really would like to call a member function of the object pointed to, rather than a global function on the pointer. For example, we might want to call the member function S ha pe :d ra w() for each element of a l is t<S ha pe Sh ap e: dr aw li st Sh ap e*>. To handle this specific example, we simply write a nonmember function that invokes the member function. For example: v oi d d ra w(S ha pe p vo id dr aw Sh ap e* p) { p dr aw ; p->d ra w() } v oi d f li st Sh ap e*>& s h) vo id f(l is t<S ha pe sh { f or _e ac h(s h.b eg in ,s h.e nd ,d ra w); fo r_ ea ch sh be gi n() sh en d() dr aw } By generalizing this technique, we can write the example like this: v oi d g li st Sh ap e*>& s h) vo id g(l is t<S ha pe sh { f or _e ac h(s h.b eg in ,s h.e nd ,m em _f un Sh ap e::d ra w)); fo r_ ea ch sh be gi n() sh en d() me m_ fu n(&S ha pe dr aw } The standard library m em _f un template (§18.4.4.2) takes a pointer to a member function (§15.5) me m_ fu n() as its argument and produces something that can be called for a pointer to the member’s class. The result of m em _f un Sh ap e::d ra w) takes a S ha pe me m_ fu n(&S ha pe dr aw Sh ap e* argument and returns whatever S ha pe :d ra w() returns. Sh ap e: dr aw The m em _f un me m_ fu n() mechanism is important because it allows the standard algorithms to be used for containers of polymorphic objects.
    74. 64 A Tour of the Standard Library Chapter 3 3.8.6 Standard Library Algorithms [tour2.algolist] What is an algorithm? A general definition of an algorithm is ‘‘a finite set of rules which gives a sequence of operations for solving a specific set of problems [and] has five important features: Finiteness ... Definiteness ... Input ... Output ... Effectiveness’’ [Knuth,1968,§1.1]. In the context of the C++ standard library, an algorithm is a set of templates operating on sequences of elements. The standard library provides dozens of algorithms. The algorithms are defined in namespace s td and presented in the <a lg or it hm header. Here are a few I have found particularly useful: st d al go ri th m> _ _____________________________________________________________________  _____________________________________________________________________ _ _ _____________________________________________________________________ Selected Standard Algorithms  f or _e ac h( ) fo r_ ea ch () Invoke function for each element (§18.5.1)    f in d( )  fi nd () Find first occurrence of arguments (§18.5.2)  f in d_ if () fi nd _i f( )  Find first match of predicate (§18.5.2)  c ou nt ()  co un t( ) Count occurrences of element (§18.5.3)  c ou nt _i f( )  co un t_ if () Count matches of predicate (§18.5.3)    r ep la ce () re pl ac e( ) Replace element with new value (§18.6.4)   r ep la ce _i f( ) Replace element that matches predicate with new value (§18.6.4)  re pl ac e_ if ()  c op y( )  co py () Copy elements (§18.6.1)  u ni qu e_ co py ()  un iq ue _c op y( ) Copy elements that are not duplicates (§18.6.1)  s or t( )  so rt () Sort elements (§18.7.1)    e qu al _r an ge () eq ua l_ ra ng e( ) Find all elements with equivalent values (§18.7.2)   me rg e( ) _m er ge () _____________________________________________________________________ Merge sorted sequences (§18.7.3) These algorithms, and many more (see Chapter 18), can be applied to elements of containers, s tr in gs, and built-in arrays. st ri ng 3.9 Math [tour2.math] Like C, C++ wasn’t designed primarily with numerical computation in mind. However, a lot of numerical work is done in C++, and the standard library reflects that. 3.9.1 Complex Numbers [tour2.complex] The standard library supports a family of complex number types along the lines of the c om pl ex co mp le x class described in §2.5.2. To support complex numbers where the scalars are single-precision, floating-point numbers (f lo at double precision numbers (d ou bl es), etc., the standard library c om - fl oa ts), do ub le co m- p le x is a template: pl ex t em pl at e<c la ss s ca la r> c la ss c om pl ex { te mp la te cl as s sc al ar cl as s co mp le x p ub li c: pu bl ic c om pl ex sc al ar r e, s ca la r i m); co mp le x(s ca la r re sc al ar im // ... }; The usual arithmetic operations and the most common mathematical functions are supported for complex numbers. For example:
    75. Section 3.9.1 Complex Numbers 65 // standard exponentiation function from <complex>: t em pl at e<c la ss C c om pl ex C> p ow co ns t c om pl ex C>&, i nt ; te mp la te cl as s C> co mp le x<C po w(c on st co mp le x<C in t) v oi d f co mp le x<f lo at f l, c om pl ex do ub le d b) vo id f(c om pl ex fl oa t> fl co mp le x<d ou bl e> db { c om pl ex lo ng d ou bl e> l d = f l+s qr t(d b); co mp le x<l on g do ub le ld fl sq rt db d b += f l*3 db fl 3; f l = p ow 1/f l,2 ; fl po w(1 fl 2) // ... } For more details, see §22.5. 3.9.2 Vector Arithmetic [tour2.valarray] The v ec to r described in §3.7.1 was designed to be a general mechanism for holding values, to be ve ct or flexible, and to fit into the architecture of containers, iterators, and algorithms. However, it does not support mathematical vector operations. Adding such operations to v ec to r would be easy, but ve ct or its generality and flexibility precludes optimizations that are often considered essential for serious numerical work. Consequently, the standard library provides a vector, called v al ar ra y, that is less va la rr ay general and more amenable to optimization for numerical computation: t em pl at e<c la ss T c la ss v al ar ra y { te mp la te cl as s T> cl as s va la rr ay // ... si ze _t T o pe ra to r[](s iz e_ t); T& op er at or // ... }; si ze _t The type s iz e_ t is the unsigned integer type that the implementation uses for array indices. The usual arithmetic operations and the most common mathematical functions are supported for v al ar ra ys. For example: va la rr ay // standard absolute value function from <valarray>: t em pl at e<c la ss T v al ar ra y<T a bs co ns t v al ar ra y<T te mp la te cl as s T> va la rr ay T> ab s(c on st va la rr ay T>&); v oi d f va la rr ay do ub le a 1, v al ar ra y<d ou bl e>& a 2) vo id f(v al ar ra y<d ou bl e>& a1 va la rr ay do ub le a2 { v al ar ra y<d ou bl e> a = a 1*3 14 a2 a1 va la rr ay do ub le a1 3.1 4+a 2/a 1; a 2 += a 1*3 14 a2 a1 3.1 4; a = a bs a); ab s(a d ou bl e d = a 2[7 ; do ub le a2 7] // ... } For more details, see §22.4. 3.9.3 Basic Numeric Support [tour2.basicnum] Naturally, the standard library contains the most common mathematical functions – such as l oglo g(), p ow po w(), and c os co s() – for floating-point types; see §22.3. In addition, classes that describe the properties of built-in types – such as the maximum exponent of a f lo at – are provided; see §22.2. fl oa t
    76. 66 A Tour of the Standard Library Chapter 3 3.10 Standard Library Facilities [tour2.post] The facilities provided by the standard library can be classified like this: [1] Basic run-time language support (e.g., for allocation and run-time type information); see §16.1.3. [2] The C standard library (with very minor modifications to minimize violations of the type system); see §16.1.2. [3] Strings and I/O streams (with support for international character sets and localization); see Chapter 20 and Chapter 21. [4] A framework of containers (such as v ec to r, l is t, and m ap and algorithms using containers ve ct or li st ma p) (such as general traversals, sorts, and merges); see Chapter 16, Chapter 17, Chapter 18, and Chapter 19. [5] Support for numerical computation (complex numbers plus vectors with arithmetic opera- tions, BLAS-like and generalized slices, and semantics designed to ease optimization); see Chapter 22. The main criterion for including a class in the library was that it would somehow be used by almost every C++ programmer (both novices and experts), that it could be provided in a general form that did not add significant overhead compared to a simpler version of the same facility, and that simple uses should be easy to learn. Essentially, the C++ standard library provides the most common fun- damental data structures together with the fundamental algorithms used on them. Every algorithm works with every container without the use of conversions. This framework, conventionally called the STL [Stepanov,1994], is extensible in the sense that users can easily pro- vide containers and algorithms in addition to the ones provided as part of the standard and have these work directly with the standard containers and algorithms. 3.11 Advice [tour2.advice] [1] Don’t reinvent the wheel; use libraries. [2] Don’t believe in magic; understand what your libraries do, how they do it, and at what cost they do it. [3] When you have a choice, prefer the standard library to other libraries. [4] Do not think that the standard library is ideal for everything. [5] Remember to #i nc lu de the headers for the facilities you use; §3.3. in cl ud e [6] Remember that standard library facilities are defined in namespace s td §3.3. st d; [7] Use s tr in g rather than c ha r*; §3.5, §3.6. st ri ng ch ar [8] If in doubt use a range-checked vector (such as V ec §3.7.2. Ve c); [9] Prefer v ec to r<T l is t<T and m ap ke y,v al ue to T ve ct or T>, li st T>, ma p<k ey va lu e> T[]; §3.7.1, §3.7.3, §3.7.4. pu sh _b ac k() ba ck _i ns er te r(); §3.7.3, §3.8. [10] When adding elements to a container, use p us h_ ba ck or b ac k_ in se rt er pu sh _b ac k() [11] Use p us h_ ba ck on a v ec to r rather than r ea ll oc on an array; §3.8. ve ct or re al lo c() [12] Catch common exceptions in m ai n(); §3.7.2. ma in
    77. Part I Basic Facilities This part describes C++’s built-in types and the basic facilities for constructing pro- grams out of them. The C subset of C++ is presented together with C++’s additional support for traditional styles of programming. It also discusses the basic facilities for composing a C++ program out of logical and physical parts. Chapters 4 Types and Declarations 5 Pointers, Arrays, and Structures 6 Expressions and Statements 7 Functions 8 Namespaces and Exceptions 9 Source Files and Programs
    78. 68 Basic Facilities Part I
    79. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 4 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Types and Declarations Accept nothing short of perfection! – anon Perfection is achieved only on the point of collapse. – C. N. Parkinson Types — fundamental types — Booleans — characters — character literals — integers — integer literals — floating-point types — floating-point literals — sizes — v oi d — vo id enumerations — declarations — names — scope — initialization — objects — t yp ed ef ty pe de fs — advice — exercises. 4.1 Types [dcl.type] Consider x = y f(2 ; y+f 2) For this to make sense in a C++ program, the names x y and f must be suitably declared. That is, x, y, the programmer must specify that entities named x y and f exist and that they are of types for x, y, which = (assignment), + (addition), and () (function call), respectively, are meaningful. Every name (identifier) in a C++ program has a type associated with it. This type determines what operations can be applied to the name (that is, to the entity referred to by the name) and how such operations are interpreted. For example, the declarations f lo at x fl oa t x; // x is a floating-point variable i nt y = 7 in t 7; // y is an integer variable with the initial value 7 f lo at f in t); fl oa t f(i nt // f is a function taking an argument of type int and returning a floating-point number
    80. 70 Types and Declarations Chapter 4 would make the example meaningful. Because y is declared to be an i nt it can be assigned to, used in t, in arithmetic expressions, etc. On the other hand, f is declared to be a function that takes an i nt as in t its argument, so it can be called given a suitable argument. This chapter presents fundamental types (§4.1.1) and declarations (§4.9). Its examples just demonstrate language features; they are not intended to do anything useful. More extensive and realistic examples are saved for later chapters after more of C++ has been described. This chapter simply provides the most basic elements from which C++ programs are constructed. You must know these elements, plus the terminology and simple syntax that goes with them, in order to com- plete a real project in C++ and especially to read code written by others. However, a thorough understanding of every detail mentioned in this chapter is not a requirement for understanding the following chapters. Consequently, you may prefer to skim through this chapter, observing the major concepts, and return later as the need for understanding of more details arises. 4.1.1 Fundamental Types [dcl.fundamental] C++ has a set of fundamental types corresponding to the most common basic storage units of a computer and the most common ways of using them to hold data: §4.2 A Boolean type (b oo l) bo ol §4.3 Character types (such as c ha r) ch ar §4.4 Integer types (such as i nt in t) §4.5 Floating-point types (such as d ou bl e) do ub le In addition, a user can define §4.8 Enumeration types for representing specific sets of values (e nu m) en um There also is §4.7 A type, v oi d, used to signify the absence of information vo id From these types, we can construct other types: §5.1 Pointer types (such as i nt in t*) §5.2 Array types (such as c ha r[]) ch ar §5.5 Reference types (such as d ou bl e&) do ub le §5.7 Data structures and classes (Chapter 10) The Boolean, character, and integer types are collectively called integral types. The integral and floating-point types are collectively called arithmetic types. Enumerations and classes (Chapter 10) are called user-defined types because they must be defined by users rather than being available for use without previous declaration, the way fundamental types are. In contrast, other types are called built-in types. The integral and floating-point types are provided in a variety of sizes to give the programmer a choice of the amount of storage consumed, the precision, and the range available for computations (§4.6). The assumption is that a computer provides bytes for holding characters, words for holding and computing integer values, some entity most suitable for floating-point computation, and addresses for referring to those entities. The C++ fundamental types together with pointers and arrays present these machine-level notions to the programmer in a reasonably implementation- independent manner. For most applications, one could simply use b oo l for logical values, c ha r for characters, i nt for bo ol ch ar in t integer values, and d ou bl e for floating-point values. The remaining fundamental types are do ub le
    81. Section 4.1.1 Fundamental Types 71 variations for optimizations and special needs that are best ignored until such needs arise. They must be known, however, to read old C and C++ code. 4.2 Booleans [dcl.bool] A Boolean, b oo l, can have one of the two values t ru e or f al se A Boolean is used to express the bo ol tr ue fa ls e. results of logical operations. For example: v oi d f in t a i nt b vo id f(i nt a, in t b) { b oo l b 1 = a b; bo ol b1 a==b // = is assignment, == is equality // ... } If a and b have the same value, b 1 becomes t ru e; otherwise, b 1 becomes f al se b1 tr ue b1 fa ls e. A common use of b oo l is as the type of the result of a function that tests some condition (a bo ol predicate). For example: bo ol is _o pe n(F il e*) b oo l i s_ op en Fi le ; b oo l g re at er in t a i nt b { r et ur n a b; } bo ol gr ea te r(i nt a, in t b) re tu rn a>b By definition, t ru e has the value 1 when converted to an integer and f al se has the value 0 Con- tr ue fa ls e 0. versely, integers can be implicitly converted to b oo l values: nonzero integers convert to t ru e and 0 bo ol tr ue converts to f al se For example: fa ls e. b oo l b = 7 bo ol 7; // bool(7) is true, so b becomes true i nt i = t ru e; in t tr ue // int(true) is 1, so i becomes 1 In arithmetic and logical expressions, b oo ls are converted to i nt integer arithmetic and logical bo ol in ts; operations are performed on the converted values. If the result is converted back to b oo l, a 0 is bo ol converted to f al se and a nonzero value is converted to t ru e. fa ls e tr ue v oi d g vo id g() { b oo l a = t ru e; bo ol tr ue b oo l b = t ru e; bo ol tr ue b oo l x = a b; // a+b is 2, so x becomes true bo ol a+b b oo l y = a b; // ab is 1, so y becomes true bo ol a|b } A pointer can be implicitly converted to a b oo l (§C.6.2.5). A nonzero pointer converts to t ru e; bo ol tr ue zero-valued pointers convert to f al se fa ls e. 4.3 Character Types [dcl.char] A variable of type c ha r can hold a character of the implementation’s character set. For example: ch ar c ha r c h = ´a ch ar ch a´;
    82. 72 Types and Declarations Chapter 4 Almost universally, a c ha r has 8 bits so that it can hold one of 256 different values. Typically, the ch ar character set is a variant of ISO-646, for example ASCII, thus providing the characters appearing on your keyboard. Many problems arise from the fact that this set of characters is only partially standardized (§C.3). Serious variations occur between character sets supporting different natural languages and also between different character sets supporting the same natural language in different ways. However, here we are interested only in how such differences affect the rules of C++. The larger and more interesting issue of how to program in a multi-lingual, multi-character-set environment is beyond the scope of this book, although it is alluded to in several places (§20.2, §21.7, §C.3.3). It is safe to assume that the implementation character set includes the decimal digits, the 26 alphabetic characters of English, and some of the basic punctuation characters. It is not safe to assume that there are no more than 127 characters in an 8-bit character set (e.g., some sets provide 255 characters), that there are no more alphabetic characters than English provides (most European languages provide more), that the alphabetic characters are contiguous (EBCDIC leaves a gap j´), or that every character used to write C++ is available (e.g., some national between ´i and ´j i´ character sets do not provide { } [ ] | \\ §C.3.1). Whenever possible, we should avoid making \\; assumptions about the representation of objects. This general rule applies even to characters. Each character constant has an integer value. For example, the value of ´b is 9 8 in the ASCII b´ 98 character set. Here is a small program that will tell you the integer value of any character you care to input: #i nc lu de <i os tr ea m> in cl ud e io st re am i nt m ai n() in t ma in { c ha r c ch ar c; s td :c in >> c st d: ci n c; s td :c ou t << \"t he v al ue o f ´\" << c << \"´ i s \" << i nt c) << ´\\ n´; st d: co ut th e va lu e of is in t(c \\n } The notation i nt c) gives the integer value for a character c The possibility of converting a c ha r in t(c c. ch ar to an integer raises the question: is a c ha r signed or unsigned? The 256 values represented by an ch ar 8-bit byte can be interpreted as the values 0 to 2 55 or as the values -1 27 to 1 27 Unfortunately, 25 5 12 7 12 7. which choice is made for a plain c ha r is implementation-defined (§C.1, §C.3.4). C++ provides two ch ar types for which the answer is definite; s ig ne d c ha r, which can hold at least the values -1 27 to 1 27 si gn ed ch ar 12 7 12 7, and u ns ig ne d c ha r, which can hold at least the values 0 to 2 55 Fortunately, the difference matters un si gn ed ch ar 25 5. only for values outside the 0 to 1 27 range, and the most common characters are within that range. 12 7 Values outside that range stored in a plain c ha r can lead to subtle portability problems. See ch ar §C.3.4 if you need to use more than one type of c ha r or if you store integers in c ha r variables. ch ar ch ar A type w ch ar _t is provided to hold characters of a larger character set such as Unicode. It is a wc ha r_ t distinct type. The size of w ch ar _t is implementation-defined and large enough to hold the largest wc ha r_ t character set supported by the implementation’s locale (see §21.7, §C.3.3). The strange name is a leftover from C. In C, w ch ar _t is a t yp ed ef (§4.9.7) rather than a built-in type. The suffix _ t was _t wc ha r_ t ty pe de f added to distinguish standard t yp ed ef ty pe de fs. Note that the character types are integral types (§4.1.1) so that arithmetic and logical operations (§6.2) apply.
    83. Section 4.3.1 Character Literals 73 4.3.1 Character Literals [dcl.char.lit] A character literal, often called a character constant, is a character enclosed in single quotes, for example, ´a and ´0 The type of a character literal is c ha r. Such character literals are really a´ 0´. ch ar symbolic constants for the integer value of the characters in the character set of the machine on which the C++ program is to run. For example, if you are running on a machine using the ASCII character set, the value of ´0 is 4 8. The use of character literals rather than decimal notation 0´ 48 makes programs more portable. A few characters also have standard names that use the backslash \\ as an escape character. For example, \\ n is a newline and \\ t is a horizontal tab. See §C.3.2 for \\n \\t details about escape characters. Wide character literals are of the form L ab where the number of characters between the L´a b´, quotes and their meanings is implementation-defined to match the w ch ar _t type. A wide character wc ha r_ t literal has type w ch ar _t wc ha r_ t. 4.4 Integer Types [dcl.int] Like c ha r, each integer type comes in three forms: ‘‘plain’’ i nt s ig ne d i nt and u ns ig ne d i nt In ch ar in t, si gn ed in t, un si gn ed in t. addition, integers come in three sizes: s ho rt i nt ‘‘plain’’ i nt and l on g i nt A l on g i nt can be sh or t in t, in t, lo ng in t. lo ng in t referred to as plain l on g. Similarly, s ho rt is a synonym for s ho rt i nt u ns ig ne d for u ns ig ne d i nt lo ng sh or t sh or t in t, un si gn ed un si gn ed in t, and s ig ne d for s ig ne d i nt si gn ed si gn ed in t. The u ns ig ne d integer types are ideal for uses that treat storage as a bit array. Using an un si gn ed u ns ig ne d instead of an i nt to gain one more bit to represent positive integers is almost never a good un si gn ed in t idea. Attempts to ensure that some values are positive by declaring variables u ns ig ne d will typi- un si gn ed cally be defeated by the implicit conversion rules (§C.6.1, §C.6.2.1). Unlike plain c ha rs, plain i nt are always signed. The signed i nt types are simply more explicit ch ar in ts in t synonyms for their plain i nt counterparts. in t 4.4.1 Integer Literals [dcl.int.lit] Integer literals come in four guises: decimal, octal, hexadecimal, and character literals. Decimal lit- erals are the most commonly used and look as you would expect them to: 0 1 23 4 12 34 9 76 97 6 1 23 45 67 89 01 23 45 67 89 0 12 34 56 78 90 12 34 56 78 90 The compiler ought to warn about literals that are too long to represent. A literal starting with zero followed by x (0 x) is a hexadecimal (base 16) number. A literal 0x starting with zero followed by a digit is an octal (base 8) number. For example: d ec im al de ci ma l: 0 2 63 63 83 83 o ct al oc ta l: 00 00 02 02 0 77 07 7 0 12 3 01 23 h ex ad ec im al he xa de ci ma l: 0 x0 0x 0 0 x2 0x 2 0 x3 f 0x 3f 0 x5 3 0x 53 The letters a b c d e and f or their uppercase equivalents, are used to represent 1 0, 1 1, 1 2, 1 3, a, b, c, d, e, f, 10 11 12 13 1 4, and 1 5, respectively. Octal and hexadecimal notations are most useful for expressing bit pat- 14 15 terns. Using these notations to express genuine numbers can lead to surprises. For example, on a machine on which an i nt is represented as a two’s complement 16-bit integer, 0 xf ff f is the negative in t 0x ff ff decimal number -1 Had more bits been used to represent an integer, it would have been 6 55 35 1. 65 53 5.
    84. 74 Types and Declarations Chapter 4 The suffix U can be used to write explicitly u ns ig ne d literals. Similarly, the suffix L can be un si gn ed used to write explicitly l on g literals. For example, 3 is an i nt 3U is an u ns ig ne d i nt and 3 L is a lo ng in t, un si gn ed in t, 3L l on g i nt If no suffix is provided, the compiler gives an integer literal a suitable type based on its lo ng in t. value and the implementation’s integer sizes (§C.4). It is a good idea to limit the use of nonobvious constants to a few well-commented c on st (§5.4) co ns t or enumerator (§4.8) initializers. 4.5 Floating-Point Types [dcl.float] The floating-point types represent floating-point numbers. Like integers, floating-point types come in three sizes: f lo at (single-precision), d ou bl e (double-precision), and l on g d ou bl e (extended- fl oa t do ub le lo ng do ub le precision). The exact meaning of single-, double-, and extended-precision is implementation-defined. Choosing the right precision for a problem where the choice matters requires significant under- standing of floating-point computation. If you don’t have that understanding, get advice, take the time to learn, or use d ou bl e and hope for the best. do ub le 4.5.1 Floating-Point Literals [dcl.fp.lit] By default, a floating-point literal is of type d ou bl e. Again, a compiler ought to warn about do ub le floating-point literals that are too large to be represented. Here are some floating-point literals: 1 23 1.2 3 .2 3 23 0 23 0.2 3 1 1. 10 1.0 1 2e 10 1.2 e1 0 1 23 e-1 5 1.2 3e 15 Note that a space cannot occur in the middle of a floating-point literal. For example, 6 5.4 3 e 21 65 43 e-2 1 is not a floating-point literal but rather four separate lexical tokens (causing a syntax error): 6 5.4 3 65 43 e 21 21 - If you want a floating-point literal of type f lo at you can define one using the suffix f or F fl oa t, F: 3 14 15 92 65 f 3.1 41 59 26 5f 2 0f 2.0 f 2 99 79 25 F 2.9 97 92 5F 4.6 Sizes [dcl.size] Some of the aspects of C++’s fundamental types, such as the size of an i nt are implementation- in t, defined (§C.2). I point out these dependencies and often recommend avoiding them or taking steps to minimize their impact. Why should you bother? People who program on a variety of systems or use a variety of compilers care a lot because if they don’t, they are forced to waste time finding and fixing obscure bugs. People who claim they don’t care about portability usually do so because they use only a single system and feel they can afford the attitude that ‘‘the language is what my com- piler implements.’’ This is a narrow and shortsighted view. If your program is a success, it is likely to be ported, so someone will have to find and fix problems related to implementation- dependent features. In addition, programs often need to be compiled with other compilers for the same system, and even a future release of your favorite compiler may do some things differently from the current one. It is far easier to know and limit the impact of implementation dependencies
    85. Section 4.6 Sizes 75 when a program is written than to try to untangle the mess afterwards. It is relatively easy to limit the impact of implementation-dependent language features. Limit- ing the impact of system-dependent library facilities is far harder. Using standard library facilities wherever feasible is one approach. The reason for providing more than one integer type, more than one unsigned type, and more than one floating-point type is to allow the programmer to take advantage of hardware characteris- tics. On many machines, there are significant differences in memory requirements, memory access times, and computation speed between the different varieties of fundamental types. If you know a machine, it is usually easy to choose, for example, the appropriate integer type for a particular vari- able. Writing truly portable low-level code is harder. Sizes of C++ objects are expressed in terms of multiples of the size of a c ha r, so by definition ch ar the size of a c ha r is 1 The size of an object or type can be obtained using the s iz eo f operator ch ar 1. si ze of (§6.2). This is what is guaranteed about sizes of fundamental types: 1 ≡ sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) 1 ≤ sizeof(bool) ≤ sizeof(long) sizeof(char) ≤ sizeof(wchar_t) ≤ sizeof(long) sizeof(float) ≤ sizeof(double) ≤ sizeof(long double) sizeof(N) ≡ sizeof(signed N) ≡ sizeof(unsigned N) where N can be c ha r, s ho rt i nt i nt or l on g i nt In addition, it is guaranteed that a c ha r has at least ch ar sh or t in t, in t, lo ng in t. ch ar 8 bits, a s ho rt at least 16 bits, and a l on g at least 32 bits. A c ha r can hold a character of the sh or t lo ng ch ar machine’s character set. Here is a graphical representation of a plausible set of fundamental types and a sample string: char: ’a’ bool: 1 short: 756 int: 100000000 int*: &c1 double: 1234567e34 char[14]: Hello, world!\\0 On the same scale (.2 inch to a byte), a megabyte of memory would stretch about three miles (five km) to the right.
    86. 76 Types and Declarations Chapter 4 The c ha r type is supposed to be chosen by the implementation to be the most suitable type for ch ar holding and manipulating characters on a given computer; it is typically an 8-bit byte. Similarly, the i nt type is supposed to be chosen to be the most suitable for holding and manipulating integers in t on a given computer; it is typically a 4-byte (32-bit) word. It is unwise to assume more. For exam- ple, there are machines with 32 bit c ha rs. ch ar When needed, implementation-dependent aspects about an implementation can be found in <l im it s> (§22.2). For example: li mi ts #i nc lu de <l im it s> in cl ud e li mi ts i nt m ai n() in t ma in { c ou t << \"l ar ge st f lo at == \" << n um er ic _l im it s<f lo at :m ax co ut la rg es t fl oa t nu me ri c_ li mi ts fl oa t>: ma x() << \", c ha r i s s ig ne d == \" << n um er ic _l im it s<c ha r>::i s_ si gn ed << ´\\ n´; is _s ig ne d ch ar is si gn ed nu me ri c_ li mi ts ch ar \\n } The fundamental types can be mixed freely in assignments and expressions. Wherever possible, values are converted so as not to lose information (§C.6). If a value v can be represented exactly in a variable of type T a conversion of v to T is value- T, preserving and no problem. The cases where conversions are not value-preserving are best avoided (§C.6.2.6). You need to understand implicit conversion in some detail in order to complete a major project and especially to understand real code written by others. However, such understanding is not required to read the following chapters. 4.7 Void [dcl.void] The type v oi d is syntactically a fundamental type. It can, however, be used only as part of a more vo id complicated type; there are no objects of type v oi d. It is used either to specify that a function does vo id not return a value or as the base type for pointers to objects of unknown type. For example: v oi d x vo id x; // error: there are no void objects v oi d f ; vo id f() // function f does not return a value (§7.3) v oi d* p v; vo id pv // pointer to object of unknown type (§5.6) When declaring a function, you must specify the type of the value returned. Logically, you would expect to be able to indicate that a function didn’t return a value by omitting the return type. How- ever, that would make the grammar (Appendix A) less regular and clash with C usage. Conse- quently, v oi d is used as a ‘‘pseudo return type’’ to indicate that a function doesn’t return a value. vo id 4.8 Enumerations [dcl.enum] An enumeration is a type that can hold a set of values specified by the user. Once defined, an enu- meration is used very much like an integer type. Named integer constants can be defined as members of an enumeration. For example, e nu m { A SM A UT O, B RE AK }; en um AS M, AU TO BR EA K
    87. Section 4.8 Enumerations 77 defines three integer constants, called enumerators, and assigns values to them. By default, enu- merator values are assigned increasing from 0 so A SM 0, A UT O==1 and B RE AK 2. An enu- 0, AS M==0 AU TO 1, BR EA K==2 meration can be named. For example: e nu m k ey wo rd { A SM A UT O, B RE AK }; en um ke yw or d AS M, AU TO BR EA K Each enumeration is a distinct type. The type of an enumerator is its enumeration. For example, A UT O is of type k ey wo rd AU TO ke yw or d. Declaring a variable k ey wo rd instead of plain i nt can give both the user and the compiler a hint ke yw or d in t as to the intended use. For example: v oi d f ke yw or d k ey vo id f(k ey wo rd ke y) { s wi tc h (k ey { sw it ch ke y) c as e A SM ca se AS M: // do something b re ak br ea k; c as e B RE AK ca se BR EA K: // do something b re ak br ea k; } } A compiler can issue a warning because only two out of three k ey wo rd values are handled. ke yw or d An enumerator can be initialized by a constant-expression (§C.5) of integral type (§4.1.1). The range of an enumeration holds all the enumeration’s enumerator values rounded up to the nearest larger binary power minus 1 The range goes down to 0 if the smallest enumerator is non-negative 1. and to the nearest lesser negative binary power if the smallest enumerator is negative. This defines the smallest bit-field capable of holding the enumerator values. For example: e nu m e 1 { d ar k, l ig ht }; en um e1 da rk li gh t // range 0:1 e nu m e 2 { a = 3 b = 9 }; en um e2 3, // range 0:15 e nu m e 3 { m in = -1 0, m ax = 1 00 00 00 }; // range -1048576:1048575 en um e3 mi n 10 ma x 10 00 00 0 A value of integral type may be explicitly converted to an enumeration type. The result of such a conversion is undefined unless the value is within the range of the enumeration. For example: e nu m f la g { x 1, y 2, z 4, e 8 }; // range 0:15 en um fl ag x=1 y=2 z=4 e=8 f la g f 1 = 5 fl ag f1 5; // type error: 5 is not of type flag f la g f 2 = f la g(5 ; fl ag f2 fl ag 5) // ok: flag(5) is of type flag and within the range of flag f la g f 3 = f la g(z e); // ok: flag(12) is of type flag and within the range of flag fl ag f3 fl ag z|e f la g f 4 = f la g(9 9); // undefined: 99 is not within the range of flag fl ag f4 fl ag 99 The last assignment shows why there is no implicit conversion from an integer to an enumeration; most integer values do not have a representation in a particular enumeration. The notion of a range of values for an enumeration differs from the enumeration notion in the Pascal family of languages. However, bit-manipulation examples that require values outside the set of enumerators to be well-defined have a long history in C and C++.
    88. 78 Types and Declarations Chapter 4 The s iz eo f an enumeration is the s iz eo f some integral type that can hold its range and not larger si ze of si ze of than s iz eo f(i nt unless an enumerator cannot be represented as an i nt or as an u ns ig ne d i nt For si ze of in t), in t un si gn ed in t. example, s iz eo f(e 1) could be 1 or maybe 4 but not 8 on a machine where s iz eo f(i nt si ze of e1 si ze of in t)==4 4. By default, enumerations are converted to integers for arithmetic operations (§6.2). An enumer- ation is a user-defined type, so users can define their own operations, such as ++ and << for an enu- meration (§11.2.3). 4.9 Declarations [dcl.dcl] Before a name (identifier) can be used in a C++ program, it must be declared. That is, its type must be specified to inform the compiler to what kind of entity the name refers. Here are some examples illustrating the diversity of declarations: c ha r c h; ch ar ch s tr in g s st ri ng s; i nt c ou nt = 1 in t co un t 1; c on st d ou bl e p i = 3 14 15 92 65 35 89 79 32 38 5; co ns t do ub le pi 3.1 41 59 26 53 58 97 93 23 85 e xt er n i nt e rr or _n um be r; ex te rn in t er ro r_ nu mb er c ha r* n am e = \"N ja l\"; ch ar na me Nj al c ha r* s ea so n[] = { \"s pr in g\", \"s um me r\", \"f al l\", \"w in te r\" }; ch ar se as on sp ri ng su mm er fa ll wi nt er s tr uc t D at e { i nt d m y }; st ru ct Da te in t d, m, y; i nt d ay Da te p { r et ur n p d; } in t da y(D at e* p) re tu rn p->d d ou bl e s qr t(d ou bl e); do ub le sq rt do ub le t em pl at e<c la ss T T a bs T a { r et ur n a 0 ? -a : a } te mp la te cl as s T> ab s(T a) re tu rn a<0 a a; t yp ed ef c om pl ex sh or t> P oi nt ty pe de f co mp le x<s ho rt Po in t; s tr uc t U se r; st ru ct Us er e nu m B ee r { C ar ls be rg T ub or g, T ho r }; en um Be er Ca rl sb er g, Tu bo rg Th or n am es pa ce N S { i nt a } na me sp ac e NS in t a; As can be seen from these examples, a declaration can do more than simply associate a type with a name. Most of these declarations are also definitions; that is, they also define an entity for the name to which they refer. For c h, that entity is the appropriate amount of memory to be used as a ch variable – that memory will be allocated. For d ay it is the specified function. For the constant p i, da y, pi it is the value 3 14 15 92 65 35 89 79 32 38 5. For D at e, that entity is a new type. For P oi nt it is the 3.1 41 59 26 53 58 97 93 23 85 Da te Po in t, type c om pl ex sh or t> so that P oi nt becomes a synonym for c om pl ex sh or t>. Of the declarations co mp le x<s ho rt Po in t co mp le x<s ho rt above, only d ou bl e s qr t(d ou bl e); do ub le sq rt do ub le e xt er n i nt e rr or _n um be r; ex te rn in t er ro r_ nu mb er s tr uc t U se r; st ru ct Us er are not also definitions; that is, the entity they refer to must be defined elsewhere. The code (body) for the function s qr t must be specified by some other declaration, the memory for the i nt variable sq rt in t e rr or _n um be r must be allocated by some other declaration of e rr or _n um be r, and some other er ro r_ nu mb er er ro r_ nu mb er declaration of the type U se r must define what that type looks like. For example: Us er
    89. Section 4.9 Declarations 79 d ou bl e s qr t(d ou bl e d { /* ... */ } do ub le sq rt do ub le d) i nt e rr or _n um be r = 1 in t er ro r_ nu mb er 1; s tr uc t U se r { /* ... */ }; st ru ct Us er There must always be exactly one definition for each name in a C++ program (for the effects of #i nc lu de see §9.2.3). However, there can be many declarations. All declarations of an entity must in cl ud e, agree on the type of the entity referred to. So, this fragment has two errors: i nt c ou nt in t co un t; i nt c ou nt // error: redefinition in t co un t; e xt er n i nt e rr or _n um be r; ex te rn in t er ro r_ nu mb er e xt er n s ho rt e rr or _n um be r; ex te rn sh or t er ro r_ nu mb er // error: type mismatch and this has none (for the use of e xt er n see §9.2): ex te rn e xt er n i nt e rr or _n um be r; ex te rn in t er ro r_ nu mb er e xt er n i nt e rr or _n um be r; ex te rn in t er ro r_ nu mb er Some definitions specify a ‘‘value’’ for the entities they define. For example: s tr uc t D at e { i nt d m y }; st ru ct Da te in t d, m, y; t yp ed ef c om pl ex sh or t> P oi nt ty pe de f co mp le x<s ho rt Po in t; i nt d ay Da te p { r et ur n p d; } in t da y(D at e* p) re tu rn p->d c on st d ou bl e p i = 3 14 15 92 65 35 89 79 32 38 5; co ns t do ub le pi 3.1 41 59 26 53 58 97 93 23 85 For types, templates, functions, and constants, the ‘‘value’’ is permanent. For nonconstant data types, the initial value may be changed later. For example: v oi d f vo id f() { i nt c ou nt = 1 in t co un t 1; c ha r* n am e = \"B ja rn e\"; ch ar na me Bj ar ne // ... c ou nt = 2 co un t 2; n am e = \"M ar ia n\"; na me Ma ri an } Of the definitions, only c ha r c h; ch ar ch s tr in g s st ri ng s; do not specify values. See §4.9.5 and §10.4.2 for explanations of how and when a variable is assigned a default value. Any declaration that specifies a value is a definition. 4.9.1 The Structure of a Declaration [dcl.parts] A declaration consists of four parts: an optional ‘‘specifier,’’ a base type, a declarator, and an optional initializer. Except for function and namespace definitions, a declaration is terminated by a semicolon. For example:
    90. 80 Types and Declarations Chapter 4 c ha r* k in gs = { \"A nt ig on us \"S el eu cu s\", \"P to le my }; ch ar ki ng s[] An ti go nu s\", Se le uc us Pt ol em y\" Here, the base type is c ha r, the declarator is *k in gs ch ar ki ng s[], and the initializer is ={...}. A specifier is an initial keyword, such as v ir tu al (§2.5.5, §12.2.6) and e xt er n (§9.2), that speci- vi rt ua l ex te rn fies some non-type attribute of what is being declared. A declarator is composed of a name and optionally some declarator operators. The most com- mon declarator operators are (§A.7.1): p oi nt er po in te r p re fi x pr ef ix * *c on st co ns t c on st an t p oi nt er co ns ta nt po in te r p re fi x pr ef ix r ef er en ce re fe re nc e p re fi x pr ef ix & a rr ay ar ra y p os tf ix po st fi x [] f un ct io n fu nc ti on p os tf ix po st fi x () Their use would be simple if they were all either prefix or postfix. However, *, [], and () were designed to mirror their use in expressions (§6.2). Thus, * is prefix and [] and () are postfix. The postfix declarator operators bind tighter than the prefix ones. Consequently, *k in gs ki ng s[] is a vector of pointers to something, and we have to use parentheses to express types such as ‘‘pointer to function;’’ see examples in §5.1. For full details, see the grammar in Appendix A. Note that the type cannot be left out of a declaration. For example: c on st c = 7 co ns t 7; // error: no type g t(i nt a i nt b { r et ur n (a b) ? a : b } // error: no return type gt in t a, in t b) re tu rn a>b b; u ns ig ne d u i; un si gn ed ui // ok: ‘unsigned’ is the type ‘unsigned int’ l on g l i; lo ng li // ok: ‘long’ is the type ‘long int’ In this, standard C++ differs from earlier versions of C and C++ that allowed the first two examples by considering i nt to be the type when none were specified (§B.2). This ‘‘implicit i nt rule was a in t in t’’ source of subtle errors and confusion. 4.9.2 Declaring Multiple Names [dcl.multi] It is possible to declare several names in a single declaration. The declaration simply contains a list of comma-separated declarators. For example, we can declare two integers like this: i nt x y in t x, y; // int x; int y; Note that operators apply to individual names only – and not to any subsequent names in the same declaration. For example: i nt p y in t* p, y; // int* p; int y; NOT int* y; i nt x *q in t x, q; // int x; int* q; i nt v 10 , *p v; in t v[1 0] pv // int v[10]; int* pv; Such constructs make a program less readable and should be avoided.
    91. Section 4.9.3 Names 81 4.9.3 Names [dcl.name] A name (identifier) consists of a sequence of letters and digits. The first character must be a letter. The underscore character _ is considered a letter. C++ imposes no limit on the number of charac- ters in a name. However, some parts of an implementation are not under the control of the com- piler writer (in particular, the linker), and those parts, unfortunately, sometimes do impose limits. Some run-time environments also make it necessary to extend or restrict the set of characters accepted in an identifier. Extensions (e.g., allowing the character $ in a name) yield nonportable programs. A C++ keyword (Appendix A), such as n ew and i nt cannot be used as a name of a ne w in t, user-defined entity. Examples of names are: t hi s_ is _a _m os t_ un us ua ll y_ lo ng _n am e th is _i s_ a_ mo st _u nu su al ly _l on g_ na me h el lo he ll o u _n am e D EF IN ED DE FI NE D f oO fo O b Ar bA r u_ na me H or se Se ns e Ho rs eS en se _ cl as s _c la ss _ __ __ _ v ar 0 va r0 v ar 1 va r1 C LA SS CL AS S Examples of character sequences that cannot be used as identifiers are: 0 12 01 2 a f oo l fo ol $s ys sy s c la ss cl as s 3 va r 3v ar p ay du e pa y.d ue f oo ba r fo o~b ar .n am e na me if if Names starting with an underscore are reserved for special facilities in the implementation and the run-time environment, so such names should not be used in application programs. When reading a program, the compiler always looks for the longest string of characters that could make up a name. Hence, v ar 10 is a single name, not the name v ar followed by the number va r1 0 va r 1 0. Also, e ls ei f is a single name, not the keyword e ls e followed by the keyword i f. 10 el se if el se if Uppercase and lowercase letters are distinct, so C ou nt and c ou nt are different names, but it is Co un t co un t unwise to choose names that differ only by capitalization. In general, it is best to avoid names that differ only in subtle ways. For example, the uppercase o (O and zero (0 can be hard to tell apart, O) 0) as can the lowercase L (l and one (1 Consequently, l 0, l O, l 1, and l l are poor choices for identi- l) 1). l0 lO l1 ll fier names. Names from a large scope ought to have relatively long and reasonably obvious names, such as v ec to r, W in do w_ wi th _b or de r, and D ep ar tm en t_ nu mb er However, code is clearer if names used ve ct or Wi nd ow _w it h_ bo rd er De pa rt me nt _n um be r. only in a small scope have short, conventional names such as x i and p Classes (Chapter 10) and x, i, p. namespaces (§8.2) can be used to keep scopes small. It is often useful to keep frequently used names relatively short and reserve really long names for infrequently used entities. Choose names to reflect the meaning of an entity rather than its implementation. For example, p ho ne _b oo k is bet- ph on e_ bo ok nu mb er _l is t ter than n um be r_ li st even if the phone numbers happen to be stored in a l is t (§3.7). Choosing good li st names is an art. Try to maintain a consistent naming style. For example, capitalize nonstandard library user- defined types and start nontypes with a lowercase letter (for example, S ha pe and c ur re nt _t ok en Sh ap e cu rr en t_ to ke n). Also, use all capitals for macros (if you must use macros; for example, H AC K) and use underscores HA CK to separate words in an identifier. However, consistency is hard to achieve because programs are typically composed of fragments from different sources and several different reasonable styles are in use. Be consistent in your use of abbreviations and acronyms.
    92. 82 Types and Declarations Chapter 4 4.9.4 Scope [dcl.scope] A declaration introduces a name into a scope; that is, a name can be used only in a specific part of the program text. For a name declared in a function (often called a local name), that scope extends from its point of declaration to the end of the block in which its declaration occurs. A block is a section of code delimited by a { } pair. A name is called global if it is defined outside any function, class (Chapter 10), or namespace (§8.2). The scope of a global name extends from the point of declaration to the end of the file in which its declaration occurs. A declaration of a name in a block can hide a declaration in an enclosing block or a global name. That is, a name can be redefined to refer to a different entity within a block. After exit from the block, the name resumes its previous meaning. For example: i nt x in t x; // global x v oi d f vo id f() { i nt x in t x; // local x hides global x x=1 1; // assign to local x { i nt x in t x; // hides first local x x=2 2; // assign to second local x } x=3 3; // assign to first local x } i nt p = &x in t* x; // take address of global x Hiding names is unavoidable when writing large programs. However, a human reader can easily fail to notice that a name has been hidden. Because such errors are relatively rare, they can be very difficult to find. Consequently, name hiding should be minimized. Using names such as i and x for global variables or for local variables in a large function is asking for trouble. A hidden global name can be referred to using the scope resolution operator ::. For example: i nt x in t x; v oi d f 2() vo id f2 { i nt x = 1 // hide global x in t 1; ::x = 2 x 2; // assign to global x x=2 2; // assign to local x // ... } There is no way to use a hidden local name. The scope of a name starts at its point of declaration; that is, after the complete declarator and before the initializer. This implies that a name can be used even to specify its own initial value. For example: i nt x in t x;
    93. Section 4.9.4 Scope 83 v oi d f 3() vo id f3 { i nt x = x // perverse: initialize x with its own (uninitialized) value in t x; } This is not illegal, just silly. A good compiler will warn if a variable is used before it has been set (see also §5.9[9]). It is possible to use a single name to refer to two different objects in a block without using the :: operator. For example: i nt x = 1 1; in t 11 v oi d f 4() vo id f4 // perverse: { i nt y = x in t x; // use global x: y = 11 i nt x = 2 2; in t 22 y=x x; // use local x: y = 22 } Function argument names are considered declared in the outermost block of a function, so v oi d f 5(i nt x vo id f5 in t x) { i nt x in t x; // error } is an error because x is defined twice in the same scope. Having this be an error allows a not uncommon, subtle mistake to be caught. 4.9.5 Initialization [dcl.init] If an initializer is specified for an object, that initializer determines the initial value of an object. If no initializer is specified, a global (§4.9.4), namespace (§8.2), or local static object (§7.1.2, §10.2.4) (collectively called static objects) is initialized to 0 of the appropriate type. For example: i nt a in t a; // means ‘‘int a = 0;’’ d ou bl e d do ub le d; // means ‘‘double d = 0.0;’’ Local variables (sometimes called automatic objects) and objects created on the free store (some- times called dynamic objects or heap objects) are not initialized by default. For example: v oi d f vo id f() { i nt x in t x; // x does not have a well-defined value // ... } Members of arrays and structures are default initialized or not depending on whether the array or structure is static. User-defined types may have default initialization defined (§10.4.2). More complicated objects require more than one value as an initializer. This is handled by ini- tializer lists delimited by { and } for C-style initialization of arrays (§5.2.1) and structures (§5.7). For user-defined types with constructors, function-style argument lists are used (§2.5.2, §10.2.3).
    94. 84 Types and Declarations Chapter 4 Note that an empty pair of parentheses () in a declaration always means ‘‘function’’ (§7.1). For example: i nt a in t a[] = { 1 2 }; 1, // array initializer P oi nt z 1,2 ; Po in t z(1 2) // function-style initializer (initialization by constructor) i nt f ; in t f() // function declaration 4.9.6 Objects and Lvalues [dcl.objects] We can allocate and use ‘‘variables’’ that do not have names, and it is possible to assign to strange-looking expressions (e.g., *p a+1 0]=7 Consequently, there is a need for a name for p[a 10 7). ‘‘something in memory.’’ This is the simplest and most fundamental notion of an object. That is, an object is a contiguous region of storage; an lvalue is an expression that refers to an object. The word lvalue was originally coined to mean ‘‘something that can be on the left-hand side of an assignment.’’ However, not every lvalue may be used on the left-hand side of an assignment; an lvalue can refer to a constant (§5.5). An lvalue that has not been declared c on st is often called a co ns t modifiable lvalue. This simple and low-level notion of an object should not be confused with the notions of class object and object of polymorphic type (§15.4.3). Unless the programmer specifies otherwise (§7.1.2, §10.4.8), an object declared in a function is created when its definition is encountered and destroyed when its name goes out of scope (§10.4.4). Such objects are called automatic objects. Objects declared in global or namespace scope and s ta t-st at - i cs declared in functions or classes are created and initialized once (only) and ‘‘live’’ until the pro- ic gram terminates (§10.4.9). Such objects are called static objects. Array elements and nonstatic structure or class members have their lifetimes determined by the object of which they are part. Using the n ew and d el et e operators, you can create objects whose lifetimes are controlled ne w de le te directly (§6.2.6). 4.9.7 Typedef [dcl.typedef] A declaration prefixed by the keyword t yp ed ef declares a new name for the type rather than a new ty pe de f variable of the given type. For example: t yp ed ef c ha r* P ch ar ty pe de f ch ar Pc ha r; P ch ar p 1, p 2; Pc ha r p1 p2 // p1 and p2 are char*s c ha r* p 3 = p 1; ch ar p3 p1 A name defined like this, usually called a ‘‘ t yp ed ef can be a convenient shorthand for a type ty pe de f,’’ with an unwieldy name. For example, u ns ig ne d c ha r is too long for really frequent use, so we un si gn ed ch ar could define a synonym, u ch ar uc ha r: t yp ed ef u ns ig ne d c ha r u ch ar ty pe de f un si gn ed ch ar uc ha r; Another use of a t yp ed ef is to limit the direct reference to a type to one place. For example: ty pe de f t yp ed ef i nt i nt 32 ty pe de f in t in t3 2; t yp ed ef s ho rt i nt 16 ty pe de f sh or t in t1 6; If we now use i nt 32 wherever we need a potentially large integer, we can port our program to a in t3 2 machine on which s iz eo f(i nt is 2 by redefining the single occurrence of i nt in our code: si ze of in t) in t
    95. Section 4.9.7 Typedef 85 t yp ed ef l on g i nt 32 ty pe de f lo ng in t3 2; For good and bad, t yp ed ef are synonyms for other types rather than distinct types. Consequently, ty pe de fs t yp ed ef mix freely with the types for which they are synonyms. People who would like to have ty pe de fs distinct types with identical semantics or identical representation should look at enumerations (§4.8) or classes (Chapter 10). 4.10 Advice [dcl.advice] [1] Keep scopes small; §4.9.4. [2] Don’t use the same name in both a scope and an enclosing scope; §4.9.4. [3] Declare one name (only) per declaration; §4.9.2. [4] Keep common and local names short, and keep uncommon and nonlocal names longer; §4.9.3. [5] Avoid similar-looking names; §4.9.3. [6] Maintain a consistent naming style; §4.9.3. [7] Choose names carefully to reflect meaning rather than implementation; §4.9.3. [8] Use a t yp ed ef to define a meaningful name for a built-in type in cases in which the built-in ty pe de f type used to represent a value might change; §4.9.7. [9] Use t yp ed ef to define synonyms for types; use enumerations and classes to define new types; ty pe de fs §4.9.7. [10] Remember that every declaration must specify a type (there is no ‘‘implicit i nt in t’’); §4.9.1. [11] Avoid unnecessary assumptions about the numeric value of characters; §4.3.1, §C.6.2.1. [12] Avoid unnecessary assumptions about the size of integers; §4.6. [13] Avoid unnecessary assumptions about the range of floating-point types; §4.6. [14] Prefer a plain i nt over a s ho rt i nt or a l on g i nt §4.6. in t sh or t in t lo ng in t; [15] Prefer a d ou bl e over a f lo at or a l on g d ou bl e; §4.5. do ub le fl oa t lo ng do ub le [16] Prefer plain c ha r over s ig ne d c ha r and u ns ig ne d c ha r; §C.3.4. ch ar si gn ed ch ar un si gn ed ch ar [17] Avoid making unnecessary assumptions about the sizes of objects; §4.6. [18] Avoid unsigned arithmetic; §4.4. [19] View s ig ne d to u ns ig ne d and u ns ig ne d to s ig ne d conversions with suspicion; §C.6.2.6. si gn ed un si gn ed un si gn ed si gn ed [20] View floating-point to integer conversions with suspicion; §C.6.2.6. [21] View conversions to a smaller type, such as i nt to c ha r, with suspicion; §C.6.2.6. in t ch ar 4.11 Exercises [dcl.exercises] 1. (∗2) Get the ‘‘Hello, world!’’ program (§3.2) to run. If that program doesn’t compile as writ- ten, look at §B.3.1. 2. (∗1) For each declaration in §4.9, do the following: If the declaration is not a definition, write a definition for it. If the declaration is a definition, write a declaration for it that is not also a defi- nition. 3. (∗1.5) Write a program that prints the sizes of the fundamental types, a few pointer types, and a few enumerations of your choice. Use the s iz eo f operator. si ze of
    96. 86 Types and Declarations Chapter 4 4. (∗1.5) Write a program that prints out the letters ´a a´..´z and the digits ´0 z´ 0´..´9 and their 9´ integer values. Do the same for other printable characters. Do the same again but use hexa- decimal notation. 5. (∗2) What, on your system, are the largest and the smallest values of the following types: c ha r, ch ar s ho rt i nt l on g, f lo at d ou bl e, l on g d ou bl e, and u ns ig ne d. sh or t, in t, lo ng fl oa t, do ub le lo ng do ub le un si gn ed 6. (∗1) What is the longest local name you can use in a C++ program on your system? What is the longest external name you can use in a C++ program on your system? Are there any restrictions on the characters you can use in a name? 7. (∗2) Draw a graph of the integer and fundamental types where a type points to another type if all values of the first can be represented as values of the second on every standards-conforming implementation. Draw the same graph for the types on your favorite implementation.
    97. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 5 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Pointers, Arrays, and Structures The sublime and the ridiculous are often so nearly related that it is difficult to class them separately. – Tom Paine Pointers — zero — arrays — string literals — pointers into arrays — constants — point- ers and constants — references — v oi d* — data structures — advice — exercises. vo id 5.1 Pointers [ptr.ptr] For a type T T is the type ‘‘pointer to T That is, a variable of type T can hold the address of T, T* T.’’ T* an object of type T For example: T. c ha r c = ´a ch ar a´; c ha r* p = &c ch ar c; // p holds the address of c or graphically: p p: c &c .. c ’a c: a’ Unfortunately, pointers to arrays and pointers to functions need a more complicated notation: i nt p i; in t* pi // pointer to int c ha r** p pc ch ar pp c; // pointer to pointer to char i nt a p[1 5]; in t* ap 15 // array of 15 pointers to ints i nt (*f p)(c ha r*); in t fp ch ar // pointer to function taking a char* argument; returns an int i nt f ch ar ; in t* f(c ha r*) // function taking a char* argument; returns a pointer to int See §4.9.1 for an explanation of the declaration syntax and Appendix A for the complete grammar.
    98. 88 Pointers, Arrays, and Structures Chapter 5 The fundamental operation on a pointer is dereferencing, that is, referring to the object pointed to by the pointer. This operation is also called indirection. The dereferencing operator is (prefix) unary *. For example: c ha r c = ´a ch ar a´; c ha r* p = &c ch ar c; // p holds the address of c c ha r c 2 = *p // c2 == ’a’ ch ar c2 p; The variable pointed to by p is c and the value stored in c is ´a so the value of *p assigned to c 2 c, a´, p c2 is ´a a´. It is possible to perform some arithmetic operations on pointers to array elements (§5.3). Point- ers to functions can be extremely useful; they are discussed in §7.7. The implementation of pointers is intended to map directly to the addressing mechanisms of the machine on which the program runs. Most machines can address a byte. Those that can’t tend to have hardware to extract bytes from words. On the other hand, few machines can directly address an individual bit. Consequently, the smallest object that can be independently allocated and pointed to using a built-in pointer type is a c ha r. Note that a b oo l occupies at least as much space ch ar bo ol as a c ha r (§4.6). To store smaller values more compactly, you can use logical operations (§6.2.4) ch ar or bit fields in structures (§C.8.1). 5.1.1 Zero [ptr.zero] Zero (0 is an i nt Because of standard conversions (§C.6.2.3), 0 can be used as a constant of any 0) in t. integral (§4.1.1), floating-point, pointer, or pointer-to-member type. The type of zero will be deter- mined by context. Zero will typically (but not necessarily) be represented by the bit pattern all- zeros of the appropriate size. No object is allocated with the address 0 Consequently, 0 acts as a pointer literal, indicating 0. that a pointer doesn’t refer to an object. In C, it has been popular to define a macro N UL L to represent the zero pointer. Because of NU LL C++’s tighter type checking, the use of plain 0 rather than any suggested N UL L macro, leads to 0, NU LL fewer problems. If you feel you must define N UL L, use NU LL c on st i nt N UL L = 0 co ns t in t NU LL 0; The c on st qualifier (§5.4) prevents accidental redefinition of N UL L and ensures that N UL L can be co ns t NU LL NU LL used where a constant is required. 5.2 Arrays [ptr.array] For a type T T si ze is the type ‘‘array of s iz e elements of type T T, T[s iz e] si ze T.’’ The elements are indexed from 0 to s iz e-1 For example: si ze 1. f lo at v 3]; fl oa t v[3 // an array of three floats: v[0], v[1], v[2] c ha r* a 32 ; ch ar a[3 2] // an array of 32 pointers to char: a[0] .. a[31]
    99. Section 5.2 Arrays 89 The number of elements of the array, the array bound, must be a constant expression (§C.5). If you need variable bounds, use a v ec to r (§3.7.1, §16.3). For example: ve ct or v oi d f in t i vo id f(i nt i) { i nt v 1[i ; in t v1 i] // error: array size not a constant expression v ec to r<i nt v 2(i ; ve ct or in t> v2 i) // ok } Multidimensional arrays are represented as arrays of arrays. For example: i nt d 2[1 0][2 0]; // d2 is an array of 10 arrays of 20 integers in t d2 10 20 Using comma notation as used for array bounds in some other languages gives compile-time errors because comma (,) is a sequencing operator (§6.2.2) and is not allowed in constant expressions (§C.5). For example, try this: i nt b ad 5,2 ; in t ba d[5 2] // error: comma not allowed in a constant expression Multidimensional arrays are described in §C.7. They are best avoided outside low-level code. 5.2.1 Array Initializers [ptr.array.init] An array can be initialized by a list of values. For example: i nt v 1[] = { 1 2 3 4 }; in t v1 1, 2, 3, c ha r v 2[] = { ´a ch ar v2 a´, ´bb´, ´c c´, 0 }; When an array is declared without a specific size, but with an initializer list, the size is calculated by counting the elements of the initializer list. Consequently, v 1 and v 2 are of type i nt 4] and v1 v2 in t[4 c ha r[4 respectively. If a size is explicitly specified, it is an error to give surplus elements in an ch ar 4], initializer list. For example: c ha r v 3[2 = { ´a ch ar v3 2] a´, ´b b´, 0 }; // error: too many initializers c ha r v 4[3 = { ´a ch ar v4 3] a´, ´b b´, 0 }; // ok If the initializer supplies too few elements, 0 is assumed for the remaining array elements. For example: i nt v 5[8 = { 1 2 3 4 }; in t v5 8] 1, 2, 3, is equivalent to i nt v 5[] = { 1 2 3 4 , 0 0 0 0 }; in t v5 1, 2, 3, 0, 0, 0, Note that there is no array assignment to match the initialization: v oi d f vo id f() { v 4 = { ´c v4 c´, ´d d´, 0 }; // error: no array assignment } When you need such assignments, use a v ec to r (§16.3) or a v al ar ra y (§22.4) instead. ve ct or va la rr ay An array of characters can be conveniently initialized by a string literal (§5.2.2).
    100. 90 Pointers, Arrays, and Structures Chapter 5 5.2.2 String Literals [ptr.string.literal] A string literal is a character sequence enclosed within double quotes: \"t hi s i s a s tr in g\" th is is st ri ng A string literal contains one more character than it appears to have; it is terminated by the null char- acter ´\\ 0´, with the value 0 For example: \\0 0. s iz eo f(\"B oh r\")==5 si ze of Bo hr 5 The type of a string literal is ‘‘array of the appropriate number of c on st characters,’’ so \" Bo hr \" is co ns t \"B oh r\" of type c on st c ha r[5 co ns t ch ar 5]. A string literal can be assigned to a c ha r*. This is allowed because in previous definitions of C ch ar and C++ , the type of a string literal was c ha r*. Allowing the assignment of a string literal to a ch ar c ha r* ensures that millions of lines of C and C++ remain valid. It is, however, an error to try to ch ar modify a string literal through such a pointer: v oi d f vo id f() { c ha r* p = \"P la to ch ar Pl at o\"; p 4] = ´e p[4 e´; // error: assignment to const; result is undefined } This kind of error cannot in general be caught until run-time, and implementations differ in their enforcement of this rule. Having string literals constant not only is obvious, but also allows imple- mentations to do significant optimizations in the way string literals are stored and accessed. If we want a string that we are guaranteed to be able to modify, we must copy the characters into an array: v oi d f vo id f() { c ha r p ch ar p[] = \"Z en o\"; Ze no // p is an array of 5 char p 0] = ´R p[0 R´; // ok } A string literal is statically allocated so that it is safe to return one from a function. For example: c on st c ha r* e rr or _m es sa ge in t i co ns t ch ar er ro r_ me ss ag e(i nt i) { // ... r et ur n \"r an ge e rr or re tu rn ra ng e er ro r\"; } The memory holding r an ge e rr or will not go away after a call of e rr or _m es sa ge ra ng e er ro r er ro r_ me ss ag e(). Whether two identical character literals are allocated as one is implementation-defined (§C.1). For example: c on st c ha r* p = \"H er ac li tu s\"; co ns t ch ar He ra cl it us c on st c ha r* q = \"H er ac li tu s\"; co ns t ch ar He ra cl it us
    101. Section 5.2.2 String Literals 91 v oi d g vo id g() { i f (p == q c ou t << \"o ne \\n if p q) co ut on e!\\ n\"; // result is implementation-defined // ... } Note that == compares addresses (pointer values) when applied to pointers, and not the values pointed to. The empty string is written as a pair of adjacent double quotes, \"\", (and has the type c on st co ns t c ha r[1 ch ar 1]). The backslash convention for representing nongraphic characters (§C.3.2) can also be used within a string. This makes it possible to represent the double quote (\") and the escape character backslash ( \\ within a string. The most common such character by far is the newline character, \\) ´\\ n´. For example: \\n c ou t<<\"b ee p a t e nd o f m es sa ge \\a \\n co ut be ep at en d of me ss ag e\\ a\\ n\"; The escape character ´\\ a´ is the ASCII character B EL (also known as alert), which causes some \\a BE L kind of sound to be emitted. It is not possible to have a ‘‘real’’ newline in a string: \"t hi s i s n ot a s tr in g th is is no t st ri ng b ut a s yn ta x e rr or bu t sy nt ax er ro r\" Long strings can be broken by whitespace to make the program text neater. For example: c ha r a lp ha ch ar al ph a[] = \"a bc de fg hi jk lm no pq rs tu vw xy z\" ab cd ef gh ij kl mn op qr st uv wx yz \"A BC DE FG HI JK LM NO PQ RS TU VW XY Z\"; AB CD EF GH IJ KL MN OP QR ST UV WX YZ The compiler will concatenate adjacent strings, so a lp ha could equivalently have been initialized al ph a by the single string: \"a bc de fg hi jk lm no pq rs tu vw xy zA BC DE FG HI JK LM NO PQ RS TU VW XY Z\"; ab cd ef gh ij kl mn op qr st uv wx yz AB CD EF GH IJ KL MN OP QR ST UV WX YZ It is possible to have the null character in a string, but most programs will not suspect that there are characters after it. For example, the string \" Je ns \\0 00 Mu nk \" will be treated as \" Je ns \" by stan- \"J en s\\ 00 0M un k\" \"J en s\" dard library functions such as s tr cp y() and s tr le n(); see §20.4.1. st rc py st rl en A string with the prefix L such as L an gs t\", is a string of wide characters (§4.3, §C.3.3). Its L, L\"a ng st type is c on st w ch ar _t co ns t wc ha r_ t[]. 5.3 Pointers into Arrays [ptr.into] In C++, pointers and arrays are closely related. The name of an array can be used as a pointer to its initial element. For example: i nt v = { 1 2 3 4 }; in t v[] 1, 2, 3, i nt p 1 = v in t* p1 v; // pointer to initial element (implicit conversion) i nt p 2 = &v 0]; in t* p2 v[0 // pointer to initial element i nt p 3 = &v 4]; in t* p3 v[4 // pointer to one beyond last element or graphically:
    102. 92 Pointers, Arrays, and Structures Chapter 5 p1 p1 p2 p2 p3 p3 . .... v v: 1234 .. Taking a pointer to the element one beyond the end of an array is guaranteed to work. This is important for many algorithms (§2.7.2, §18.3). However, since such a pointer does not in fact point to an element of the array, it may not be used for reading or writing. The result of taking the address of the element before the initial element is undefined and should be avoided. On some machine architectures, arrays are often allocated on machine addressing boundaries, so ‘‘one before the initial element’’ simply doesn’t make sense. The implicit conversion of an array name to a pointer to the initial element of the array is exten- sively used in function calls in C-style code. For example: e xt er n \"C i nt s tr le n(c on st c ha r*); // from <string.h> ex te rn C\" in t st rl en co ns t ch ar v oi d f vo id f() { c ha r v = \"A nn em ar ie ch ar v[] An ne ma ri e\"; c ha r* p = v ch ar v; // implicit conversion of char[] to char* s tr le n(p ; st rl en p) s tr le n(v ; st rl en v) // implicit conversion of char[] to char* v=p p; // error: cannot assign to array } The same value is passed to the standard library function s tr le n() in both calls. The snag is that it st rl en is impossible to avoid the implicit conversion. In other words, there is no way of declaring a func- tion so that the array v is copied when the function is called. Fortunately, there is no implicit or explicit conversion from a pointer to an array. The implicit conversion of the array argument to a pointer means that the size of the array is lost to the called function. However, the called function must somehow determine the size to perform a meaningful operation. Like other C standard library functions taking pointers to characters, s tr le n() relies on zero to indicate end-of-string; s tr le n(p returns the number of characters up to st rl en st rl en p) and not including the terminating 0 This is all pretty low-level. The standard library v ec to r 0. ve ct or (§16.3) and s tr in g (Chapter 20) don’t suffer from this problem. st ri ng 5.3.1 Navigating Arrays [ptr.navigate] Efficient and elegant access to arrays (and similar data structures) is the key to many algorithms (see §3.8, Chapter 18). Access can be achieved either through a pointer to an array plus an index or through a pointer to an element. For example, traversing a character string using an index, v oi d f i(c ha r v vo id fi ch ar v[]) { f or (i nt i = 0 v i]!=0 i fo r in t 0; v[i 0; i++) u se v[i ; us e(v i]) }
    103. Section 5.3.1 Navigating Arrays 93 is equivalent to a traversal using a pointer: v oi d f p(c ha r v vo id fp ch ar v[]) { f or (c ha r* p = v *p 0; p fo r ch ar v; p!=0 p++) u se p); us e(*p } The prefix * operator dereferences a pointer so that *p is the character pointed to by p p p,and ++ increments the pointer so that it refers to the next element of the array. There is no inherent reason why one version should be faster than the other. With modern com- pilers, identical code should be generated for both examples (see §5.9[8]). Programmers can choose between the versions on logical and aesthetic grounds. The result of applying the arithmetic operators +, -, ++, or -- to pointers depends on the type of the object pointed to. When an arithmetic operator is applied to a pointer p of type T p is T*, assumed to point to an element of an array of objects of type T p 1 points to the next element of T; p+1 that array, and p 1 points to the previous element. This implies that the integer value of p 1 will p-1 p+1 be s iz eo f(T larger than the integer value of p For example, executing si ze of T) p. #i nc lu de <i os tr ea m> in cl ud e io st re am i nt m ai n () in t ma in { i nt v i[1 0]; in t vi 10 s ho rt v s[1 0]; sh or t vs 10 s td :c ou t << &v i[0 << ´ ´ << &v i[1 << ´\\ n´; st d: co ut vi 0] vi 1] \\n s td :c ou t << &v s[0 << ´ ´ << &v s[1 << ´\\ n´; st d: co ut vs 0] vs 1] \\n } produced 0 x7 ff fa ef 0 0 x7 ff fa ef 4 0x 7f ff ae f0 0x 7f ff ae f4 0 x7 ff fa ed c 0 x7 ff fa ed e 0x 7f ff ae dc 0x 7f ff ae de using a default hexadecimal notation for pointer values. This shows that on my implementation, s iz eo f(s ho rt is 2 and s iz eo f(i nt is 4 si ze of sh or t) si ze of in t) 4. Subtraction of pointers is defined only when both pointers point to elements of the same array (although the language has no fast way of ensuring that is the case). When subtracting one pointer from another, the result is the number of array elements between the two pointers (an integer). One can add an integer to a pointer or subtract an integer from a pointer; in both cases, the result is a pointer value. If that value does not point to an element of the same array as the original pointer or one beyond, the result of using that value is undefined. For example: v oi d f vo id f() { i nt v 1[1 0]; in t v1 10 i nt v 2[1 0]; in t v2 10 i nt i 1 = &v 1[5 in t i1 v1 5]-&v 1[3 ; // i1 = 2 v1 3] i nt i 2 = &v 1[5 in t i2 v1 5]-&v 2[3 ; // result undefined v2 3]
    104. 94 Pointers, Arrays, and Structures Chapter 5 i nt p 1 = v 2+2 in t* p1 v2 2; // p1 = &v2[2] i nt p 2 = v 2-2 in t* p2 v2 2; // *p2 undefined } Complicated pointer arithmetic is usually unnecessary and often best avoided. Addition of pointers makes no sense and is not allowed. Arrays are not self-describing because the number of elements of an array is not guaranteed to be stored with the array. This implies that to traverse an array that does not contain a terminator the way character strings do, we must somehow supply the number of elements. For example: v oi d f p(c ha r v , u ns ig ne d i nt s iz e) vo id fp ch ar v[] un si gn ed in t si ze { f or (i nt i 0; i si ze i fo r in t i=0 i<s iz e; i++) u se v[i ; us e(v i]) c on st i nt N = 7 co ns t in t 7; c ha r v 2[N ; ch ar v2 N] f or (i nt i 0; i N; i fo r in t i=0 i<N i++) u se v2 i]); us e(v 2[i } Note that most C++ implementations offer no range checking for arrays. This array concept is inherently low-level. A more advanced notion of arrays can be provided through the use of classes; see §3.7.1. 5.4 Constants [ptr.const] C++ offers the concept of a user-defined constant, a c on st to express the notion that a value doesn’t co ns t, change directly. This is useful in several contexts. For example, many objects don’t actually have their values changed after initialization, symbolic constants lead to more maintainable code than do literals embedded directly in code, pointers are often read through but never written through, and most function parameters are read but not written to. The keyword c on st can be added to the declaration of an object to make the object declared a co ns t constant. Because it cannot be assigned to, a constant must be initialized. For example: c on st i nt m od el = 9 0; co ns t in t mo de l 90 // model is a const c on st i nt v = { 1 2 3 4 }; co ns t in t v[] 1, 2, 3, // v[i] is a const c on st i nt x co ns t in t x; // error: no initializer Declaring something c on st ensures that its value will not change within its scope: co ns t v oi d f vo id f() { m od el = 2 00 mo de l 20 0; // error v 2]++; v[2 // error } Note that c on st modifies a type; that is, it restricts the ways in which an object can be used, rather co ns t than specifying how the constant is to be allocated. For example:
    105. Section 5.4 Constants 95 v oi d g co ns t X p vo id g(c on st X* p) { // can’t modify *p here } v oi d h vo id h() { X v al va l; // val can be modified g va l); g(&v al // ... } Depending on how smart it is, a compiler can take advantage of an object being a constant in sev- eral ways. For example, the initializer for a constant is often (but not always) a constant expression (§C.5); if it is, it can be evaluated at compile time. Further, if the compiler knows every use of the c on st it need not allocate space to hold it. For example: co ns t, c on st i nt c 1 = 1 co ns t in t c1 1; c on st i nt c 2 = 2 co ns t in t c2 2; co ns t in t c3 my _f 3) c on st i nt c 3 = m y_ f(3 ; // don’t know the value of c3 at compile time e xt er n c on st i nt c 4; ex te rn co ns t in t c4 // don’t know the value of c4 at compile time c on st i nt p = &c 2; co ns t in t* c2 // need to allocate space for c2 Given this, the compiler knows the values of c 1 and c 2 so that they can be used in constant expres- c1 c2 sions. Because the values of c 3 and c 4 are not known at compile time (using only the information c3 c4 available in this compilation unit; see §9.1), storage must be allocated for c 3 and c 4. Because the c3 c4 address of c 2 is taken (and presumably used somewhere), storage must be allocated for c 2. The c2 c2 simple and common case is the one in which the value of the constant is known at compile time and no storage needs to be allocated; c 1 is an example of that. The keyword e xt er n indicates that c 4 is c1 ex te rn c4 defined elsewhere (§9.2). It is typically necessary to allocate store for an array of constants because the compiler cannot, in general, figure out which elements of the array are referred to in expressions. On many machines, however, efficiency improvements can be achieved even in this case by placing arrays of constants in read-only storage. Common uses for c on st are as array bounds and case labels. For example: co ns ts c on st i nt a = 4 2; co ns t in t 42 c on st i nt b = 9 9; co ns t in t 99 c on st i nt m ax = 1 28 co ns t in t ma x 12 8; i nt v ma x]; in t v[m ax v oi d f in t i vo id f(i nt i) { s wi tc h (i { sw it ch i) c as e a ca se a: // ...
    106. 96 Pointers, Arrays, and Structures Chapter 5 c as e b ca se b: // ... } } Enumerators (§4.8) are often an alternative to c on st in such cases. co ns ts The way c on st can be used with class member functions is discussed in §10.2.6 and §10.2.7. co ns t Symbolic constants should be used systematically to avoid ‘‘magic numbers’’ in code. If a numeric constant, such as an array bound, is repeated in code, it becomes hard to revise that code because every occurrence of that constant must be changed to make a correct update. Using a sym- bolic constant instead localizes information. Usually, a numeric constant represents an assumption about the program. For example, 4 may represent the number of bytes in an integer, 1 28 the num- 12 8 ber of characters needed to buffer input, and 6 24 the exchange factor between Danish kroner and 6.2 4 U.S. dollars. Left as numeric constants in the code, these values are hard for a maintainer to spot and understand. Often, such numeric values go unnoticed and become errors when a program is ported or when some other change violates the assumptions they represent. Representing assump- tions as well-commented symbolic constants minimizes such maintenance problems. 5.4.1 Pointers and Constants [ptr.pc] When using a pointer, two objects are involved: the pointer itself and the object pointed to. ‘‘Pre- fixing’’ a declaration of a pointer with c on st makes the object, but not the pointer, a constant. To co ns t declare a pointer itself, rather than the object pointed to, to be a constant, we use the declarator operator *c on st instead of plain *. For example: co ns t v oi d f 1(c ha r* p vo id f1 ch ar p) { c ha r s ch ar s[] = \"G or m\"; Go rm c on st c ha r* p c = s co ns t ch ar pc s; // pointer to constant p c[3 = ´g pc 3] g´; // error: pc points to constant pc = p pc p; // ok c ha r *c on st c p = s ch ar co ns t cp s; // constant pointer c p[3 = ´a cp 3] a´; // ok cp = p cp p; // error: cp is constant c on st c ha r *c on st c pc = s co ns t ch ar co ns t cp c s; // const pointer to const c pc 3] = ´a cp c[3 a´; // error: cpc points to constant c pc = p cp c p; // error: cpc is constant } The declarator operator that makes a pointer constant is *c on st There is no c on st declarator co ns t. co ns t* operator, so a c on st appearing before the * is taken to be part of the base type. For example: co ns t c ha r *c on st c p; ch ar co ns t cp // const pointer to char c ha r c on st p c; ch ar co ns t* pc // pointer to const char c on st c ha r* p c2 co ns t ch ar pc 2; // pointer to const char Some people find it helpful to read such declarations right-to-left. For example, ‘‘c p is a c on st cp co ns t pointer to a c ha r’’ and ‘‘p c2 is a pointer to a c ha r c on st ch ar pc 2 ch ar co ns t.’’
    107. Section 5.4.1 Pointers and Constants 97 An object that is a constant when accessed through one pointer may be variable when accessed in other ways. This is particularly useful for function arguments. By declaring a pointer argument c on st the function is prohibited from modifying the object pointed to. For example: co ns t, c ha r* s tr cp y(c ha r* p c on st c ha r* q ; // cannot modify *q ch ar st rc py ch ar p, co ns t ch ar q) You can assign the address of a variable to a pointer to constant because no harm can come from that. However, the address of a constant cannot be assigned to an unrestricted pointer because this would allow the object’s value to be changed. For example: v oi d f 4() vo id f4 { i nt a = 1 in t 1; c on st i nt c = 2 co ns t in t 2; c on st i nt p 1 = &c co ns t in t* p1 c; // ok c on st i nt p 2 = &a co ns t in t* p2 a; // ok i nt p 3 = &c in t* p3 c; // error: initialization of int* with const int* *p 3 = 7 p3 7; // try to change the value of c } It is possible to explicitly remove the restrictions on a pointer to c on st by explicit type conversion co ns t (§10.2.7.1 and §15.4.2.1). 5.5 References [ptr.ref] A reference is an alternative name for an object. The main use of references is for specifying argu- ments and return values for functions in general and for overloaded operators (Chapter 11) in par- ticular. The notation X means reference to X For example: X& X. v oi d f vo id f() { i nt i = 1 in t 1; i nt r = i in t& i; // r and i now refer to the same int i nt x = r in t r; // x = 1 r=2 2; // i = 2 } To ensure that a reference is a name for something (that is, bound to an object), we must initialize the reference. For example: i nt i = 1 in t 1; i nt r 1 = i in t& r1 i; // ok: r1 initialized i nt r 2; in t& r2 // error: initializer missing e xt er n i nt r 3; ex te rn in t& r3 // ok: r3 initialized elsewhere Initialization of a reference is something quite different from assignment to it. Despite appear- ances, no operator operates on a reference. For example:
    108. 98 Pointers, Arrays, and Structures Chapter 5 v oi d g vo id g() { i nt i i = 0 in t ii 0; i nt r r = i i; in t& rr ii r r++; rr // ii is incremented to 1 i nt p p = &r r; in t* pp rr // pp points to ii } This is legal, but r r++ does not increment the reference r r; rather, ++ is applied to an i nt that hap- rr rr in t pens to be i i. Consequently, the value of a reference cannot be changed after initialization; it ii always refers to the object it was initialized to denote. To get a pointer to the object denoted by a reference r r, we can write &r r. rr rr The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. It doesn’t do much harm thinking about references that way, as long as one remem- bers that a reference isn’t an object that can be manipulated the way a pointer is: p p: pp &i i ii r r: rr i i: ii 1 In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run-time. Initialization of a reference is trivial when the initializer is an lvalue (an object whose address you can take; see §4.9.6). The initializer for a ‘‘plain’’ T must be an lvalue of type T T& T. The initializer for a c on st T need not be an lvalue or even of type T In such cases, co ns t T& T. [1] first, implicit type conversion to T is applied if necessary (see §C.6); [2] then, the resulting value is placed in a temporary variable of type T and T; [3] finally, this temporary variable is used as the value of the initializer. Consider: d ou bl e& d r = 1 do ub le dr 1; // error: lvalue needed c on st d ou bl e& c dr = 1 co ns t do ub le cd r 1; // ok The interpretation of this last initialization might be: d ou bl e t em p = d ou bl e(1 ; // first create a temporary with the right value do ub le te mp do ub le 1) c on st d ou bl e& c dr = t em p; // then use the temporary as the initializer for cdr co ns t do ub le cd r te mp A temporary created to hold a reference initializer persists until the end of its reference’s scope. References to variables and references to constants are distinguished because the introduction of a temporary in the case of the variable is highly error-prone; an assignment to the variable would become an assignment to the – soon to disappear – temporary. No such problem exists for refer- ences to constants, and references to constants are often important as function arguments (§11.6). A reference can be used to specify a function argument so that the function can change the value of an object passed to it. For example:
    109. Section 5.5 References 99 v oi d i nc re me nt in t& a a) { a a++; } vo id in cr em en t(i nt aa aa v oi d f vo id f() { i nt x = 1 in t 1; i nc re me nt x); in cr em en t(x // x = 2 } The semantics of argument passing are defined to be those of initialization, so when called, i nc re me nt argument a a became another name for x To keep a program readable, it is often best in cr em en t’s aa x. to avoid functions that modify their arguments. Instead, you can return a value from the function explicitly or require a pointer argument: i nt n ex t(i nt p { r et ur n p 1; } in t ne xt in t p) re tu rn p+1 v oi d i nc r(i nt p { (*p vo id in cr in t* p) p)++; } v oi d g vo id g() { i nt x = 1 in t 1; i nc re me nt x); in cr em en t(x // x = 2 x = n ex t(x ; ne xt x) // x = 3 i nc r(&x ; in cr x) // x = 4 } The i nc re me nt x) notation doesn’t give a clue to the reader that x value is being modified, the in cr em en t(x x’s way x ne xt x) and i nc r(&x does. Consequently ‘‘plain’’ reference arguments should be used x=n ex t(x in cr x) only where the name of the function gives a strong hint that the reference argument is modified. References can also be used to define functions that can be used on both the left-hand and right-hand sides of an assignment. Again, many of the most interesting uses of this are found in the design of nontrivial user-defined types. As an example, let us define a simple associative array. First, we define struct P ai r like this: Pa ir s tr uc t P ai r { st ru ct Pa ir s tr in g n am e; st ri ng na me d ou bl e v al do ub le va l; }; The basic idea is that a s tr in g has a floating-point value associated with it. It is easy to define a st ri ng function, v al ue va lu e(), that maintains a data structure consisting of one P ai r for each different string Pa ir that has been presented to it. To shorten the presentation, a very simple (and inefficient) implemen- tation is used: v ec to r<P ai r> p ai rs ve ct or Pa ir pa ir s; d ou bl e& v al ue co ns t s tr in g& s do ub le va lu e(c on st st ri ng s) /* maintain a set of Pairs: search for s, return its value if found; otherwise make a new Pair and return the default value 0 */ {
    110. 100 Pointers, Arrays, and Structures Chapter 5 f or (i nt i = 0 i < p ai rs si ze ; i fo r in t 0; pa ir s.s iz e() i++) i f (s == p ai rs i].n am e) r et ur n p ai rs i].v al if s pa ir s[i na me re tu rn pa ir s[i va l; P ai r p = { s 0 }; Pa ir s, p ai rs pu sh _b ac k(p ; // add Pair at end (§3.7.3) pa ir s.p us h_ ba ck p) r et ur n p ai rs pa ir s.s iz e()-1 va l; re tu rn pa ir s[p ai rs si ze 1].v al } This function can be understood as an array of floating-point values indexed by character strings. For a given argument string, v al ue va lu e() finds the corresponding floating-point object (not the value of the corresponding floating-point object); it then returns a reference to it. For example: i nt m ai n() // count the number of occurrences of each word on input in t ma in { s tr in g b uf st ri ng bu f; w hi le (c in bu f) v al ue bu f)++; wh il e ci n>>b uf va lu e(b uf f or (v ec to r<P ai r>::c on st _i te ra to r p = p ai rs be gi n(); p pa ir s.e nd ; ++p fo r ve ct or Pa ir co ns t_ it er at or pa ir s.b eg in p!=p ai rs en d() p) c ou t << p na me << \": \" << p va l << ´\\ n´; co ut p->n am e p->v al \\n } Each time around, the w hi le wh il e-loop reads one word from the standard input stream c in into the string ci n b uf (§3.6) and then updates the counter associated with it. Finally, the resulting table of different bu f words in the input, each with its number of occurrences, is printed. For example, given the input aa bb bb aa aa bb aa aa aa bb bb aa aa bb aa aa this program will produce: a a: 5 aa b b: 3 bb It is easy to refine this into a proper associative array type by using a template class with the selec- tion operator [] overloaded (§11.8). It is even easier just to use the standard library m ap (§17.4.1). ma p 5.6 Pointer to Void [ptr.ptrtovoid] A pointer of any type of object can be assigned to a variable of type v oi d*, a v oi d* can be assigned vo id vo id to another v oi d*, v oi d*s can be compared for equality and inequality, and a v oi d* can be explicitly vo id vo id vo id converted to another type. Other operations would be unsafe because the compiler cannot know what kind of object is really pointed to. Consequently, other operations result in compile-time errors. To use a v oi d*, we must explicitly convert it to a pointer to a specific type. For example: vo id v oi d f in t* p i) vo id f(i nt pi { v oi d* p v = p i; // ok: implicit conversion of int* to void* vo id pv pi *p v; pv // error: can’t dereference void* p v++; pv // error: can’t increment void* (the size of the object pointed to is unknown)
    111. Section 5.6 Pointer to Void 101 in t* pi 2 st at ic _c as t<i nt i nt p i2 = s ta ti c_ ca st in t*>(p v); pv // explicit conversion back to int* d ou bl e* p d1 = p v; do ub le pd 1 pv // error d ou bl e* p d2 = p i; do ub le pd 2 pi // error do ub le pd 3 st at ic _c as t<d ou bl e*>(p v); // unsafe d ou bl e* p d3 = s ta ti c_ ca st do ub le pv } In general, it is not safe to use a pointer that has been converted (‘‘cast’’) to a type that differs from the type the object pointed to. For example, a machine may assume that every d ou bl e is allocated do ub le on an 8-byte boundary. If so, strange behavior could arise if p i pointed to an i nt that wasn’t allo- pi in t cated that way. This form of explicit type conversion is inherently unsafe and ugly. Consequently, st at ic _c as t, the notation used, s ta ti c_ ca st was designed to be ugly. The primary use for v oi d* is for passing pointers to functions that are not allowed to make vo id assumptions about the type of the object and for returning untyped objects from functions. To use such an object, we must use explicit type conversion. Functions using v oi d* pointers typically exist at the very lowest level of the system, where real vo id hardware resources are manipulated. For example: vo id my _a ll oc si ze _t n) v oi d* m y_ al lo c(s iz e_ t n ; // allocate n bytes from my special heap Occurrences of v oi d*s at higher levels of the system should be viewed with suspicion because they vo id are likely indicators of design errors. Where used for optimization, v oi d* can be hidden behind a vo id type-safe interface (§13.5, §24.4.2). Pointers to functions (§7.7) and pointers to members (§15.5) cannot be assigned to v oi d*s. vo id 5.7 Structures [ptr.struct] An array is an aggregate of elements of the same type. A s tr uc t is an aggregate of elements of st ru ct (nearly) arbitrary types. For example: s tr uc t a dd re ss { st ru ct ad dr es s c ha r* n am e; ch ar na me // \"Jim Dandy\" l on g i nt n um be r; lo ng in t nu mb er // 61 c ha r* s tr ee t; ch ar st re et // \"South St\" c ha r* t ow n; ch ar to wn // \"New Providence\" c ha r s ta te 2]; ch ar st at e[2 // ’N’ ’J’ l on g z ip lo ng zi p; // 7974 }; This defines a new type called a dd re ss consisting of the items you need in order to send mail to ad dr es s someone. Note the semicolon at the end. This is one of very few places in C++ where it is neces- sary to have a semicolon after a curly brace, so people are prone to forget it. Variables of type a dd re ss can be declared exactly as other variables, and the individual ad dr es s members can be accessed using the . (dot) operator. For example:
    112. 102 Pointers, Arrays, and Structures Chapter 5 v oi d f vo id f() { a dd re ss j d; ad dr es s jd j d.n am e = \"J im D an dy jd na me Ji m Da nd y\"; j d.n um be r = 6 1; jd nu mb er 61 } The notation used for initializing arrays can also be used for initializing variables of structure types. For example: a dd re ss j d = { ad dr es s jd \"J im D an dy Ji m Da nd y\", 6 1, \"S ou th S t\", 61 So ut h St \"N ew P ro vi de nc e\", {´N Ne w Pr ov id en ce N´,´J J´}, 7 97 4 79 74 }; Using a constructor (§10.2.3) is usually better, however. Note that j d.s ta te could not be initialized jd st at e by the string \" NJ \". Strings are terminated by the character ´\\ 0´. Hence, \" NJ \" has three characters \"N J\" \\0 \"N J\" – one more than will fit into j d.s ta te jd st at e. Structure objects are often accessed through pointers using the -> (structure pointer derefer- ence) operator. For example: v oi d p ri nt _a dd r(a dd re ss p vo id pr in t_ ad dr ad dr es s* p) { c ou t << p na me << ´\\ n´ co ut p->n am e \\n << p nu mb er << ´ ´ << p st re et << ´\\ n´ p->n um be r p->s tr ee t \\n << p to wn << ´\\ n´ p->t ow n \\n << p st at e[0 << p st at e[1 << ´ ´ << p zi p << ´\\ n´; p->s ta te 0] p->s ta te 1] p->z ip \\n } When p is a pointer, p m is equivalent to (*p m. p->m p).m Objects of structure types can be assigned, passed as function arguments, and returned as the result from a function. For example: a dd re ss c ur re nt ad dr es s cu rr en t; a dd re ss s et _c ur re nt ad dr es s n ex t) ad dr es s se t_ cu rr en t(a dd re ss ne xt { a dd re ss p re v = c ur re nt ad dr es s pr ev cu rr en t; c ur re nt = n ex t; cu rr en t ne xt r et ur n p re v; re tu rn pr ev } Other plausible operations, such as comparison (== and !=), are not defined. However, the user can define such operators (Chapter 11). The size of an object of a structure type is not necessarily the sum of the sizes of its members. This is because many machines require objects of certain types to be allocated on architecture- dependent boundaries or handle such objects much more efficiently if they are. For example, inte- gers are often allocated on word boundaries. On such machines, objects are said to have to be aligned properly. This leads to ‘‘holes’’ in the structures. For example, on many machines,
    113. Section 5.7 Structures 103 s iz eo f(a dd re ss is 2 4, and not 2 2 as might be expected. You can minimize wasted space by sim- si ze of ad dr es s) 24 22 ply ordering members by size (largest member first). However, it is usually best to order members for readability and sort them by size only if there is a demonstrated need to optimize. The name of a type becomes available for use immediately after it has been encountered and not just after the complete declaration has been seen. For example: s tr uc t L in k { st ru ct Li nk L in k* p re vi ou s; Li nk pr ev io us L in k* s uc ce ss or Li nk su cc es so r; }; It is not possible to declare new objects of a structure type until the complete declaration has been seen. For example: st ru ct No _g oo d s tr uc t N o_ go od { No _g oo d me mb er N o_ go od m em be r; // error: recursive definition }; No _g oo d. This is an error because the compiler is not able to determine the size of N o_ go od To allow two (or more) structure types to refer to each other, we can declare a name to be the name of a structure type. For example: s tr uc t L is t; st ru ct Li st // to be defined later s tr uc t L in k { st ru ct Li nk L in k* p re Li nk pr e; L in k* s uc Li nk su c; Li st me mb er _o f; L is t* m em be r_ of }; s tr uc t L is t { st ru ct Li st L in k* h ea d; Li nk he ad }; Without the first declaration of L is t, use of L is t in the declaration of L in k would have caused a syn- Li st Li st Li nk tax error. The name of a structure type can be used before the type is defined as long as that use does not require the name of a member or the size of the structure to be known. For example: c la ss S cl as s S; // ‘S’ is the name of some type e xt er n S a ex te rn a; Sf ;f() v oi d g S); vo id g(S S h S*); S* h(S However, many such declarations cannot be used unless the type S is defined: v oi d k S* p vo id k(S p) { Saa; // error: S not defined; size needed to allocate
    114. 104 Pointers, Arrays, and Structures Chapter 5 f; f() // error: S not defined; size needed to return value g a); g(a // error: S not defined; size needed to pass argument p m=7 p->m 7; // error: S not defined; member name not known S q = h p); S* h(p // ok: pointers can be allocated and passed q m=7 q->m 7; // error: S not defined; member name not known } A s tr uc t is a simple form of a c la ss (Chapter 10). st ru ct cl as s For reasons that reach into the pre-history of C, it is possible to declare a s tr uc t and a non- st ru ct structure with the same name in the same scope. For example: s tr uc t s ta t { /* ... */ }; st ru ct st at i nt s ta t(c ha r* n am e, s tr uc t s ta t* b uf ; in t st at ch ar na me st ru ct st at bu f) In that case, the plain name (s ta t) is the name of the non-structure, and the structure must be st at referred to with the prefix s tr uc t. Similarly, the keywords c la ss u ni on (§C.8.2), and e nu m (§4.8) st ru ct cl as s, un io n en um can be used as prefixes for disambiguation. However, it is best not to overload names to make that necessary. 5.7.1 Type Equivalence [ptr.equiv] Two structures are different types even when they have the same members. For example, s tr uc t S 1 { i nt a }; st ru ct S1 in t a; s tr uc t S 2 { i nt a }; st ru ct S2 in t a; are two different types, so S1 x S1 x; S 2 y = x // error: type mismatch S2 x; Structure types are also different from fundamental types, so S1 x S1 x; i nt i = x // error: type mismatch in t x; Every s tr uc t must have a unique definition in a program (§9.2.3). st ru ct 5.8 Advice [ptr.advice] [1] Avoid nontrivial pointer arithmetic; §5.3. [2] Take care not to write beyond the bounds of an array; §5.3.1. [3] Use 0 rather than N UL L; §5.1.1. NU LL [4] Use v ec to r and v al ar ra y rather than built-in (C-style) arrays; §5.3.1. ve ct or va la rr ay [5] Use s tr in g rather than zero-terminated arrays of c ha r; §5.3. st ri ng ch ar [6] Minimize use of plain reference arguments; §5.5. [7] Avoid v oi d* except in low-level code; §5.6. vo id [8] Avoid nontrivial literals (‘‘magic numbers’’) in code. Instead, define and use symbolic con- stants; §4.8, §5.4.
    115. Section 5.9 Exercises 105 5.9 Exercises [ptr.exercises] 1. (∗1) Write declarations for the following: a pointer to a character, an array of 10 integers, a ref- erence to an array of 10 integers, a pointer to an array of character strings, a pointer to a pointer to a character, a constant integer, a pointer to a constant integer, and a constant pointer to an integer. Initialize each one. 2. (∗1.5) What, on your system, are the restrictions on the pointer types c ha r*, i nt and v oi d*? ch ar in t*, vo id For example, may an i nt have an odd value? Hint: alignment. in t* 3. (∗1) Use t yp ed ef to define the types u ns ig ne d c ha r, c on st u ns ig ne d c ha r, pointer to integer, ty pe de f un si gn ed ch ar co ns t un si gn ed ch ar pointer to pointer to c ha r, pointer to arrays of c ha r, array of 7 pointers to i nt pointer to an array ch ar ch ar in t, of 7 pointers to i nt and array of 8 arrays of 7 pointers to i nt in t, in t. 4. (∗1) Write a function that swaps (exchanges the values of) two integers. Use i nt as the argu- in t* ment type. Write another swap function using i nt as the argument type. in t& 5. (∗1.5) What is the size of the array s tr in the following example: st r c ha r s tr = \"a s ho rt s tr in g\"; ch ar st r[] a sh or t st ri ng What is the length of the string \" a s ho rt s tr in g\" \"a sh or t st ri ng \"? 6. (∗1) Define functions f ch ar g ch ar f(c ha r), g(c ha r&), and h co ns t c ha r&). Call them with the arguments h(c on st ch ar ´a 4 9, 3 30 0, c u c, and s c, where c is a c ha r, u c is an u ns ig ne d c ha r, and s c is a s ig ne d a´, 49 33 00 c, uc sc ch ar uc un si gn ed ch ar sc si gn ed c ha r. Which calls are legal? Which calls cause the compiler to introduce a temporary variable? ch ar 7. (∗1.5) Define a table of the names of months of the year and the number of days in each month. Write out that table. Do this twice; once using an array of c ha r for the names and an array for ch ar the number of days and once using an array of structures, with each structure holding the name of a month and the number of days in it. 8. (∗2) Run some tests to see if your compiler really generates equivalent code for iteration using pointers and iteration using indexing (§5.3.1). If different degrees of optimization can be requested, see if and how that affects the quality of the generated code. 9. (∗1.5) Find an example where it would make sense to use a name in its own initializer. 10. (∗1) Define an array of strings in which the strings contain the names of the months. Print those strings. Pass the array to a function that prints those strings. 11. (∗2) Read a sequence of words from input. Use Q ui t as a word that terminates the input. Print Qu it the words in the order they were entered. Don’t print a word twice. Modify the program to sort the words before printing them. 12. (∗2) Write a function that counts the number of occurrences of a pair of letters in a s tr in g and st ri ng another that does the same in a zero-terminated array of c ha r (a C-style string). For example, ch ar the pair \"ab\" appears twice in \"xabaacbaxabb\". 13. (∗1.5) Define a s tr uc t D at e to keep track of dates. Provide functions that read D at es from st ru ct Da te Da te input, write D at es to output, and initialize a D at e with a date. Da te Da te
    116. 106 Pointers, Arrays, and Structures Chapter 5 .
    117. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 6 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Expressions and Statements Premature optimization is the root of all evil. – D. Knuth On the other hand, we cannot ignore efficiency. – Jon Bentley Desk calculator example — input — command line arguments — expression summary — logical and relational operators — increment and decrement — free store — explicit type conversion — statement summary — declarations — selection statements — decla- rations in conditions — iteration statements — the infamous g ot o — comments and go to indentation — advice — exercises. 6.1 A Desk Calculator [expr.calculator] Statements and expressions are introduced by presenting a desk calculator program that provides the four standard arithmetic operations as infix operators on floating-point numbers. The user can also define variables. For example, given the input r=2 52.5 a re a = p i * r * r ar ea pi (pi is predefined) the calculator program will write 25 2.5 1 9.6 35 19 63 5 where 2 5 is the result of the first line of input and 1 9.6 35 is the result of the second. 2.5 19 63 5
    118. 108 Expressions and Statements Chapter 6 The calculator consists of four main parts: a parser, an input function, a symbol table, and a driver. Actually, it is a miniature compiler in which the parser does the syntactic analysis, the input function handles input and lexical analysis, the symbol table holds permanent information, and the driver handles initialization, output, and errors. We could add many features to this calculator to make it more useful (§6.6[20]), but the code is long enough as it is, and most features would just add code without providing additional insight into the use of C++. 6.1.1 The Parser [expr.parser] Here is a grammar for the language accepted by the calculator: p ro gr am pr og ra m: E ND EN D // END is end-of-input ex pr _l is t EN D e xp r_ li st E ND ex pr _l is t: e xp r_ li st e xp re ss io n P RI NT ex pr es si on PR IN T // PRINT is semicolon ex pr es si on PR IN T ex pr _l is t e xp re ss io n P RI NT e xp r_ li st e xp re ss io n: ex pr es si on e xp re ss io n + t er m ex pr es si on te rm e xp re ss io n - t er m ex pr es si on te rm t er m te rm t er m: te rm t er m / p ri ma ry te rm pr im ar y t er m * p ri ma ry te rm pr im ar y p ri ma ry pr im ar y p ri ma ry pr im ar y: N UM BE R NU MB ER N AM E NA ME N AM E = e xp re ss io n NA ME ex pr es si on - p ri ma ry pr im ar y ( e xp re ss io n ) ex pr es si on In other words, a program is a sequence of expressions separated by semicolons. The basic units of an expression are numbers, names, and the operators *, /, +, - (both unary and binary), and =. Names need not be declared before use. The style of syntax analysis used is usually called recursive descent; it is a popular and straight- forward top-down technique. In a language such as C++, in which function calls are relatively cheap, it is also efficient. For each production in the grammar, there is a function that calls other functions. Terminal symbols (for example, E ND N UM BE R, +, and -) are recognized by the lexi- EN D, NU MB ER cal analyzer, g et _t ok en ge t_ to ke n(); and nonterminal symbols are recognized by the syntax analyzer func- tions, e xp r(), t er m(), and p ri m(). As soon as both operands of a (sub)expression are known, the ex pr te rm pr im expression is evaluated; in a real compiler, code could be generated at this point. The parser uses a function g et _t ok en ge t_ to ke n() to get input. The value of the most recent call of g et _t ok en cu rr _t ok cu rr _t ok ge t_ to ke n() can be found in the global variable c ur r_ to k. The type of c ur r_ to k is the enumera- tion T ok en _v al ue To ke n_ va lu e:
    119. Section 6.1.1 The Parser 109 e nu m T ok en _v al ue { en um To ke n_ va lu e N AM E, NA ME N UM BE R, NU MB ER E ND EN D, P LU S=´+´, PL US M IN US MI NU S=´-´, M UL MU L=´*´, D IV DI V=´/´, P RI NT PR IN T=´;´, A SS IG N=´=´, L P=´(´, AS SI GN LP R P=´)´ RP }; T ok en _v al ue c ur r_ to k = P RI NT To ke n_ va lu e cu rr _t ok PR IN T; Representing each token by the integer value of its character is convenient and efficient and can be a help to people using debuggers. This works as long as no character used as input has a value used as an enumerator – and no character set I know of has a printing character with a single-digit inte- cu rr _t ok ger value. I chose P RI NT as the initial value for c ur r_ to k because that is the value it will have PR IN T after the calculator has evaluated an expression and displayed its value. Thus, I ‘‘start the system’’ in a normal state to minimize the chance of errors and the need for special startup code. Each parser function takes a b oo l (§4.2) argument indicating whether the function needs to call bo ol g et _t ok en to get the next token. Each parser function evaluates ‘‘its’’ expression and returns the ge t_ to ke n() value. The function e xp r() handles addition and subtraction. It consists of a single loop that looks ex pr for terms to add or subtract: d ou bl e e xp r(b oo l g et do ub le ex pr bo ol ge t) // add and subtract { d ou bl e l ef t = t er m(g et ; do ub le le ft te rm ge t) f or (;;) fo r // ‘‘forever’’ sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { c as e P LU S: ca se PL US l ef t += t er m(t ru e); le ft te rm tr ue b re ak br ea k; c as e M IN US ca se MI NU S: l ef t -= t er m(t ru e); le ft te rm tr ue b re ak br ea k; d ef au lt de fa ul t: r et ur n l ef t; re tu rn le ft } } This function really does not do much itself. In a manner typical of higher-level functions in a large program, it calls other functions to do the work. The switch-statement tests the value of its condition, which is supplied in parentheses after the s wi tc h keyword, against a set of constants. The break-statements are used to exit the switch- sw it ch statement. The constants following the c as e labels must be distinct. If the value tested does not ca se match any c as e label, the d ef au lt is chosen. The programmer need not provide a d ef au lt ca se de fa ul t de fa ul t. Note that an expression such as 2 3+4 is evaluated as (2 3)+4 as specified in the grammar. 2-3 4 2-3 4, The curious notation f or fo r(;;) is the standard way to specify an infinite loop; you could pro- nounce it ‘‘forever.’’ It is a degenerate form of a for-statement (§6.3.3); w hi le tr ue is an alterna- wh il e(t ru e) tive. The switch-statement is executed repeatedly until something different from + and - is found, and then the return-statement in the default case is executed. The operators += and -= are used to handle the addition and subtraction; l ef t=l ef t+t er m() and le ft le ft te rm
    120. 110 Expressions and Statements Chapter 6 l ef t=l ef t-t er m() could have been used without changing the meaning of the program. However, le ft le ft te rm l ef t+=t er m() and l ef t-=t er m() not only are shorter but also express the intended operation le ft te rm le ft te rm directly. Each assignment operator is a separate lexical token, so a + = 1 is a syntax error because 1; of the space between the + and the =. Assignment operators are provided for the binary operators + - * / % & | ^ << >> so that the following assignment operators are possible = += -= *= /= %= &= |= ^= <<= >>= The % is the modulo, or remainder, operator; &, |, and ^ are the bitwise logical operators AND, OR, and exclusive OR; << and >> are the left shift and right shift operators; §6.2 summarizes the operators and their meanings. For a binary operator @ applied to operands of built-in types, an expression x @= y means x =x @y except that x is evaluated once only. x@ =y x= x@ y, Chapter 8 and Chapter 9 discuss how to organize a program as a set of modules. With one exception, the declarations for this calculator example can be ordered so that everything is declared exactly once and before it is used. The exception is e xp r(), which calls t er m(), which calls ex pr te rm p ri m(), which in turn calls e xp r(). This loop must be broken somehow. A declaration pr im ex pr d ou bl e e xp r(b oo l); do ub le ex pr bo ol before the definition of p ri m() will do nicely. pr im Function t er m() handles multiplication and division in the same way e xp r() handles addition te rm ex pr and subtraction: d ou bl e t er m(b oo l g et do ub le te rm bo ol ge t) // multiply and divide { d ou bl e l ef t = p ri m(g et ; do ub le le ft pr im ge t) f or (;;) fo r sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { c as e M UL ca se MU L: l ef t *= p ri m(t ru e); le ft pr im tr ue b re ak br ea k; c as e D IV ca se DI V: i f (d ou bl e d = p ri m(t ru e)) { if do ub le pr im tr ue l ef t /= d le ft d; b re ak br ea k; } r et ur n e rr or di vi de b y 0 ; re tu rn er ro r(\"d iv id e by 0\") d ef au lt de fa ul t: r et ur n l ef t; re tu rn le ft } } The result of dividing by zero is undefined and usually disastrous. We therefore test for 0 before dividing and call e rr or if we detect a zero divisor. The function e rr or is described in §6.1.4. er ro r() er ro r() The variable d is introduced into the program exactly where it is needed and initialized immedi- ately. The scope of a name introduced in a condition is the statement controlled by that condition,
    121. Section 6.1.1 The Parser 111 and the resulting value is the value of the condition (§6.3.2.1). Consequently, the division and assignment l ef t/=d is done if and only if d is nonzero. le ft d The function p ri m() handling a primary is much like e xp r() and t er m(), except that because pr im ex pr te rm we are getting lower in the call hierarchy a bit of real work is being done and no loop is necessary: do ub le nu mb er _v al ue d ou bl e n um be r_ va lu e; st ri ng st ri ng _v al ue s tr in g s tr in g_ va lu e; d ou bl e p ri m(b oo l g et do ub le pr im bo ol ge t) // handle primaries { i f (g et g et _t ok en ; if ge t) ge t_ to ke n() sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { c as e N UM BE R: ca se NU MB ER // floating-point constant nu mb er _v al ue d ou bl e v = n um be r_ va lu e; do ub le { g et _t ok en ; ge t_ to ke n() r et ur n v re tu rn v; } c as e N AM E: ca se NA ME d ou bl e& v = t ab le st ri ng _v al ue ; do ub le ta bl e[s tr in g_ va lu e] { i f (g et _t ok en if ge t_ to ke n() == A SS IG N) v = e xp r(t ru e); AS SI GN ex pr tr ue r et ur n v re tu rn v; } c as e M IN US ca se MI NU S: // unary minus r et ur n -p ri m(t ru e); re tu rn pr im tr ue c as e L P: ca se LP d ou bl e e = e xp r(t ru e); do ub le ex pr tr ue { if cu rr _t ok i f (c ur r_ to k != R P) r et ur n e rr or RP re tu rn er ro r(\") e xp ec te d\"); ex pe ct ed g et _t ok en ; ge t_ to ke n() // eat ’)’ r et ur n e re tu rn e; } d ef au lt de fa ul t: r et ur n e rr or pr im ar y e xp ec te d\"); re tu rn er ro r(\"p ri ma ry ex pe ct ed } } When a N UM BE R (that is, an integer or floating-point literal) is seen, its value is returned. The NU MB ER input routine g et _t ok en nu mb er _v al ue ge t_ to ke n() places the value in the global variable n um be r_ va lu e. Use of a global variable in a program often indicates that the structure is not quite clean – that some sort of opti- mization has been applied. So it is here. Ideally, a lexical token consists of two parts: a value spec- ifying the kind of token (a T ok en _v al ue in this program) and (when needed) the value of the token. To ke n_ va lu e cu rr _t ok nu mb er _v al ue Here, there is only a single, simple variable, c ur r_ to k, so the global variable n um be r_ va lu e is needed to hold the value of the last N UM BE R read. Eliminating this spurious global variable is left NU MB ER nu mb er _v al ue as an exercise (§6.6[21]). Saving the value of n um be r_ va lu e in the local variable v before calling g et _t ok en ge t_ to ke n() is not really necessary. For every legal input, the calculator always uses one number in the computation before reading another from input. However, saving the value and displaying it correctly after an error helps the user. nu mb er _v al ue In the same way that the value of the last N UM BE R is kept in n um be r_ va lu e, the character NU MB ER st ri ng _v al ue string representation of the last N AM E seen is kept in s tr in g_ va lu e. Before doing anything to a NA ME
    122. 112 Expressions and Statements Chapter 6 name, the calculator must first look ahead to see if it is being assigned to or simply read. In both cases, the symbol table is consulted. The symbol table is a m ap (§3.7.4, §17.4.1): ma p m ap st ri ng do ub le t ab le ma p<s tr in g,d ou bl e> ta bl e; That is, when t ab le is indexed by a s tr in g, the resulting value is the d ou bl e corresponding to the ta bl e st ri ng do ub le s tr in g. For example, if the user enters st ri ng r ad iu s = 6 37 8.3 88 ra di us 63 78 38 8; the calculator will execute d ou bl e& v = t ab le ra di us ; do ub le ta bl e[\"r ad iu s\"] // ... expr() calculates the value to be assigned ... v = 6 37 8.3 88 63 78 38 8; The reference v is used to hold on to the d ou bl e associated with r ad iu s while e xp r() calculates the do ub le ra di us ex pr value 6 37 8.3 88 from the input characters. 63 78 38 8 6.1.2 The Input Function [expr.input] Reading input is often the messiest part of a program. This is because a program must communi- cate with a person, it must cope with that person’s whims, conventions, and seemingly random errors. Trying to force the person to behave in a manner more suitable for the machine is often (rightly) considered offensive. The task of a low-level input routine is to read characters and com- pose higher-level tokens from them. These tokens are then the units of input for higher-level rou- tines. Here, low-level input is done by g et _t ok en ge t_ to ke n(). Writing a low-level input routine need not be an everyday task. Many systems provide standard functions for this. I build g et _t ok en in two stages. First, I provide a deceptively simple version that imposes a ge t_ to ke n() burden on the user. Next, I modify it into a slightly less elegant, but much easier to use, version. The idea is to read a character, use that character to decide what kind of token needs to be com- posed, and then return the T ok en _v al ue representing the token read. To ke n_ va lu e The initial statements read the first non-whitespace character into c h and check that the read ch operation succeeded: T ok en _v al ue g et _t ok en To ke n_ va lu e ge t_ to ke n() { c ha r c h = 0 ch ar ch 0; c in ch ci n>>c h; s wi tc h (c h) { sw it ch ch c as e 0 ca se 0: re tu rn cu rr _t ok EN D; r et ur n c ur r_ to k=E ND // assign and return By default, operator >> skips whitespace (that is, spaces, tabs, newlines, etc.) and leaves the value of c h unchanged if the input operation failed. Consequently, c h==0 indicates end of input. ch ch 0 Assignment is an operator, and the result of the assignment is the value of the variable assigned EN D cu rr _t ok to. This allows me to assign the value E ND to c ur r_ to k and return it in the same statement. Hav- ing a single statement rather than two is useful in maintenance. If the assignment and the return became separated in the code, a programmer might update the one and forget to update to the other.
    123. Section 6.1.2 The Input Function 113 Let us look at some of the cases separately before considering the complete function. The expression terminator ´;´, the parentheses, and the operators are handled simply by returning their values: c as e ´;´: ca se c as e ´*´: ca se c as e ´/´: ca se c as e ´+´: ca se c as e ´-´: ca se c as e ´(´: ca se c as e ´)´: ca se c as e ´=´: ca se r et ur n c ur r_ to k=T ok en _v al ue ch ; re tu rn cu rr _t ok To ke n_ va lu e(c h) Numbers are handled like this: c as e ´0 c as e ´1 ca se 0´: ca se 1´: c as e ´2 ca se 2´: c as e ´3 ca se 3´: c as e ´4 ca se 4´: c as e ´5 ca se 5´: c as e ´6 ca se 6´: c as e ´7 ca se 7´: c as e ´8 ca se 8´: c as e ´9 ca se 9´: c as e ´.´: ca se c in pu tb ac k(c h); ci n.p ut ba ck ch nu mb er _v al ue c in >> n um be r_ va lu e; ci n re tu rn cu rr _t ok NU MB ER r et ur n c ur r_ to k=N UM BE R; Stacking c as e labels horizontally rather than vertically is generally not a good idea because this ca se arrangement is harder to read. However, having one line for each digit is tedious. Because opera- tor >> is already defined for reading floating-point constants into a d ou bl e, the code is trivial. First do ub le the initial character (a digit or a dot) is put back into c in Then the constant can be read into ci n. nu mb er _v al ue n um be r_ va lu e. A name is handled similarly: d ef au lt de fa ul t: // NAME, NAME =, or error i f (i sa lp ha ch if is al ph a(c h)) { c in pu tb ac k(c h); ci n.p ut ba ck ch c in st ri ng _v al ue ci n>>s tr in g_ va lu e; re tu rn cu rr _t ok NA ME r et ur n c ur r_ to k=N AM E; } e rr or ba d t ok en ; er ro r(\"b ad to ke n\") re tu rn cu rr _t ok PR IN T; r et ur n c ur r_ to k=P RI NT The standard library function i sa lp ha (§20.4.2) is used to avoid listing every character as a sepa- is al ph a() st ri ng _v al ue rate c as e label. Operator >> applied to a string (in this case, s tr in g_ va lu e) reads until it hits white- ca se space. Consequently, a user must terminate a name by a space before an operator using the name as an operand. This is less than ideal, so we will return to this problem in §6.1.3. Here, finally, is the complete input function: T ok en _v al ue g et _t ok en To ke n_ va lu e ge t_ to ke n() { c ha r c h = 0 ch ar ch 0; c in ch ci n>>c h;
    124. 114 Expressions and Statements Chapter 6 s wi tc h (c h) { sw it ch ch c as e 0 ca se 0: re tu rn cu rr _t ok EN D; r et ur n c ur r_ to k=E ND c as e ´;´: ca se c as e ´*´: ca se c as e ´/´: ca se c as e ´+´: ca se c as e ´-´: ca se c as e ´(´: ca se c as e ´)´: ca se c as e ´=´: ca se r et ur n c ur r_ to k=T ok en _v al ue ch ; re tu rn cu rr _t ok To ke n_ va lu e(c h) c as e ´0 ca se 0´: c as e ´1 ca se 1´: c as e ´2 ca se 2´: c as e ´3 ca se 3´: c as e ´4 ca se 4´: c as e ´5 ca se 5´: c as e ´6 ca se 6´: c as e ´7 ca se 7´: c as e ´8 ca se 8´: c as e ´9 ca se 9´: c as e ´.´: ca se c in pu tb ac k(c h); ci n.p ut ba ck ch nu mb er _v al ue c in >> n um be r_ va lu e; ci n re tu rn cu rr _t ok NU MB ER r et ur n c ur r_ to k=N UM BE R; d ef au lt de fa ul t: // NAME, NAME =, or error i f (i sa lp ha ch { if is al ph a(c h)) c in pu tb ac k(c h); ci n.p ut ba ck ch c in st ri ng _v al ue ci n>>s tr in g_ va lu e; re tu rn cu rr _t ok NA ME r et ur n c ur r_ to k=N AM E; } e rr or ba d t ok en ; er ro r(\"b ad to ke n\") re tu rn cu rr _t ok PR IN T; r et ur n c ur r_ to k=P RI NT } } The conversion of an operator to its token value is trivial because the T ok en _v al ue of an operator To ke n_ va lu e was defined as the integer value of the operator (§4.8). 6.1.3 Low-level Input [expr.low] Using the calculator as defined so far reveals a few inconveniences. It is tedious to remember to add a semicolon after an expression in order to get its value printed, and having a name terminated by whitespace only is a real nuisance. For example, x 7 is an identifier – rather than the identifier x=7 x followed by the operator = and the number 7 Both problems are solved by replacing the type- 7. oriented default input operations in g et _t ok en with code that reads individual characters. ge t_ to ke n() First, we’ll make a newline equivalent to the semicolon used to mark the end of expression: T ok en _v al ue g et _t ok en To ke n_ va lu e ge t_ to ke n() { c ha r c h; ch ar ch d o { // skip whitespace except ’\\n’ do re tu rn cu rr _t ok EN D; i f(!c in ge t(c h)) r et ur n c ur r_ to k = E ND if ci n.g et ch } w hi le (c h!=´\\ n´ && i ss pa ce ch ; wh il e ch \\n is sp ac e(c h))
    125. Section 6.1.3 Low-level Input 115 s wi tc h (c h) { sw it ch ch c as e ´;´: ca se c as e ´\\ n´: ca se \\n re tu rn cu rr _t ok PR IN T; r et ur n c ur r_ to k=P RI NT A do-statement is used; it is equivalent to a while-statement except that the controlled statement is always executed at least once. The call c in ge t(c h) reads a single character from the standard ci n.g et ch input stream into c h. By default, g et ch ge t() does not skip whitespace the way o pe ra to r >> does. The op er at or test i f (!c in ge t(c h)) fails if no character can be read from c in in this case, E ND is returned to if ci n.g et ch ci n; EN D terminate the calculator session. The operator ! (NOT) is used because g et ge t() returns t ru e in case tr ue of success. The standard library function i ss pa ce is sp ac e() provides the standard test for whitespace (§20.4.2); i ss pa ce c) returns a nonzero value if c is a whitespace character and zero otherwise. The test is is sp ac e(c implemented as a table lookup, so using i ss pa ceis sp ac e() is much faster than testing for the individual whitespace characters. Similar functions test if a character is a digit – i sd ig it is di gi t() – a letter – i sa l- is al - p ha – or a digit or letter – i sa ln um ph a() is al nu m(). After whitespace has been skipped, the next character is used to determine what kind of lexical token is coming. The problem caused by >> reading into a string until whitespace is encountered is solved by reading one character at a time until a character that is not a letter or a digit is found: d ef au lt de fa ul t: // NAME, NAME=, or error i f (i sa lp ha ch if is al ph a(c h)) { st ri ng _v al ue ch s tr in g_ va lu e = c h; st ri ng _v al ue pu sh _b ac k(c h) w hi le (c in ge t(c h) && i sa ln um ch wh il e ci n.g et ch is al nu m(c h)) s tr in g_ va lu e.p us h_ ba ck ch ; c in pu tb ac k(c h); ci n.p ut ba ck ch re tu rn cu rr _t ok NA ME r et ur n c ur r_ to k=N AM E; } e rr or ba d t ok en ; er ro r(\"b ad to ke n\") re tu rn cu rr _t ok PR IN T; r et ur n c ur r_ to k=P RI NT Fortunately, these two improvements could both be implemented by modifying a single local sec- tion of code. Constructing programs so that improvements can be implemented through local mod- ifications only is an important design aim. 6.1.4 Error Handling [expr.error] Because the program is so simple, error handling is not a major concern. The error function simply counts the errors, writes out an error message, and returns: i nt n o_ of _e rr or s; in t no _o f_ er ro rs d ou bl e e rr or co ns t s tr in g& s do ub le er ro r(c on st st ri ng s) { n o_ of _e rr or s++; no _o f_ er ro rs c er r << \"e rr or \" << s << ´\\ n´; ce rr er ro r: \\n r et ur n 1 re tu rn 1; } The stream c er r is an unbuffered output stream usually used to report errors (§21.2.1). ce rr
    126. 116 Expressions and Statements Chapter 6 The reason for returning a value is that errors typically occur in the middle of the evaluation of an expression, so we should either abort that evaluation entirely or return a value that is unlikely to cause subsequent errors. The latter is adequate for this simple calculator. Had g et _t ok en ge t_ to ke n() kept track of the line numbers, e rr or er ro r() could have informed the user approximately where the error occurred. This would be useful when the calculator is used noninteractively (§6.6[19]). Often, a program must be terminated after an error has occurred because no sensible way of continuing has been devised. This can be done by calling e xi t(), which first cleans up things like ex it output streams and then terminates the program with its argument as the return value (§9.4.1.1). More stylized error-handling mechanisms can be implemented using exceptions (see §8.3, Chapter 14), but what we have here is quite suitable for a 150-line calculator. 6.1.5 The Driver [expr.driver] With all the pieces of the program in place, we need only a driver to start things. In this simple example, m ai n() can do that: ma in i nt m ai n() in t ma in { t ab le pi ta bl e[\"p i\"] = 3 14 15 92 65 35 89 79 32 38 5; 3.1 41 59 26 53 58 97 93 23 85 // insert predefined names t ab le e\"] = 2 71 82 81 82 84 59 04 52 35 4; ta bl e[\"e 2.7 18 28 18 28 45 90 45 23 54 w hi le (c in { wh il e ci n) g et _t ok en ; ge t_ to ke n() if cu rr _t ok i f (c ur r_ to k == E ND b re ak EN D) br ea k; if cu rr _t ok i f (c ur r_ to k == P RI NT c on ti nu e; PR IN T) co nt in ue c ou t << e xp r(f al se << ´\\ n´; co ut ex pr fa ls e) \\n } r et ur n n o_ of _e rr or s; re tu rn no _o f_ er ro rs } Conventionally, m ai n() should return zero if the program terminates normally and nonzero other- ma in wise (§3.2). Returning the number of errors accomplishes this nicely. As it happens, the only initialization needed is to insert the predefined names into the symbol table. The primary task of the main loop is to read expressions and write out the answer. This is achieved by the line: c ou t << e xp r(f al se << ´\\ n´; co ut ex pr fa ls e) \\n The argument f al se tells e xp r() that it does not need to call g et _t ok en fa ls e ex pr ge t_ to ke n() to get a current token on which to work. Testing c in each time around the loop ensures that the program terminates if something goes ci n wrong with the input stream, and testing for E ND ensures that the loop is correctly exited when EN D g et _t ok en encounters end-of-file. A break-statement exits its nearest enclosing switch-statement ge t_ to ke n() or loop (that is, a for-statement, while-statement, or do-statement). Testing for P RI NT (that is, for PR IN T ´\\ n´ and ´;´) relieves e xp r() of the responsibility for handling empty expressions. A continue- \\n ex pr statement is equivalent to going to the very end of a loop, so in this case
    127. Section 6.1.5 The Driver 117 w hi le (c in { wh il e ci n) // ... if cu rr _t ok i f (c ur r_ to k == P RI NT c on ti nu e; PR IN T) co nt in ue c ou t << e xp r(f al se << ´\\ n´; co ut ex pr fa ls e) \\n } is equivalent to w hi le (c in { wh il e ci n) // ... if cu rr _t ok i f (c ur r_ to k != P RI NT PR IN T) c ou t << e xp r(f al se << ´\\ n´; co ut ex pr fa ls e) \\n } 6.1.6 Headers [expr.headers] The calculator uses standard library facilities. Therefore, appropriate headers must be #i nc lu de to in cl ud ed complete the program: #i nc lu de io st re am in cl ud e<i os tr ea m> // I/O #i nc lu de st ri ng in cl ud e<s tr in g> // strings #i nc lu de ma p> in cl ud e<m ap // map #i nc lu de cc ty pe in cl ud e<c ct yp e> // isalpha(), etc. All of these headers provide facilities in the s td namespace, so to use the names they provide we st d must either use explicit qualification with s td : or bring the names into the global namespace by st d: u si ng n am es pa ce s td us in g na me sp ac e st d; To avoid confusing the discussion of expressions with modularity issues, I did the latter. Chapter 8 and Chapter 9 discuss ways of organizing this calculator into modules using namespaces and how to organize it into source files. On many systems, standard headers have equivalents with a .h suf- h fix that declare the classes, functions, etc., and place them in the global namespace (§9.2.1, §9.2.4, §B.3.1). 6.1.7 Command-Line Arguments [expr.command] After the program was written and tested, I found it a bother to first start the program, then type the expressions, and finally quit. My most common use was to evaluate a single expression. If that expression could be presented as a command-line argument, a few keystrokes could be avoided. A program starts by calling m ai n() (§3.2, §9.4). When this is done, m ai n() is given two ma in ma in arguments specifying the number of arguments, usually called a rg c, and an array of arguments, ar gc usually called a rg v. The arguments are character strings, so the type of a rg v is c ha r*[a rg c+1 ar gv ar gv ch ar ar gc 1]. The name of the program (as it occurs on the command line) is passed as a rg v[0 so a rg c is ar gv 0], ar gc always at least 1 The list of arguments is zero-terminated; that is, a rg v[a rg c]==0 For example, 1. ar gv ar gc 0. for the command d c 1 50 1.1 93 4 dc 15 0/1 19 34 the arguments have these values:
    128. 118 Expressions and Statements Chapter 6 a rg c: ar gc 2 a rg v: ar gv 0 . . \"d c\" dc \"1 50 /1 .1 93 4\" 15 0/ 1. 19 34 Because the conventions for calling m ai n() are shared with C, C-style arrays and strings are used. ma in It is not difficult to get hold of a command-line argument. The problem is how to use it with minimal reprogramming. The idea is to read from the command string in the same way that we read from the input stream. A stream that reads from a string is unsurprisingly called an i st ri ng st re am Unfortunately, there is no elegant way of making c in refer to an i st ri ng st re am is tr in gs tr ea m. ci n is tr in gs tr ea m. Therefore, we must find a way of getting the calculator input functions to refer to an i st ri ng st re am is tr in gs tr ea m. Furthermore, we must find a way of getting the calculator input functions to refer to an i st ri ng st re am or to c in depending on what kind of command-line argument we supply. is tr in gs tr ea m ci n A simple solution is to introduce a global pointer i np ut that points to the input stream to be used in pu t and have every input routine use that: i st re am i np ut // pointer to input stream is tr ea m* in pu t; i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv { s wi tc h (a rg c) { sw it ch ar gc c as e 1 ca se 1: // read from standard input i np ut = &c in in pu t ci n; b re ak br ea k; c as e 2 ca se 2: // read argument string i np ut = n ew i st ri ng st re am ar gv 1]); in pu t ne w is tr in gs tr ea m(a rg v[1 b re ak br ea k; d ef au lt de fa ul t: e rr or to o m an y a rg um en ts ; er ro r(\"t oo ma ny ar gu me nt s\") r et ur n 1 re tu rn 1; } t ab le pi = 3 14 15 92 65 35 89 79 32 38 5; ta bl e[\"p i\"] 3.1 41 59 26 53 58 97 93 23 85 // insert predefined names t ab le e\"] = 2 71 82 81 82 84 59 04 52 35 4; ta bl e[\"e 2.7 18 28 18 28 45 90 45 23 54 w hi le (*i np ut { wh il e in pu t) g et _t ok en ; ge t_ to ke n() if cu rr _t ok i f (c ur r_ to k == E ND b re ak EN D) br ea k; if cu rr _t ok i f (c ur r_ to k == P RI NT c on ti nu e; PR IN T) co nt in ue c ou t << e xp r(f al se << ´\\ n´; co ut ex pr fa ls e) \\n } i f (i np ut != &c in d el et e i np ut if in pu t ci n) de le te in pu t; r et ur n n o_ of _e rr or s; re tu rn no _o f_ er ro rs }
    129. Section 6.1.7 Command-Line Arguments 119 An i st ri ng st re am is a kind of i st re am that reads from its character string argument (§21.5.3). is tr in gs tr ea m is tr ea m Upon reaching the end of its string, an i st ri ng st re am fails exactly like other streams do when they is tr in gs tr ea m hit the end of input (§3.6, §21.3.3). To use an i st ri ng st re am you must include <s st re am is tr in gs tr ea m, ss tr ea m>. It would be easy to modify m ai n() to accept several command-line arguments, but this does ma in not appear to be necessary, especially as several expressions can be passed as a single argument: d c \"r at e=1 19 34 15 0/r at e;1 9.7 5/r at e;2 17 ra te dc ra te 1.1 93 4;1 50 ra te 19 75 ra te 21 7/r at e\" I use quotes because ; is the command separator on my UNIX systems. Other systems have differ- ent conventions for supplying arguments to a program on startup. It was inelegant to modify all of the input routines to use *i np ut rather than c in to gain the flex- in pu t ci n ibility to use alternative sources of input. The change could have been avoided had I shown fore- sight by introducing something like i np ut from the start. A more general and useful view is to note in pu t that the source of input really should be the parameter of a calculator module. That is, the funda- mental problem with this calculator example is that what I refer to as ‘‘the calculator’’ is only a col- lection of functions and data. There is no module (§2.4) or object (§2.5.2) that explicitly represents the calculator. Had I set out to design a calculator module or a calculator type, I would naturally have considered what its parameters should be (§8.5[3], §10.6[16]). 6.1.8 A Note on Style [expr.style] To programmers unacquainted with associative arrays, the use of the standard library m ap as the ma p symbol table seems almost like cheating. It is not. The standard library and other libraries are meant to be used. Often, a library has received more care in its design and implementation than a programmer could afford for a handcrafted piece of code to be used in just one program. Looking at the code for the calculator, especially at the first version, we can see that there isn’t much traditional C-style, low-level code presented. Many of the traditional tricky details have been replaced by uses of standard library classes such as o st re am s tr in g, and m ap (§3.4, §3.5, §3.7.4, os tr ea m, st ri ng ma p Chapter 17). Note the relative scarcity of arithmetic, loops, and even assignments. This is the way things ought to be in code that doesn’t manipulate hardware directly or implement low-level abstractions. 6.2 Operator Summary [expr.operators] This section presents a summary of expressions and some examples. Each operator is followed by one or more names commonly used for it and an example of its use. In these tables, a class_name is the name of a class, a member is a member name, an object is an expression yielding a class object, a pointer is an expression yielding a pointer, an expr is an expression, and an lvalue is an expression denoting a nonconstant object. A type can be a fully general type name (with *, (), etc.) only when it appears in parentheses; elsewhere, there are restrictions (§A.5). The syntax of expressions is independent of operand types. The meanings presented here apply when the operands are of built-in types (§4.1.1). In addition, you can define meanings for operators applied to operands of user-defined types (§2.5.2, Chapter 11).
    130. 120 Expressions and Statements Chapter 6 _ ____________________________________________________________  ____________________________________________________________ _ ____________________________________________________________ _ Operator Summary   scope resolution class_name :: member    scope resolution  namespace_name :: member  global  :: name  global ____________________________________________________________ _ :: qualified-name    member selection object . member   member selection  pointer -> member  subscripting  pointer [ expr ]  function call  expr ( expr_list )  value construction  type ( expr_list )    post increment lvalue ++   post decrement  lvalue --  type identification  t yp ei d ( type ) ty pe id  run-time type identification  t yp ei d ( expr ) ty pe id  run-time checked conversion  d yn am ic _c as t < type > ( expr ) dy na mi c_ ca st   st at ic _c as t  compile-time checked conversion s ta ti c_ ca st < type > ( expr )  r ei nt er pr et _c as t < type > ( expr )   unchecked conversion re in te rp re t_ ca st  co ns t ____________________________________________________________ c on st _c as t < type > ( expr ) _c on st conversion co ns t_ ca st  size of object  s iz eo f expr si ze of    size of type s iz eo f ( type ) si ze of   pre increment  ++ lvalue  pre decrement  -- lvalue  complement  ~ expr  not  ! expr    unary minus - expr   unary plus  + expr  address of  & lvalue  dereference  ∗ expr  create (allocate)  n ew type ne w    create (allocate and initialize) n ew type ( expr-list ) ne w   create (place)  n ew ( expr-list ) type ne w  create (place and initialize) n ew ( expr-list ) type ( expr-list )  ne w  destroy (de-allocate)  d el et e pointer de le te  destroy array  d el et e[] pointer de le te   _cast (type conversion) ____________________________________________________________ ( type ) expr   member selection  object .* pointer-to-member  ____________________________________________________________ _member selection pointer ->* pointer-to-member   expr ∗ expr  multiply   divide  expr / expr  modulo (remainder) ____________________________________________________________  _  expr % expr
    131. Section 6.2 Operator Summary 121 ________________________________________ ________________________________________  ________________________________________ Operator Summary (continued)   add (plus) expr + expr   ________________________________________  subtract (minus) expr - expr  shift left  expr << expr  shift right  ________________________________________ expr >> expr   less than  expr < expr  less than or equal  expr <= expr  greater than  expr > expr  greater than or equal  ________________________________________ expr >= expr   equal  expr == expr ________________________________________  not equal expr != expr  bitwise AND  ________________________________________ expr & expr  ________________________________________  bitwise exclusive OR expr ^ expr ________________________________________  bitwise inclusive OR expr | expr   ________________________________________ logical AND expr && expr  ________________________________________  logical inclusive OR expr || expr  simple assignment  lvalue = expr   lvalue ∗= expr multiply and assign    divide and assign  lvalue /= expr  modulo and assign  lvalue %= expr  add and assign  lvalue += expr  subtract and assign  lvalue -= expr   shift left and assign lvalue <<= expr    shift right and assign lvalue >>= expr    AND and assign lvalue &= expr  inclusive OR and assign  lvalue |= expr  exclusive OR and assign  ________________________________________ lvalue ^= expr  ________________________________________  conditional expression expr ? expr : expr _______________________________________ _ throw exception t hr ow expr th ro w    comma (sequencing) ________________________________________ expr , expr Each box holds operators with the same precedence. Operators in higher boxes have higher prece- dence than operators in lower boxes. For example: a b*c means a b*c rather than (a b)*c a+b c a+(b c) a+b c because * has higher precedence than +. Unary operators and assignment operators are right-associative; all others are left-associative. For example, a b=c means a b=c a b+c means (a b)+c and *p a=b c a=(b c), a+b c a+b c, p++ means *(p p++), not p)++. (*p A few grammar rules cannot be expressed in terms of precedence (also known as binding strength) and associativity. For example, a b<c d=e f=g means a a=b c?d e:f g a=((b c)?(d e):(f g)), b<c d=e f=g but you need to look at the grammar (§A.5) to determine that.
    132. 122 Expressions and Statements Chapter 6 6.2.1 Results [expr.res] The result types of arithmetic operators are determined by a set of rules known as ‘‘the usual arith- metic conversions’’ (§C.6.3). The overall aim is to produce a result of the ‘‘largest’’ operand type. For example, if a binary operator has a floating-point operand, the computation is done using floating-point arithmetic and the result is a floating-point value. If it has a l on g operand, the com- lo ng putation is done using long integer arithmetic, and the result is a l on g. Operands that are smaller lo ng than an i nt (such as b oo l and c ha r) are converted to i nt before the operator is applied. in t bo ol ch ar in t The relational operators, ==, <=, etc., produce Boolean results. The meaning and result type of user-defined operators are determined by their declarations (§11.2). Where logically feasible, the result of an operator that takes an lvalue operand is an lvalue denoting that lvalue operand. For example: v oi d f in t x i nt y vo id f(i nt x, in t y) { i nt j = x = y in t y; // the value of x=y is the value of x after the assignment i nt p = &++x in t* x; // p points to x i nt q = &(x in t* x++); // error: x++ is not an lvalue (it is not the value stored in x) i nt p p = &(x y?x y); in t* pp x>y x:y // address of the int with the larger value } If both the second and third operands of ?: are lvalues and have the same type, the result is of that type and is an lvalue. Preserving lvalues in this way allows greater flexibility in using operators. This is particularly useful when writing code that needs to work uniformly and efficiently with both built-in and user-defined types (e.g., when writing templates or programs that generate C++ code). si ze _t The result of s iz eo f is of an unsigned integral type called s iz e_ t defined in <c st dd ef si ze of cs td de f>. The result of pointer subtraction is of a signed integral type called p tr di ff _t defined in <c st dd ef pt rd if f_ t cs td de f>. Implementations do not have to check for arithmetic overflow and hardly any do. For example: v oi d f vo id f() { i nt i = 1 in t 1; w hi le (0 < i i wh il e 0 i) i++; c ou t << \"i h as b ec om e n eg at iv e!\" << i << ´\\ n´; co ut i ha s be co me ne ga ti ve \\n } This will (eventually) try to increase i past the largest integer. What happens then is undefined, but typically the value ‘‘wraps around’’ to a negative number (on my machine -2 14 74 83 64 8). Simi- 21 47 48 36 48 larly, the effect of dividing by zero is undefined, but doing so usually causes abrupt termination of the program. In particular, underflow, overflow, and division by zero do not throw standard excep- tions (§14.10). 6.2.2 Evaluation Order [expr.evaluation] The order of evaluation of subexpressions within an expression is undefined. In particular, you cannot assume that the expression is evaluated left to right. For example: i nt x = f 2)+g 3); // undefined whether f() or g() is called first in t f(2 g(3
    133. Section 6.2.2 Evaluation Order 123 Better code can be generated in the absence of restrictions on expression evaluation order. How- ever, the absence of restrictions on evaluation order can lead to undefined results. For example, i nt i = 1 in t 1; v i] = i v[i i++; // undefined result may be evaluated as either v 1]=1 or v 2]=1 or may cause some even stranger behavior. Com- v[1 1 v[2 1 pilers can warn about such ambiguities. Unfortunately, most do not. The operators , (comma), && (logical and), and || (logical or) guarantee that their left-hand operand is evaluated before their right-hand operand. For example, b a=2 a+1 assigns 3 to b b=(a 2,a 1) b. Examples of the use of || and && can be found in §6.2.3. For built-in types, the second operand of && is evaluated only if its first operand is t ru e, and the second operand of || is evaluated only if its tr ue first operand is f al se this is sometimes called short-circuit evaluation. Note that the sequencing fa ls e; operator , (comma) is logically different from the comma used to separate arguments in a function call. Consider: f 1(v i],i f1 v[i i++); // two arguments f 2( (v i],i f2 v[i i++) ); // one argument The call of f 1 has two arguments, v i] and i f1 v[i i++, and the order of evaluation of the argument expressions is undefined. Order dependence of argument expressions is very poor style and has undefined behavior. The call of f 2 has one argument, the comma expression (v i],i f2 v[i i++), which is equivalent to i i++. Parentheses can be used to force grouping. For example, a b/c means (a b)/c so parenthe- a*b c a*b c ses must be used to get a b/c a b/c may be evaluated as (a b)/c only if the user cannot a*(b c); a*(b c) a*b c tell the difference. In particular, for many floating-point computations a b/c and (a b)/c are a*(b c) a*b c significantly different, so a compiler will evaluate such expressions exactly as written. 6.2.3 Operator Precedence [expr.precedence] Precedence levels and associativity rules reflect the most common usage. For example, i f (i 0 || m ax i) // ... if i<=0 ma x<i means ‘‘if i is less than or equal to 0 or if m ax is less than i That is, it is equivalent to ma x i.’’ i f ( (i 0) || (m ax i) ) // ... if i<=0 ma x<i and not the legal but nonsensical i f (i <= (0 ma x) < i // ... if i 0||m ax i) However, parentheses should be used whenever a programmer is in doubt about those rules. Use of parentheses becomes more common as the subexpressions become more complicated, but compli- cated subexpressions are a source of errors. Therefore, if you start feeling the need for parentheses, you might consider breaking up the expression by using an extra variable. There are cases when the operator precedence does not result in the ‘‘obvious’’ interpretation. For example: i f (i ma sk == 0 if i&m as k 0) // oops! == expression as operand for &
    134. 124 Expressions and Statements Chapter 6 This does not apply a mask to i and then test if the result is zero. Because == has higher prece- dence than &, the expression is interpreted as i ma sk 0). Fortunately, it is easy enough for a i&(m as k==0 compiler to warn about most such mistakes. In this case, parentheses are important: i f ((i ma sk == 0 // ... if i&m as k) 0) It is worth noting that the following does not work the way a mathematician might expect: i f (0 <= x <= 9 9) // ... if 0 99 This is legal, but it is interpreted as (0 x)<=9 9, where the result of the first comparison is either 0<=x 99 t ru e or f al se This Boolean value is then implicitly converted to 1 or 0 which is then compared to tr ue fa ls e. 0, 9 9, yielding t ru e. To test whether x is in the range 0 99 we might use: 99 tr ue 0..9 9, i f (0 x && x 99 // ... if 0<=x x<=9 9) A common mistake for novices is to use = (assignment) instead of == (equals) in a condition: i f (a = 7 // oops! constant assignment in condition if a 7) This is natural because = means ‘‘equals’’ in many languages. Again, it is easy for a compiler to warn about most such mistakes – and many do. 6.2.4 Bitwise Logical Operators [expr.logical] The bitwise logical operators &, |, ^, ~, >>, and << are applied to objects of integer types – that is, b oo l, c ha r, s ho rt i nt l on g, and their u ns ig ne d counterparts. The results are also integers. bo ol ch ar sh or t, in t, lo ng un si gn ed A typical use of bitwise logical operators is to implement the notion of a small set (a bit vector). In this case, each bit of an unsigned integer represents one member of the set, and the number of bits limits the number of members. The binary operator & is interpreted as intersection, | as union, ^ as symmetric difference, and ~ as complement. An enumeration can be used to name the mem- bers of such a set. Here is a small example borrowed from an implementation of o st re am os tr ea m: e nu m i os _b as e::i os ta te { en um io s_ ba se io st at e g oo db it 0, e of bi t=1 f ai lb it 2, b ad bi t=4 go od bi t=0 eo fb it 1, fa il bi t=2 ba db it 4 }; The implementation of a stream can set and test its state like this: s ta te = g oo db it st at e go od bi t; // ... i f (s ta te ba db it fa il bi t)) // stream no good if st at e&(b ad bi t|f ai lb it The extra parentheses are necessary because & has higher precedence than |. A function that reaches the end of input might report it like this: s ta te |= e of bi t; st at e eo fb it The |= operator is used to add to the state. A simple assignment, s ta te eo fb it would have cleared st at e=e of bi t, all other bits. These stream state flags are observable from outside the stream implementation. For example, we could see how the states of two streams differ like this:
    135. Section 6.2.4 Bitwise Logical Operators 125 i nt d if f = c in rd st at e()^c ou t.r ds ta te ; in t di ff ci n.r ds ta te co ut rd st at e() // rdstate() returns the state Computing differences of stream states is not very common. For other similar types, computing differences is essential. For example, consider comparing a bit vector that represents the set of interrupts being handled with another that represents the set of interrupts waiting to be handled. Please note that this bit fiddling is taken from the implementation of iostreams rather than from the user interface. Convenient bit manipulation can be very important, but for reliability, maintain- ability, portability, etc., it should be kept at low levels of a system. For more general notions of a set, see the standard library s et (§17.4.3), b it se t (§17.5.3), and v ec to r<b oo l> (§16.3.11). se t bi ts et ve ct or bo ol Using fields (§C.8.1) is really a convenient shorthand for shifting and masking to extract bit fields from a word. This can, of course, also be done using the bitwise logical operators. For example, one could extract the middle 16 bits of a 32-bit l on g like this: lo ng u ns ig ne d s ho rt m id dl e(l on g a { r et ur n (a 8)&0 xf ff f; } un si gn ed sh or t mi dd le lo ng a) re tu rn a>>8 0x ff ff Do not confuse the bitwise logical operators with the logical operators: &&, ||, and ! . The latter return either t ru e or f al se and they are primarily useful for writing the test in an i f, w hi le or f or tr ue fa ls e, if wh il e, fo r statement (§6.3.2, §6.3.3). For example, !0 (not zero) is the value t ru e, whereas ~0 (complement 0 tr ue 0 of zero) is the bit pattern all-ones, which in two’s complement representation is the value -1 1. 6.2.5 Increment and Decrement [expr.incr] The ++ operator is used to express incrementing directly, rather than expressing it indirectly using a combination of an addition and an assignment. By definition, ++l va lu e means l va lu e+=1 which lv al ue lv al ue 1, again means l va lu e=l va lu e+1 provided l va lu e has no side effects. The expression denoting the lv al ue lv al ue 1 lv al ue object to be incremented is evaluated once (only). Decrementing is similarly expressed by the -- operator. The operators ++ and -- can be used as both prefix and postfix operators. The value of ++x is the new (that is, incremented) value of x For example, y x x. y=++x is equivalent to y x+=1 x y=(x 1). The value of x x++, however, is the old value of x For example, y x++ is equivalent to x. y=x y t=x x+=1 t), where t is a variable of the same type as x y=(t x,x 1,t x. Like addition and subtraction of pointers, ++ and -- on pointers operate in terms of elements of the array into which the pointer points; p makes p point to the next element (§5.3.1). p++ The increment operators are particularly useful for incrementing and decrementing variables in loops. For example, one can copy a zero-terminated string like this: v oi d c py ch ar p c on st c ha r* q vo id cp y(c ha r* p, co ns t ch ar q) { w hi le (*p = *q wh il e p++ q++) ; } Like C, C++ is both loved and hated for enabling such terse, expression-oriented coding. Because w hi le (*p = *q wh il e p++ q++) ; is more than a little obscure to non-C programmers and because the style of coding is not uncom- mon in C and C++, it is worth examining more closely. Consider first a more traditional way of copying an array of characters:
    136. 126 Expressions and Statements Chapter 6 i nt l en gt h = s tr le n(q ; in t le ng th st rl en q) f or (i nt i = 0 i le ng th i fo r in t 0; i<=l en gt h; i++) p i] = q i]; p[i q[i This is wasteful. The length of a zero-terminated string is found by reading the string looking for the terminating zero. Thus, we read the string twice: once to find its length and once to copy it. So we try this instead: i nt i in t i; f or (i = 0 q i]!=0 ; i fo r i 0; q[i 0 i++) p i] = q i]; p[i q[i p i] = 0 // terminating zero p[i 0; The variable i used for indexing can be eliminated because p and q are pointers: w hi le (*q != 0 { wh il e q 0) p q; *p = *q p p++; // point to next character q q++; // point to next character } *p = 0 p 0; // terminating zero Because the post-increment operation allows us first to use the value and then to increment it, we can rewrite the loop like this: w hi le (*q != 0 { wh il e q 0) p++ = *q q++; *p } *p = 0 // terminating zero p 0; The value of *p = *q is *q We can therefore rewrite the example like this: p++ q++ q. w hi le ((*p wh il e p++ = *q q++) != 0 { } 0) In this case, we don’t notice that *q is zero until we already have copied it into *p and incremented q p p Consequently, we can eliminate the final assignment of the terminating zero. Finally, we can p. reduce the example further by observing that we don’t need the empty block and that the ‘‘!= 0 is 0’’ redundant because the result of a pointer or integral condition is always compared to zero anyway. Thus, we get the version we set out to discover: w hi le (*p wh il e p++ = *q q++) ; Is this version less readable than the previous versions? Not to an experienced C or C++ program- mer. Is this version more efficient in time or space than the previous versions? Except for the first version that called s tr le n(), not really. Which version is the most efficient will vary among st rl en machine architectures and among compilers. The most efficient way of copying a zero-terminated character string for your particular machine ought to be the standard string copy function: // from <string.h> c ha r* s tr cp y(c ha r*, c on st c ha r*); ch ar st rc py ch ar co ns t ch ar For more general copying, the standard c op y algorithm (§2.7.2, §18.6.1) can be used. Whenever co py possible, use standard library facilities in preference to fiddling with pointers and bytes. Standard library functions may be inlined (§7.1.1) or even implemented using specialized machine
    137. Section 6.2.5 Increment and Decrement 127 instructions. Therefore, you should measure carefully before believing that some piece of hand- crafted code outperforms library functions. 6.2.6 Free Store [expr.free] A named object has its lifetime determined by its scope (§4.9.4). However, it is often useful to cre- ate an object that exists independently of the scope in which it was created. In particular, it is com- mon to create objects that can be used after returning from the function in which they were created. The operator n ew creates such objects, and the operator d el et e can be used to destroy them. ne w de le te Objects allocated by n ew are said to be ‘‘on the free store’’ (also, to be ‘‘heap objects,’’ or ‘‘allo- ne w cated in dynamic memory’’). Consider how we might write a compiler in the style used for the desk calculator (§6.1). The syntax analysis functions might build a tree of the expressions for use by the code generator: s tr uc t E no de { st ru ct En od e T ok en _v al ue o pe r; To ke n_ va lu e op er E no de l ef t; En od e* le ft E no de r ig ht En od e* ri gh t; // ... }; E no de e xp r(b oo l g et En od e* ex pr bo ol ge t) { E no de l ef t = t er m(g et ; En od e* le ft te rm ge t) f or (;;) fo r sw it ch cu rr _t ok s wi tc h(c ur r_ to k) { c as e P LU S: ca se PL US c as e M IN US ca se MI NU S: E no de n = n ew E no de En od e* ne w En od e; // create an Enode on free store { n->o pe r cu rr _t ok n op er = c ur r_ to k; n le ft = l ef t; n->l ef t le ft n ri gh t = t er m(t ru e); n->r ig ht te rm tr ue l ef t = n le ft n; b re ak br ea k; } d ef au lt de fa ul t: r et ur n l ef t; re tu rn le ft // return node } } A code generator would then use the resulting nodes and delete them: v oi d g en er at e(E no de n vo id ge ne ra te En od e* n) { s wi tc h (n op er { sw it ch n->o pe r) c as e P LU S: ca se PL US // ... d el et e n // delete an Enode from the free store de le te n; } }
    138. 128 Expressions and Statements Chapter 6 An object created by n ew exists until it is explicitly destroyed by d el et e. Then, the space it occu- ne w de le te pied can be reused by n ew A C++ implementation does not guarantee the presence of a ‘‘garbage ne w. collector’’ that looks out for unreferenced objects and makes them available to n ew for reuse. Con- ne w sequently, I will assume that objects created by n ew are manually freed using d el et e. If a garbage ne w de le te collector is present, the d el et es can be omitted in most cases (§C.9.1). de le te The d el et e operator may be applied only to a pointer returned by n ew or to zero. Applying de le te ne w d el et e to zero has no effect. de le te More specialized versions of operator n ew can also be defined (§15.6). ne w 6.2.6.1 Arrays [expr.array] Arrays of objects can also be created using n ew For example: ne w. ch ar sa ve _s tr in g(c on st ch ar p) c ha r* s av e_ st ri ng co ns t c ha r* p { c ha r* s = n ew c ha r[s tr le n(p 1]; ch ar ne w ch ar st rl en p)+1 s tr cp y(s p); st rc py s,p // copy from p to s r et ur n s re tu rn s; } i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv { i f (a rg c < 2 e xi t(1 ; if ar gc 2) ex it 1) sa ve _s tr in g(a rg v[1 c ha r* p = s av e_ st ri ng ar gv 1]); ch ar // ... d el et e[] p de le te p; } The ‘‘plain’’ operator d el et e is used to delete individual objects; d el et e[] is used to delete arrays. de le te de le te To deallocate space allocated by n ew d el et e and d el et e[] must be able to determine the size of ne w, de le te de le te the object allocated. This implies that an object allocated using the standard implementation of n ew will occupy slightly more space than a static object. Typically, one word is used to hold the ne w object’s size. Note that a v ec to r (§3.7.1, §16.3) is a proper object and can therefore be allocated and deallo- ve ct or cated using plain n ew and d el et e. For example: ne w de le te v oi d f in t n vo id f(i nt n) { v ec to r<i nt ve ct or in t>* p = n ew v ec to r<i nt n); ne w ve ct or in t>(n // individual object i nt q = n ew i nt n]; in t* ne w in t[n // array // ... d el et e p de le te p; d el et e[] q de le te q; }
    139. Section 6.2.6.2 Memory Exhaustion 129 6.2.6.2 Memory Exhaustion [expr.exhaust] The free store operators n ew d el et e, n ew ne w, de le te ne w[], and d el et e[] are implemented using functions: de le te v oi d* o pe ra to r n ew si ze _t ; vo id op er at or ne w(s iz e_ t) // space for individual object v oi d o pe ra to r d el et e(v oi d*); vo id op er at or de le te vo id si ze _t v oi d* o pe ra to r n ew vo id op er at or ne w[](s iz e_ t); // space for array v oi d o pe ra to r d el et e[](v oi d*); vo id op er at or de le te vo id When operator n ew needs to allocate space for an object, it calls o pe ra to r n ew to allocate a suit- ne w op er at or ne w() able number of bytes. Similarly, when operator n ew needs to allocate space for an array, it calls ne w o pe ra to r n ew op er at or ne w[](). The standard implementations of o pe ra to r n ew op er at or ne w() and o pe ra to r n ew op er at or ne w[]() do not initialize the memory returned. What happens when n ew can find no store to allocate? By default, the allocator throws a ne w b ad _a ll oc exception. For example: ba d_ al lo c v oi d f vo id f() { t ry { tr y f or fo r(;;) n ew c ha r[1 00 00 ; ne w ch ar 10 00 0] } ca tc h(b ad _a ll oc c at ch ba d_ al lo c) { c er r << \"M em or y e xh au st ed \\n ce rr Me mo ry ex ha us te d!\\ n\"; } } However much memory we have available, this will eventually invoke the b ad _a ll oc handler. ba d_ al lo c We can specify what n ew should do upon memory exhaustion. When n ew fails, it first calls a ne w ne w function specified by a call to s et _n ew _h an dl er declared in <n ew if any. For example: se t_ ne w_ ha nd le r() ne w>, v oi d o ut _o f_ st or e() vo id ou t_ of _s to re { c er r << \"o pe ra to r n ew f ai le d: o ut o f s to re \\n ce rr op er at or ne w fa il ed ou t of st or e\\ n\"; t hr ow b ad _a ll oc ; th ro w ba d_ al lo c() } i nt m ai n() in t ma in { s et _n ew _h an dl er ou t_ of _s to re ; // make out_of_store the new_handler se t_ ne w_ ha nd le r(o ut _o f_ st or e) f or (;;) n ew c ha r[1 00 00 ; fo r ne w ch ar 10 00 0] c ou t << \"d on e\\ n\"; co ut do ne \\n } This will never get to write d on e. Instead, it will write do ne o pe ra to r n ew f ai le d: o ut o f s to re op er at or ne w fa il ed ou t of st or e See §14.4.5 for a plausible implementation of an o pe ra to r n ew op er at or ne w() that checks to see if there is a new handler to call and that throws b ad _a ll oc if not. A n ew _h an dl er might do something more ba d_ al lo c ne w_ ha nd le r clever than simply terminating the program. If you know how n ew and d el et e work – for example, ne w de le te
    140. 130 Expressions and Statements Chapter 6 because you provided your own o pe ra to r n ew op er at or ne w() and o pe ra to r d el et e() – the handler might op er at or de le te attempt to find some memory for n ew to return. In other words, a user might provide a garbage ne w collector, thus rendering the use of d el et e optional. Doing this is most definitely not a task for a de le te beginner, though. For almost everybody who needs an automatic garbage collector, the right thing to do is to acquire one that has already been written and tested (§C.9.1). By providing a n ew _h an dl er we take care of the check for memory exhaustion for every ordi- ne w_ ha nd le r, nary use of n ew in the program. Two alternative ways of controlling memory allocation exist. We ne w can either provide nonstandard allocation and deallocation functions (§15.6) for the standard uses of n ew or rely on additional allocation information provided by the user (§10.4.11, §19.4.5). ne w 6.2.7 Explicit Type Conversion [expr.cast] Sometimes, we have to deal with‘‘raw memory;’’ that is, memory that holds or will hold objects of a type not known to the compiler. For example, a memory allocator may return a v oi d* pointing to vo id newly allocated memory or we might want to state that a given integer value is to be treated as the address of an I/O device: vo id ma ll oc si ze _t v oi d* m al lo c(s iz e_ t); v oi d f vo id f() { st at ic _c as t<i nt i nt p = s ta ti c_ ca st in t*>(m al lo c(1 00 ; in t* ma ll oc 10 0)) // new allocation used as ints I O_ de vi ce d 1 = r ei nt er pr et _c as t<I O_ de vi ce IO _d ev ic e* d1 re in te rp re t_ ca st IO _d ev ic e*>(0 Xf f0 0); 0X ff 00 // device at 0Xff00 // ... } A compiler does not know the type of the object pointed to by the v oi d*. Nor can it know whether vo id the integer 0 Xf f0 0 is a valid address. Consequently, the correctness of the conversions are com- 0X ff 00 pletely in the hands of the programmer. Explicit type conversion, often called casting, is occasion- ally essential. However, traditionally it is seriously overused and a major source of errors. st at ic _c as t The s ta ti c_ ca st operator converts between related types such as one pointer type to another, an enumeration to an integral type, or a floating-point type to an integral type. The r ei nt er pr et _c as t re in te rp re t_ ca st handles conversions between unrelated types such as an integer to a pointer. This distinction st at ic _c as t allows the compiler to apply some minimal type checking for s ta ti c_ ca st and makes it easier for a programmer to find the more dangerous conversions represented as r ei nt er pr et _c as ts. Some re in te rp re t_ ca st s ta ti c_ ca st are portable, but few r ei nt er pr et _c as ts are. Hardly any guarantees are made for st at ic _c as ts re in te rp re t_ ca st r ei nt er pr et _c as t, but generally it produces a value of a new type that has the same bit pattern as its re in te rp re t_ ca st argument. If the target has at least as many bits as the original value, we can r ei nt er pr et _c as t the re in te rp re t_ ca st result back to its original type and use it. The result of a r ei nt er pr et _c as t is guaranteed to be re in te rp re t_ ca st usable only if its result type is the exact type used to define the value involved. Note that r ei nt er pr et _c as t is the kind of conversion that must be used for pointers to functions (§7.7). re in te rp re t_ ca st If you feel tempted to use an explicit type conversion, take the time to consider if it is really necessary. In C++, explicit type conversion is unnecessary in most cases when C needs it (§1.6) and also in many cases in which earlier versions of C++ needed it (§1.6.2, §B.2.3). In many pro- grams, explicit type conversion can be completely avoided; in others, its use can be localized to a few routines. In this book, explicit type conversion is used in realistic situations in §6.2.7, §7.7, §13.5, §15.4, and §25.4.1, only.
    141. Section 6.2.7 Explicit Type Conversion 131 A form of run-time checked conversion, d yn am ic _c as t (§15.4.1), and a cast for removing c on st dy na mi c_ ca st co ns t qualifiers, c on st _c as t (§15.4.2.1), are also provided. co ns t_ ca st From C, C++ inherited the notation (T e, which performs any conversion that can be expressed T)e as a combination of s ta ti c_ ca st r ei nt er pr et _c as ts, and c on st _c as ts to make a value of type T st at ic _c as ts, re in te rp re t_ ca st co ns t_ ca st from the expression e (§B.2.3). This C-style cast is far more dangerous than the named conversion operators because the notation is harder to spot in a large program and the kind of conversion intended by the programmer is not explicit. That is, (T e might be doing a portable conversion T)e between related types, a nonportable conversion between unrelated types, or removing the c on st co ns t modifier from a pointer type. Without knowing the exact types of T and e you cannot tell. e, 6.2.8 Constructors [expr.ctor] The construction of a value of type T from a value e can be expressed by the functional notation T e). For example: T(e v oi d f do ub le d vo id f(d ou bl e d) { i nt i = i nt d); in t in t(d // truncate d c om pl ex z = c om pl ex d); // make a complex from d co mp le x co mp le x(d // ... } The T e) construct is sometimes referred to as a function-style cast. For a built-in type T T e) is T(e T, T(e st at ic _c as t<T e). equivalent to s ta ti c_ ca st T>(e Unfortunately, this implies that the use of T e) is not always T(e safe. For arithmetic types, values can be truncated and even explicit conversion of a longer integer type to a shorter (such as l on g to c ha r) can result in undefined behavior. I try to use the notation lo ng ch ar exclusively where the construction of a value is well-defined; that is, for narrowing arithmetic con- versions (§C.6), for conversion from integers to enumerations (§4.8), and the construction of objects of user-defined types (§2.5.2, §10.2.3). Pointer conversions cannot be expressed directly using the T e) notation. For example, T(e c ha r*(2 is a syntax error. Unfortunately, the protection that the constructor notation provides ch ar 2) against such dangerous conversions can be circumvented by using t yp ed ef names (§4.9.7) for ty pe de f pointer types. The constructor notation T is used to express the default value of type T For example: T() T. v oi d f do ub le d vo id f(d ou bl e d) { i nt j = i nt ; in t in t() // default int value c om pl ex z = c om pl ex ; co mp le x co mp le x() // default complex value // ... } The value of an explicit use of the constructor for a built-in type is 0 converted to that type (§4.9.5). Thus, i nt in t() is another way of writing 0 For a user-defined type T T 0. T, T() is defined by the default constructor (§10.4.2), if any. The use of the constructor notation for built-in types is particularly important when writing tem- plates. Then, the programmer does not know whether a template parameter will refer to a built-in type or a user-defined type (§16.3.4, §17.4.1.2).
    142. 132 Expressions and Statements Chapter 6 6.3 Statement Summary [expr.stmts] Here are a summary and some examples of C++ statements: _ ______________________________________________________________  ______________________________________________________________ _ _ ______________________________________________________________ Statement Syntax   statement:    declaration   { statement-listopt }   t ry { statement-listopt } handler-list tr y   expressionopt ;       i f ( condition ) statement if   i f ( condition ) statement e ls e statement if el se   s wi tc h ( condition ) statement sw it ch     w hi le ( condition ) statement wh il e     d o statement w hi le ( expression ) ; do wh il e  f or ( for-init-statement conditionopt ; expressionopt ) statement  fo r     c as e constant-expression : statement ca se   d ef au lt : statement de fa ul t     b re ak ; br ea k   c on ti nu e ; co nt in ue     r et ur n expressionopt ; re tu rn       g ot o identifier ; go to   identifier : statement    statement-list:    statement statement-listopt      condition:    expression   type-specifier declarator = expression      handler-list:    c at ch ( exception-declaration ) { statement-listopt } ca tc h  ______________________________________________________________  _  handler-list handler-listopt Note that a declaration is a statement and that there is no assignment statement or procedure call statement; assignments and function calls are expressions. The statements for handling exceptions, try-blocks, are described in §8.3.1.
    143. Section 6.3.1 Declarations as Statements 133 6.3.1 Declarations as Statements [expr.dcl] A declaration is a statement. Unless a variable is declared s ta ti c, its initializer is executed when- st at ic ever the thread of control passes through the declaration (see also §10.4.8). The reason for allow- ing declarations wherever a statement can be used (and a few other places; §6.3.2.1, §6.3.3.1) is to enable the programmer to minimize the errors caused by uninitialized variables and to allow better locality in code. There is rarely a reason to introduce a variable before there is a value for it to hold. For example: v oi d f ve ct or st ri ng vo id f(v ec to r<s tr in g>& v i nt i c on st c ha r* p v, in t i, co ns t ch ar p) { i f (p 0) r et ur n; if p==0 re tu rn i f (i 0 || v si ze if i<0 v.s iz e()<=i e rr or ba d i nd ex ; i) er ro r(\"b ad in de x\") s tr in g s = v i]; st ri ng v[i i f (s == p { if s p) // ... } // ... } The ability to place declarations after executable code is essential for many constants and for single-assignment styles of programming where a value of an object is not changed after initial- ization. For user-defined types, postponing the definition of a variable until a suitable initializer is available can also lead to better performance. For example, s tr in g s /* ... */ s = \"T he b es t i s t he e ne my o f t he g oo d.\"; st ri ng s; Th e be st is th e en em y of th e go od can easily be much slower than s tr in g s = \"V ol ta ir e\"; st ri ng Vo lt ai re The most common reason to declare a variable without an initializer is that it requires a statement to initialize it. Examples are input variables and arrays. 6.3.2 Selection Statements [expr.select] A value can be tested by either an i f statement or a s wi tc h statement: if sw it ch i f ( condition ) statement if i f ( condition ) statement e ls e statement if el se s wi tc h ( condition ) statement sw it ch The comparison operators == != < <= > >= return the b oo l t ru e if the comparison is true and f al se otherwise. bo ol tr ue fa ls e In an i f statement, the first (or only) statement is executed if the expression is nonzero and the if second statement (if it is specified) is executed otherwise. This implies that any arithmetic or pointer expression can be used as a condition. For example, if x is an integer, then i f (x // ... if x)
    144. 134 Expressions and Statements Chapter 6 means i f (x != 0 // ... if x 0) For a pointer p p, i f (p // ... if p) is a direct statement of the test ‘‘does p point to a valid object,’’ whereas i f (p != 0 // ... if p 0) states the same question indirectly by comparing to a value known not to point to an object. Note that the representation of the pointer 0 is not all-zeros on all machines (§5.1.1). Every compiler I have checked generated the same code for both forms of the test. The logical operators && || ! are most commonly used in conditions. The operators && and || will not evaluate their second argument unless doing so is necessary. For example, i f (p && 1 p->c ou nt // ... if p 1<p co un t) first tests that p is nonzero. It tests 1 p->c ou nt only if p is nonzero. 1<p co un t Some if-statements can conveniently be replaced by conditional-expressions. For example, i f (a <= b if a b) m ax = b ma x b; e ls e el se m ax = a ma x a; is better expressed like this: m ax = (a b) ? b : a ma x a<=b a; The parentheses around the condition are not necessary, but I find the code easier to read when they are used. A switch-statement can alternatively be written as a set of i f-s ta te me nt For example, if st at em en ts. s wi tc h (v al { sw it ch va l) c as e 1 ca se 1: f; f() b re ak br ea k; c as e 2 ca se 2: g; g() b re ak br ea k; d ef au lt de fa ul t: h; h() b re ak br ea k; } could alternatively be expressed as
    145. Section 6.3.2 Selection Statements 135 i f (v al == 1 if va l 1) f; f() e ls e i f (v al == 2 el se if va l 2) g; g() e ls e el se h; h() The meaning is the same, but the first (s wi tc h) version is preferred because the nature of the opera- sw it ch tion (testing a value against a set of constants) is explicit. This makes the s wi tc h statement easier sw it ch to read for nontrivial examples. It can also lead to the generation of better code. Beware that a case of a switch must be terminated somehow unless you want to carry on execut- ing the next case. Consider: s wi tc h (v al { sw it ch va l) // beware c as e 1 ca se 1: c ou t << \"c as e 1 \\n co ut ca se 1\\ n\"; c as e 2 ca se 2: c ou t << \"c as e 2 \\n co ut ca se 2\\ n\"; d ef au lt de fa ul t: c ou t << \"d ef au lt c as e n ot f ou nd \\n co ut de fa ul t: ca se no t fo un d\\ n\"; } Invoked with v al 1, this prints va l==1 c as e 1 ca se c as e 2 ca se d ef au lt c as e n ot f ou nd de fa ul t: ca se no t fo un d to the great surprise of the uninitiated. It is a good idea to comment the (rare) cases in which a fall-through is intentional so that an uncommented fall-through can be assumed to be an error. A b re ak is the most common way of terminating a case, but a r et ur n is often useful (§6.1.1). br ea k re tu rn 6.3.2.1 Declarations in Conditions [expr.cond] To avoid accidental misuse of a variable, it is usually a good idea to introduce the variable into the smallest scope possible. In particular, it is usually best to delay the definition of a local variable until one can give it an initial value. That way, one cannot get into trouble by using the variable before its initial value is assigned. One of the most elegant applications of these two principles is to declare a variable in a condi- tion. Consider: i f (d ou bl e d = p ri m(t ru e)) { if do ub le pr im tr ue l ef t /= d le ft d; b re ak br ea k; } Here, d is declared and initialized and the value of d after initialization is tested as the value of the condition. The scope of d extends from its point of declaration to the end of the statement that the condition controls. For example, had there been an e ls e-branch to the if-statement, d would be in el se scope on both branches.
    146. 136 Expressions and Statements Chapter 6 The obvious and traditional alternative is to declare d before the condition. However, this opens the scope (literally) for the use of d before its initialization or after its intended useful life: d ou bl e d do ub le d; // ... d2 = d d2 d; // oops! // ... i f (d = p ri m(t ru e)) { if d pr im tr ue l ef t /= d le ft d; b re ak br ea k; } // ... d = 2 0; // two unrelated uses of d 2.0 In addition to the logical benefits of declaring variables in conditions, doing so also yields the most compact source code. A declaration in a condition must declare and initialize a single variable or c on st co ns t. 6.3.3 Iteration Statements [expr.loop] A loop can be expressed as a f or w hi le or d o statement: fo r, wh il e, do w hi le ( c on di ti on ) s ta te me nt wh il e co nd it io n st at em en t d o s ta te me nt w hi le ( e xp re ss io n ) ; do st at em en t wh il e ex pr es si on f or ( f or in it st at em en t c on di ti ono ptt ; e xp re ss io no ptt ) s ta te me nt fo r fo r-i ni t-s ta te me nt co nd it io nop ex pr es si on op st at em en t Each of these statements executes a statement (called the controlled statement or the body of the loop) repeatedly until the condition becomes false or the programmer breaks out of the loop some other way. The for-statement is intended for expressing fairly regular loops. The loop variable, the termi- nation condition, and the expression that updates the loop variable can be presented ‘‘up front’’ on a single line. This can greatly increase readability and thereby decrease the frequency of errors. If no initialization is needed, the initializing statement can be empty. If the condition is omitted, the for-statement will loop forever unless the user explicitly exits it by a b re ak r et ur n, g ot o, t hr ow or br ea k, re tu rn go to th ro w, some less obvious way such as a call of e xi t() (§9.4.1.1). If the expression is omitted, we must ex it update some form of loop variable in the body of the loop. If the loop isn’t of the simple ‘‘intro- duce a loop variable, test the condition, update the loop variable’’ variety, it is often better expressed as a while-statement. A for-statement is also useful for expressing a loop without an explicit termination condition: f or fo r(;;) { // ‘‘forever’’ // ... } A while-statement simply executes its controlled statement until its condition becomes f al se I tend fa ls e. to prefer while-statements over for-statements when there isn’t an obvious loop variable or where the update of a loop variable naturally comes in the middle of the loop body. An input loop is an example of a loop where there is no obvious loop variable:
    147. Section 6.3.3 Iteration Statements 137 w hi le ci n>>c h) // ... wh il e(c in ch In my experience, the do-statement is a source of errors and confusion. The reason is that its body is always executed once before the condition is evaluated. However, for the body to work cor- rectly, something very much like the condition must hold even the first time through. More often than I would have guessed, I have found that condition not to hold as expected either when the pro- gram was first written and tested or later after the code preceding it has been modified. I also prefer the condition ‘‘up front where I can see it.’’ Consequently, I tend to avoid do-statements. 6.3.3.1 Declarations in For-Statements [expr.for] A variable can be declared in the initializer part of a for-statement. If that initializer is a declara- tion, the variable (or variables) it introduces is in scope until the end of the for-statement. For example: v oi d f in t v , i nt m ax vo id f(i nt v[] in t ma x) { f or (i nt i = 0 i ma x; i fo r in t 0; i<m ax i++) v i] = i i; v[i i*i } If the final value of an index needs to be known after exit from a f or fo r-loop, the index variable must be declared outside the f or fo r-loop (e.g., §6.3.4). 6.3.4 Goto [expr.goto] C++ possesses the infamous g ot o: go to g ot o identifier ; go to identifier : statement The g ot o has few uses in general high-level programming, but it can be very useful when C++ code go to is generated by a program rather than written directly by a person; for example, g ot os can be used go to in a parser generated from a grammar by a parser generator. The g ot o can also be important in the go to rare cases in which optimal efficiency is essential, for example, in the inner loop of some real-time application. One of the few sensible uses of g ot o in ordinary code is to break out from a nested loop or go to switch-statement (a b re ak breaks out of only the innermost enclosing loop or switch-statement). br ea k For example: v oi d f vo id f() { i nt i in t i; i nt j in t j;
    148. 138 Expressions and Statements Chapter 6 f or (i = 0 i n; i fo r i 0; i<n i++) f or (j = 0 j m; j fo r j 0; j<m j++) i f (n m[i j] == a g ot o f ou nd if nm i][j a) go to fo un d; // not found // ... f ou nd fo un d: // nm[i][j] == a } There is also a c on ti nu e statement that, in effect, goes to the end of a loop statement, as explained co nt in ue in §6.1.5. 6.4 Comments and Indentation [expr.comment] Judicious use of comments and consistent use of indentation can make the task of reading and understanding a program much more pleasant. Several different consistent styles of indentation are in use. I see no fundamental reason to prefer one over another (although, like most programmers, I have my preferences, and this book reflects them). The same applies to styles of comments. Comments can be misused in ways that seriously affect the readability of a program. The com- piler does not understand the contents of a comment, so it has no way of ensuring that a comment [1] is meaningful, [2] describes the program, and [3] is up to date. Most programs contain comments that are incomprehensible, ambiguous, and just plain wrong. Bad comments can be worse than no comments. If something can be stated in the language itself, it should be, and not just mentioned in a com- ment. This remark is aimed at comments such as these: // variable \"v\" must be initialized // variable \"v\" must be used only by function \"f()\" // call function \"init()\" before calling any other function in this file // call function \"cleanup()\" at the end of your program // don’t use function \"weird()\" // function \"f()\" takes two arguments Such comments can often be rendered unnecessary by proper use of C++. For example, one might utilize the linkage rules (§9.2) and the visibility, initialization, and cleanup rules for classes (see §10.4.1) to make the preceding examples redundant. Once something has been stated clearly in the language, it should not be mentioned a second time in a comment. For example: a = b c; // a becomes b+c b+c c ou nt co un t++; // increment the counter Such comments are worse than simply redundant. They increase the amount of text the reader has to look at, they often obscure the structure of the program, and they may be wrong. Note, however,
    149. Section 6.4 Comments and Indentation 139 that such comments are used extensively for teaching purposes in programming language textbooks such as this. This is one of the many ways a program in a textbook differs from a real program. My preference is for: [1] A comment for each source file stating what the declarations in it have in common, refer- ences to manuals, general hints for maintenance, etc. [2] A comment for each class, template, and namespace [3] A comment for each nontrivial function stating its purpose, the algorithm used (unless it is obvious), and maybe something about the assumptions it makes about its environment [4] A comment for each global and namespace variable and constant [5] A few comments where the code is nonobvious and/or nonportable [6] Very little else For example: tbl.c: Implementation of the symbol table. // /* Gaussian elimination with partial pivoting. See Ralston: \"A first course ...\" pg 411. */ swap() assumes the stack layout of an SGI R6000. // /*********************************** Copyright (c) 1997 AT&T, Inc. All rights reserved ************************************/ A well-chosen and well-written set of comments is an essential part of a good program. Writing good comments can be as difficult as writing the program itself. It is an art well worth cultivating. Note also that if // comments are used exclusively in a function, then any part of that function can be commented out using /* */ style comments, and vice versa. 6.5 Advice [expr.advice] [1] Prefer the standard library to other libraries and to ‘‘handcrafted code;’’ §6.1.8. [2] Avoid complicated expressions; §6.2.3. [3] If in doubt about operator precedence, parenthesize; §6.2.3. [4] Avoid explicit type conversion (casts); §6.2.7. [5] When explicit type conversion is necessary, prefer the more specific cast operators to the C- style cast; §6.2.7. [6] Use the T e) notation exclusively for well-defined construction; §6.2.8. T(e [7] Avoid expressions with undefined order of evaluation; §6.2.2. [8] Avoid g ot o; §6.3.4. go to [9] Avoid do-statements; §6.3.3. [10] Don’t declare a variable until you have a value to initialize it with; §6.3.1, §6.3.2.1, §6.3.3.1.
    150. 140 Expressions and Statements Chapter 6 [11] Keep comments crisp; §6.4. [12] Maintain a consistent indentation style; §6.4. [13] Prefer defining a member o pe ra to r n ew op er at or ne w() (§15.6) to replacing the global o pe ra to r n ew op er at or ne w(); §6.2.6.2. [14] When reading input, always consider ill-formed input; §6.1.3. 6.6 Exercises [expr.exercises] 1. (∗1) Rewrite the following f or statement as an equivalent w hi le statement: fo r wh il e fo r i=0 i<m ax _l en gt h; i++) i f (i np ut _l in e[i == ´?´) q ue st _c ou nt f or (i 0; i ma x_ le ng th i if in pu t_ li ne i] qu es t_ co un t++; Rewrite it to use a pointer as the controlled variable, that is, so that the test is of the form p==´?´. *p 2. (∗1) Fully parenthesize the following expressions: a = b + c * d << 2 & 8 a & 0 77 != 3 07 7 a == b || a == c && c < 5 c = x != 0 0 <= i < 7 f 1,2 3 f(1 2)+3 a = - 1 + + b -- - 5 a = b == c ++ a=b=c=0 a 4][2 *= * b ? c : * d * 2 a[4 2] a b,c d a-b c=d 3. (∗2) Read a sequence of possibly whitespace-separated (name,value) pairs, where the name is a single whitespace-separated word and the value is an integer or a floating-point value. Compute and print the sum and mean for each name and the sum and mean for all names. Hint: §6.1.8. 4. (∗1) Write a table of values for the bitwise logical operations (§6.2.4) for all possible combina- tions of 0 and 1 operands. 5. (∗1.5) Find 5 different C++ constructs for which the meaning is undefined (§C.2). (∗1.5) Find 5 different C++ constructs for which the meaning is implementation-defined (§C.2). 6. (∗1) Find 10 different examples of nonportable C++ code. 7. (∗2) Write 5 expressions for which the order of evaluation is undefined. Execute them to see what one or – preferably – more implementations do with them. 8. (∗1.5) What happens if you divide by zero on your system? What happens in case of overflow and underflow? 9. (∗1) Fully parenthesize the following expressions:
    151. Section 6.6 Exercises 141 p++ *p p *--p a-- ++a (i nt p->m in t*)p m *p m p.m *a i] a[i 10. (*2) Write these functions: s tr le n(), which returns the length of a C-style string; s tr cp y(), st rl en st rc py which copies a string into another; and s tr cm p(), which compares two strings. Consider what st rc mp the argument types and return types ought to be. Then compare your functions with the stan- dard library versions as declared in <c st ri ng (<s tr in g.h and as specified in §20.4.1. cs tr in g> st ri ng h>) 11. (∗1) See how your compiler reacts to these errors: v oi d f in t a i nt b vo id f(i nt a, in t b) { i f (a = 3 // ... if a 3) i f (a 07 7 == 0 // ... if a&0 77 0) a := b 1; b+1 } Devise more simple errors and see how the compiler reacts. 12. (∗2) Modify the program from §6.6[3] to also compute the median. 13. (∗2) Write a function c at ca t() that takes two C-style string arguments and returns a string that is the concatenation of the arguments. Use n ew to find store for the result. ne w 14. (∗2) Write a function r ev re v() that takes a string argument and reverses the characters in it. That is, after r ev p) the last character of p will be the first, etc. re v(p 15. (∗1.5) What does the following example do? v oi d s en d(i nt t o, i nt f ro m, i nt c ou nt vo id se nd in t* to in t* fr om in t co un t) // Duff’s device. Helpful comment deliberately deleted. { i nt n = (c ou nt 7)/8 in t co un t+7 8; s wi tc h (c ou nt 8) { sw it ch co un t%8 c as e 0 ca se 0: d o { *t o++ = *f ro m++; do to fr om c as e 7 ca se 7: *t o++ = *f ro m++; to fr om c as e 6 ca se 6: *t o++ = *f ro m++; to fr om c as e 5 ca se 5: *t o++ = *f ro m++; to fr om c as e 4 ca se 4: *t o++ = *f ro m++; to fr om c as e 3 ca se 3: *t o++ = *f ro m++; to fr om c as e 2 ca se 2: *t o++ = *f ro m++; to fr om c as e 1 ca se 1: *t o++ = *f ro m++; to fr om } w hi le (--n 0); wh il e n>0 } } Why would anyone write something like that? 16. (∗2) Write a function a to i(c on st c ha r*) that takes a string containing digits and returns the at oi co ns t ch ar corresponding i nt For example, a to i(\"1 23 is 1 23 Modify a to i() to handle C++ octal and in t. at oi 12 3\") 12 3. at oi hexadecimal notation in addition to plain decimal numbers. Modify a to i() to handle the C++ at oi
    152. 142 Expressions and Statements Chapter 6 character constant notation. 17. (∗2) Write a function i to a(i nt i c ha r b it oa in t i, ch ar b[]) that creates a string representation of i in b and returns bb. 18. (*2) Type in the calculator example and get it to work. Do not ‘‘save time’’ by using an already entered text. You’ll learn most from finding and correcting ‘‘little silly errors.’’ 19. (∗2) Modify the calculator to report line numbers for errors. 20. (∗3) Allow a user to define functions in the calculator. Hint: Define a function as a sequence of operations just as a user would have typed them. Such a sequence can be stored either as a character string or as a list of tokens. Then read and execute those operations when the function is called. If you want a user-defined function to take arguments, you will have to invent a nota- tion for that. 21. (∗1.5) Convert the desk calculator to use a s ym bo l structure instead of using the static variables sy mb ol nu mb er _v al ue st ri ng _v al ue n um be r_ va lu e and s tr in g_ va lu e. 22. (∗2.5) Write a program that strips comments out of a C++ program. That is, read from c in ci n, remove both // comments and /* */ comments, and write the result to c ou t. Do not worry co ut about making the layout of the output look nice (that would be another, and much harder, exer- cise). Do not worry about incorrect programs. Beware of //, /*, and */ in comments, strings, and character constants. 23. (∗2) Look at some programs to get an idea of the variety of indentation, naming, and comment- ing styles actually used.
    153. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 7 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Functions To iterate is human, to recurse divine. – L. Peter Deutsch Function declarations and definitions — argument passing — return values — function overloading — ambiguity resolution — default arguments — s td ar gs — pointers to st da rg s functions — macros — advice — exercises. 7.1 Function Declarations [fct.dcl] The typical way of getting something done in a C++ program is to call a function to do it. Defining a function is the way you specify how an operation is to be done. A function cannot be called unless it has been previously declared. A function declaration gives the name of the function, the type of the value returned (if any) by the function, and the number and types of the arguments that must be supplied in a call of the func- tion. For example: El em ne xt _e le m() E le m* n ex t_ el em ; c ha r* s tr cp y(c ha r* t o, c on st c ha r* f ro m); ch ar st rc py ch ar to co ns t ch ar fr om v oi d e xi t(i nt ; vo id ex it in t) The semantics of argument passing are identical to the semantics of initialization. Argument types are checked and implicit argument type conversion takes place when necessary. For example: d ou bl e s qr t(d ou bl e); do ub le sq rt do ub le d ou bl e s r2 = s qr t(2 ; do ub le sr 2 sq rt 2) // call sqrt() with the argument double(2) d ou bl e s q3 = s qr t(\"t hr ee ; do ub le sq 3 sq rt th re e\") // error: sqrt() requires an argument of type double The value of such checking and type conversion should not be underestimated.
    154. 144 Functions Chapter 7 A function declaration may contain argument names. This can be a help to the reader of a pro- gram, but the compiler simply ignores such names. As mentioned in §4.7, v oi d as a return type vo id means that the function does not return a value. 7.1.1 Function Definitions [fct.def] Every function that is called in a program must be defined somewhere (once only). A function def- inition is a function declaration in which the body of the function is presented. For example: e xt er n v oi d s wa p(i nt ex te rn vo id sw ap in t*, i nt ; // a declaration in t*) v oi d s wa p(i nt p i nt q vo id sw ap in t* p, in t* q) // a definition { i nt t = *p in t p; p q; *p = *q *q = t q t; } The type of the definition and all declarations for a function must specify the same type. The argu- ment names, however, are not part of the type and need not be identical. It is not uncommon to have function definitions with unused arguments: v oi d s ea rc h(t ab le t c on st c ha r* k ey c on st c ha r*) vo id se ar ch ta bl e* t, co ns t ch ar ke y, co ns t ch ar { // no use of the third argument } As shown, the fact that an argument is unused can be indicated by not naming it. Typically, unnamed arguments arise from the simplification of code or from planning ahead for extensions. In both cases, leaving the argument in place, although unused, ensures that callers are not affected by the change. A function can be defined to be i nl in e. For example: in li ne i nl in e i nt f ac in t n in li ne in t fa c(i nt n) { r et ur n (n 2) ? 1 : n fa c(n 1); re tu rn n<2 n*f ac n-1 } The i nl in e specifier is a hint to the compiler that it should attempt to generate code for a call of in li ne f ac fa c() inline rather than laying down the code for the function once and then calling through the usual function call mechanism. A clever compiler can generate the constant 7 20 for a call f ac 6). 72 0 fa c(6 The possibility of mutually recursive inline functions, inline functions that recurse or not depending on input, etc., makes it impossible to guarantee that every call of an i nl in e function is actually in li ne inlined. The degree of cleverness of a compiler cannot be legislated, so one compiler might gener- ate 7 20 another 6 fa c(5 and yet another an un-inlined call f ac 6). 72 0, 6*f ac 5), fa c(6 To make inlining possible in the absence of unusually clever compilation and linking facilities, the definition – and not just the declaration – of an inline function must be in scope (§9.2). An i nl in e specifier does not affect the semantics of a function. In particular, an inline function still has in li ne a unique address and so has s ta ti c variables (§7.1.2) of an inline function. st at ic
    155. Section 7.1.2 Static Variables 145 7.1.2 Static Variables [fct.static] A local variable is initialized when the thread of execution reaches its definition. By default, this happens in every call of the function and each invocation of the function has its own copy of the variable. If a local variable is declared s ta ti c, a single, statically allocated object will be used to st at ic represent that variable in all calls of the function. It will be initialized only the first time the thread of execution reaches its definition. For example: v oi d f in t a vo id f(i nt a) { w hi le (a wh il e a--) { s ta ti c i nt n = 0 st at ic in t 0; // initialized once i nt x = 0 in t 0; // initialized n times c ou t << \"n == \" << n co ut n n++ << \", x == \" << x x++ << ´\\ n´; \\n } } i nt m ai n() in t ma in { f 3); f(3 } This prints: n == 0 x == 0 0, n == 1 x == 0 1, n == 2 x == 0 2, A static variable provides a function with ‘‘a memory’’ without introducing a global variable that might be accessed and corrupted by other functions (see also §10.2.4). 7.2 Argument Passing [fct.arg] When a function is called, store is set aside for its formal arguments and each formal argument is initialized by its corresponding actual argument. The semantics of argument passing are identical to the semantics of initialization. In particular, the type of an actual argument is checked against the type of the corresponding formal argument, and all standard and user-defined type conversions are performed. There are special rules for passing arrays (§7.2.1), a facility for passing unchecked arguments (§7.6), and a facility for specifying default arguments (§7.5). Consider: v oi d f in t v al i nt r ef vo id f(i nt va l, in t& re f) { v al va l++; r ef re f++; } When ff() is called, v al va l++ increments a local copy of the first actual argument, whereas r ef re f++ increments the second actual argument. For example,
    156. 146 Functions Chapter 7 v oi d g vo id g() { i nt i = 1 in t 1; i nt j = 1 in t 1; f i,j ; f(i j) } will increment j but not i The first argument, i is passed by value, the second argument, j is i. i, j, passed by reference. As mentioned in §5.5, functions that modify call-by-reference arguments can make programs hard to read and should most often be avoided (but see §21.2.1). It can, however, be noticeably more efficient to pass a large object by reference than to pass it by value. In that case, the argument might be declared c on st to indicate that the reference is used for efficiency rea- co ns t sons only and not to enable the called function to change the value of the object: v oi d f co ns t L ar ge a rg vo id f(c on st La rg e& ar g) { // the value of \"arg\" cannot be changed without explicit use of type conversion } The absence of c on st in the declaration of a reference argument is taken as a statement of intent to co ns t modify the variable: v oi d g La rg e& a rg ; // assume that g() modifies arg vo id g(L ar ge ar g) Similarly, declaring a pointer argument c on st tells readers that the value of an object pointed to by co ns t that argument is not changed by the function. For example: i nt s tr le n(c on st c ha r*); in t st rl en co ns t ch ar // number of characters in a C-style string c ha r* s tr cp y(c ha r* t o, c on st c ha r* f ro m); ch ar st rc py ch ar to co ns t ch ar fr om // copy a C-style string i nt s tr cm p(c on st c ha r*, c on st c ha r*); in t st rc mp co ns t ch ar co ns t ch ar // compare C-style strings The importance of using c on st arguments increases with the size of a program. co ns t Note that the semantics of argument passing are different from the semantics of assignment. This is important for c on st arguments, reference arguments, and arguments of some user-defined co ns t types (§10.4.4.1). A literal, a constant, and an argument that requires conversion can be passed as a c on st argu- co ns t& ment, but not as a non-c on st argument. Allowing conversions for a c on st T argument ensures that co ns t co ns t T& such an argument can be given exactly the same set of values as a T argument by passing the value in a temporary, if necessary. For example: f lo at f sq rt co ns t f lo at ; // Fortran-style sqrt taking a reference argument fl oa t fs qr t(c on st fl oa t&) v oi d g do ub le d vo id g(d ou bl e d) { f lo at r = f sq rt 2.0 f); fl oa t fs qr t(2 0f // pass ref to temp holding 2.0f r = f sq rt r); fs qr t(r // pass ref to r r = f sq rt d); fs qr t(d // pass ref to temp holding float(d) } Disallowing conversions for non-c on st reference arguments (§5.5) avoids the possibility of silly co ns t mistakes arising from the introduction of temporaries. For example:
    157. Section 7.2 Argument Passing 147 v oi d u pd at e(f lo at i ; vo id up da te fl oa t& i) v oi d g do ub le d f lo at r vo id g(d ou bl e d, fl oa t r) { u pd at e(2 0f ; // error: const argument up da te 2.0 f) u pd at e(r ; up da te r) // pass ref to r u pd at e(d ; up da te d) // error: type conversion required } Had these calls been allowed, u pd at e() would quietly have updated temporaries that immediately up da te were deleted. Usually, that would come as an unpleasant surprise to the programmer. 7.2.1 Array Arguments [fct.array] If an array is used as a function argument, a pointer to its initial element is passed. For example: i nt s tr le n(c on st c ha r*); in t st rl en co ns t ch ar v oi d f vo id f() { c ha r v ch ar v[] = \"a n a rr ay an ar ra y\"; i nt i = s tr le n(v ; in t st rl en v) i nt j = s tr le n(\"N ic ho la s\"); in t st rl en Ni ch ol as } That is, an argument of type T T[] will be converted to a T when passed as an argument. This T* implies that an assignment to an element of an array argument changes the value of an element of the argument array. In other words, arrays differ from other types in that an array is not (and can- not be) passed by value. The size of an array is not available to the called function. This can be a nuisance, but there are several ways of circumventing this problem. C-style strings are zero-terminated, so their size can be computed easily. For other arrays, a second argument specifying the size can be passed. For example: v oi d c om pu te 1(i nt v ec _p tr i nt v ec _s iz e); vo id co mp ut e1 in t* ve c_ pt r, in t ve c_ si ze // one way s tr uc t V ec { st ru ct Ve c i nt p tr in t* pt r; i nt s iz e; in t si ze }; v oi d c om pu te 2(c on st V ec v ; vo id co mp ut e2 co ns t Ve c& v) // another way Alternatively, a type such as v ec to r (§3.7.1, §16.3) can be used instead of an array. ve ct or Multidimensional arrays are trickier (see §C.7), but often arrays of pointers can be used instead, and they need no special treatment. For example: c ha r* d ay ch ar da y[] = { \"m on mo n\", \"t ue tu e\", \"w ed we d\", \"t hu th u\", \"f ri fr i\", \"s at \"s un sa t\", su n\" }; Again, v ec to r and similar types are alternatives to the built-in, low-level arrays and pointers. ve ct or
    158. 148 Functions Chapter 7 7.3 Value Return [fct.return] A value must be returned from a function that is not declared v oi d (however, m ai n() is special; see vo id ma in §3.2). Conversely, a value cannot be returned from a v oi d function. For example: vo id i nt f 1() { } in t f1 // error: no value returned v oi d f 2() { } vo id f2 // ok i nt f 3() { r et ur n 1 } in t f3 re tu rn 1; // ok v oi d f 4() { r et ur n 1 } vo id f4 re tu rn 1; // error: return value in void function i nt f 5() { r et ur n; } in t f5 re tu rn // error: return value missing v oi d f 6() { r et ur n; } vo id f6 re tu rn // ok A return value is specified by a return statement. For example: i nt f ac in t n { r et ur n (n 1) ? n fa c(n 1) : 1 } in t fa c(i nt n) re tu rn n>1 n*f ac n-1 1; A function that calls itself is said to be recursive. There can be more than one return statement in a function: i nt f ac 2(i nt n in t fa c2 in t n) { i f (n > 1 r et ur n n fa c2 n-1 ; if n 1) re tu rn n*f ac 2(n 1) r et ur n 1 re tu rn 1; } Like the semantics of argument passing, the semantics of function value return are identical to the semantics of initialization. A return statement is considered to initialize an unnamed variable of the returned type. The type of a return expression is checked against the type of the returned type, and all standard and user-defined type conversions are performed. For example: d ou bl e f do ub le f() { r et ur n 1 } // 1 is implicitly converted to double(1) re tu rn 1; Each time a function is called, a new copy of its arguments and local (automatic) variables is cre- ated. The store is reused after the function returns, so a pointer to a local variable should never be returned. The contents of the location pointed to will change unpredictably: i nt f p() { i nt l oc al = 1 /* ... */ r et ur n &l oc al } in t* fp in t lo ca l 1; re tu rn lo ca l; // bad This error is less common than the equivalent error using references: i nt f r() { i nt l oc al = 1 /* ... */ r et ur n l oc al } in t& fr in t lo ca l 1; re tu rn lo ca l; // bad Fortunately, a compiler can easily warn about returning references to local variables. A v oi d function cannot return a value. However, a call of a v oi d function doesn’t yield a value, vo id vo id so a v oi d function can use a call of a v oi d function as the expression in a r et ur n statement. For vo id vo id re tu rn example: v oi d g in t* p ; vo id g(i nt p) v oi d h in t* p { /* ... */ r et ur n g p); } // ok: return of ‘‘no value’’ vo id h(i nt p) re tu rn g(p This form of return is important when writing template functions where the return type is a tem- plate parameter (see §18.4.4.2).
    159. Section 7.4 Overloaded Function Names 149 7.4 Overloaded Function Names [fct.over] Most often, it is a good idea to give different functions different names, but when some functions conceptually perform the same task on objects of different types, it can be more convenient to give them the same name. Using the same name for operations on different types is called overloading. The technique is already used for the basic operations in C++. That is, there is only one name for addition, +, yet it can be used to add values of integer, floating-point, and pointer types. This idea is easily extended to functions defined by the programmer. For example: v oi d p ri nt in t); vo id pr in t(i nt // print an int v oi d p ri nt co ns t c ha r*); // print a C-style character string vo id pr in t(c on st ch ar As far as the compiler is concerned, the only thing functions of the same name have in common is that name. Presumably, the functions are in some sense similar, but the language does not con- strain or aid the programmer. Thus overloaded function names are primarily a notational conve- nience. This convenience is significant for functions with conventional names such as s qr t, p ri nt sq rt pr in t, and o pe n. When a name is semantically significant, this convenience becomes essential. This hap- op en pens, for example, with operators such as +, *, and <<, in the case of constructors (§11.7), and in generic programming (§2.7.2, Chapter 18). When a function f is called, the compiler must figure out which of the functions with the name f is to be invoked. This is done by comparing the types of the actual arguments with the types of the formal arguments of all functions called f The idea is to f. invoke the function that is the best match on the arguments and give a compile-time error if no function is the best match. For example: v oi d p ri nt do ub le ; vo id pr in t(d ou bl e) v oi d p ri nt lo ng ; vo id pr in t(l on g) v oi d f vo id f() { p ri nt 1L ; pr in t(1 L) // print(long) p ri nt 1.0 ; pr in t(1 0) // print(double) p ri nt 1); pr in t(1 // error, ambiguous: print(long(1)) or print(double(1))? } Finding the right version to call from a set of overloaded functions is done by looking for a best match between the type of the argument expression and the parameters (formal arguments) of the functions. To approximate our notions of what is reasonable, a series of criteria are tried in order: [1] Exact match; that is, match using no or only trivial conversions (for example, array name to pointer, function name to pointer to function, and T to c on st T co ns t T) [2] Match using promotions; that is, integral promotions (b oo l to i nt c ha r to i nt s ho rt to i nt bo ol in t, ch ar in t, sh or t in t, and their u ns ig ne d counterparts; §C.6.1), f lo at to d ou bl e, and d ou bl e to l on g d ou bl e un si gn ed fl oa t do ub le do ub le lo ng do ub le [3] Match using standard conversions (for example, i nt to d ou bl e, d ou bl e to i nt D er iv ed to in t do ub le do ub le in t, De ri ve d* B as e* (§12.2), T* to v oi d* (§5.6), i nt to u ns ig ne d i nt §C.6) Ba se vo id in t un si gn ed in t; [4] Match using user-defined conversions (§11.4) [5] Match using the ellipsis ... in a function declaration (§7.6) If two matches are found at the highest level where a match is found, the call is rejected as ambigu- ous. The resolution rules are this elaborate primarily to take into account the elaborate C and C++ rules for built-in numeric types (§C.6). For example:
    160. 150 Functions Chapter 7 v oi d vo id p ri nt in t); pr in t(i nt v oi d vo id p ri nt co ns t c ha r*); pr in t(c on st ch ar v oi d vo id p ri nt do ub le ; pr in t(d ou bl e) v oi d vo id p ri nt lo ng ; pr in t(l on g) v oi d vo id p ri nt ch ar ; pr in t(c ha r) v oi d h ch ar c i nt i s ho rt s f lo at f vo id h(c ha r c, in t i, sh or t s, fl oa t f) { p ri nt c); pr in t(c // exact match: invoke print(char) p ri nt i); pr in t(i // exact match: invoke print(int) p ri nt s); pr in t(s // integral promotion: invoke print(int) p ri nt f); pr in t(f // float to double promotion: print(double) p ri nt a´); pr in t(´a // exact match: invoke print(char) p ri nt 49 ; pr in t(4 9) // exact match: invoke print(int) p ri nt 0); pr in t(0 // exact match: invoke print(int) p ri nt a\"); pr in t(\"a // exact match: invoke print(const char*) } The call p ri nt 0) invokes p ri nt in t) because 0 is an i nt The call p ri nt a´) invokes pr in t(0 pr in t(i nt in t. pr in t(´a p ri nt ch ar because ´a is a c ha r (§4.3.1). The reason to distinguish between conversions and pr in t(c ha r) a´ ch ar promotions is that we want to prefer safe promotions, such as c ha r to i nt over unsafe conversions, ch ar in t, such as i nt to c ha r. in t ch ar The overloading resolution is independent of the order of declaration of the functions consid- ered. Overloading relies on a relatively complicated set of rules, and occasionally a programmer will be surprised which function is called. So, why bother? Consider the alternative to overloading. Often, we need similar operations performed on objects of several types. Without overloading, we must define several functions with different names: v oi d p ri nt _i nt in t); vo id pr in t_ in t(i nt v oi d p ri nt _c ha r(c ha r); vo id pr in t_ ch ar ch ar v oi d p ri nt _s tr in g(c on st c ha r*); // C-style string vo id pr in t_ st ri ng co ns t ch ar v oi d g in t i c ha r c c on st c ha r* p d ou bl e d vo id g(i nt i, ch ar c, co ns t ch ar p, do ub le d) { p ri nt _i nt i); pr in t_ in t(i // ok p ri nt _c ha r(c ; pr in t_ ch ar c) // ok p ri nt _s tr in g(p ; pr in t_ st ri ng p) // ok p ri nt _i nt c); pr in t_ in t(c // ok? calls print_int(int(c)) p ri nt _c ha r(i ; pr in t_ ch ar i) // ok? calls print_char(char(i)) p ri nt _s tr in g(i ; pr in t_ st ri ng i) // error p ri nt _i nt d); pr in t_ in t(d // ok? calls print_int(int(d)) } Compared to the overloaded p ri nt pr in t(), we have to remember several names and remember to use those correctly. This can be tedious, defeats attempts to do generic programming (§2.7.2), and gen- erally encourages the programmer to focus on relatively low-level type issues. Because there is no overloading, all standard conversions apply to arguments to these functions. It can also lead to
    161. Section 7.4 Overloaded Function Names 151 errors. In the previous example, this implies that only one of the four calls with a ‘‘wrong’’ argu- ment is caught by the compiler. Thus, overloading can increase the chances that an unsuitable argument will be rejected by the compiler. 7.4.1 Overloading and Return Type [fct.return] Return types are not considered in overload resolution. The reason is to keep resolution for an indi- vidual operator (§11.2.1, §11.2.4) or function call context-independent. Consider: f lo at s qr t(f lo at ; fl oa t sq rt fl oa t) d ou bl e s qr t(d ou bl e); do ub le sq rt do ub le v oi d f do ub le d a, f lo at f la vo id f(d ou bl e da fl oa t fl a) { f lo at f l = s qr t(d a); // call sqrt(double) fl oa t fl sq rt da d ou bl e d = s qr t(d a); // call sqrt(double) do ub le sq rt da f l = s qr t(f la ; fl sq rt fl a) // call sqrt(float) d = s qr t(f la ; sq rt fl a) // call sqrt(float) } If the return type were taken into account, it would no longer be possible to look at a call of s qr t() sq rt in isolation and determine which function was called. 7.4.2 Overloading and Scopes [fct.scope] Functions declared in different non-namespace scopes do not overload. For example: v oi d f in t); vo id f(i nt v oi d g vo id g() { v oi d f do ub le ; vo id f(d ou bl e) f 1); f(1 // call f(double) } Clearly, f in t) would have been the best match for f 1), but only f do ub le is in scope. In such f(i nt f(1 f(d ou bl e) cases, local declarations can be added or subtracted to get the desired behavior. As always, inten- tional hiding can be a useful technique, but unintentional hiding is a source of surprises. When overloading across class scopes (§15.2.2) or namespace scopes (§8.2.9.2) is wanted, using- declarations or u si ng di re ct iv es can be used (§8.2.2). See also §8.2.6 and §8.2.9.2. us in g-d ir ec ti ve s 7.4.3 Manual Ambiguity Resolution [fct.man.ambig] Declaring too few (or too many) overloaded versions of a function can lead to ambiguities. For example: v oi d f 1(c ha r); vo id f1 ch ar v oi d f 1(l on g); vo id f1 lo ng v oi d f 2(c ha r*); vo id f2 ch ar v oi d f 2(i nt ; vo id f2 in t*)
    162. 152 Functions Chapter 7 v oi d k in t i vo id k(i nt i) { f 1(i ; f1 i) // ambiguous: f1(char) or f1(long) f 2(0 ; f2 0) // ambiguous: f2(char*) or f2(int*) } Where possible, the thing to do in such cases is to consider the set of overloaded versions of a func- tion as a whole and see if it makes sense according to the semantics of the function. Often the problem can be solved by adding a version that resolves ambiguities. For example, adding i nl in e v oi d f 1(i nt n { f 1(l on g(n ; } in li ne vo id f1 in t n) f1 lo ng n)) would resolve all ambiguities similar to f 1(i in favor of the larger type l on g i nt f1 i) lo ng in t. One can also add an explicit type conversion to resolve a specific call. For example: f2 st at ic _c as t<i nt f 2(s ta ti c_ ca st in t*>(0 ; 0)) However, this is most often simply an ugly stopgap. Soon another similar call will be made and have to be dealt with. Some C++ novices get irritated by the ambiguity errors reported by the compiler. More experi- enced programmers appreciate these error messages as useful indicators of design errors. 7.4.4 Resolution for Multiple Arguments [fct.fct.res] Given the overload resolution rules, one can ensure that the simplest algorithm (function) will be used when the efficiency or precision of computations differs significantly for the types involved. For example: i nt p ow in t, i nt ; in t po w(i nt in t) d ou bl e p ow do ub le d ou bl e); do ub le po w(d ou bl e, do ub le c om pl ex co mp le x p ow do ub le c om pl ex ; po w(d ou bl e, co mp le x) c om pl ex co mp le x p ow co mp le x, i nt ; po w(c om pl ex in t) c om pl ex co mp le x p ow co mp le x, d ou bl e); po w(c om pl ex do ub le c om pl ex co mp le x p ow co mp le x, c om pl ex ; po w(c om pl ex co mp le x) v oi d k co mp le x z vo id k(c om pl ex z) { i nt i = p ow 2,2 ; in t po w(2 2) // invoke pow(int,int) d ou bl e d = p ow 2.0 2.0 ; do ub le po w(2 0,2 0) // invoke pow(double,double) c om pl ex z 2 = p ow 2,z ; co mp le x z2 po w(2 z) // invoke pow(double,complex) c om pl ex z 3 = p ow z,2 ; co mp le x z3 po w(z 2) // invoke pow(complex,int) c om pl ex z 4 = p ow z,z ; co mp le x z4 po w(z z) // invoke pow(complex,complex) } In the process of choosing among overloaded functions with two or more arguments, a best match is found for each argument using the rules from §7.4. A function that is the best match for one argument and a better than or equal match for all other arguments is called. If no such function exists, the call is rejected as ambiguous. For example:
    163. Section 7.4.4 Resolution for Multiple Arguments 153 v oi d g vo id g() { d ou bl e d = p ow 2.0 2); // error: pow(int(2.0),2) or pow(2.0,double(2))? do ub le po w(2 0,2 } The call is ambiguous because 2 0 is the best match for the first argument of 2.0 p ow do ub le do ub le and 2 is the best match for the second argument of p ow in t,i nt po w(d ou bl e,d ou bl e) po w(i nt in t). 7.5 Default Arguments [fct.defarg] A general function often needs more arguments than are necessary to handle simple cases. In par- ticular, functions that construct objects (§10.2.3) often provide several options for flexibility. Con- sider a function for printing an integer. Giving the user an option of what base to print it in seems reasonable, but in most programs integers will be printed as decimal integer values. For example: v oi d p ri nt in t v al ue i nt b as e =1 0); // default base is 10 vo id pr in t(i nt va lu e, in t ba se 10 v oi d f vo id f() { p ri nt 31 ; pr in t(3 1) p ri nt 31 10 ; pr in t(3 1,1 0) p ri nt 31 16 ; pr in t(3 1,1 6) p ri nt 31 2); pr in t(3 1,2 } might produce this output: 3 1 3 1 1 f 1 11 11 31 31 1f 11 11 1 The effect of a default argument can alternatively be achieved by overloading: v oi d p ri nt in t v al ue i nt b as e); vo id pr in t(i nt va lu e, in t ba se i nl in e v oi d p ri nt in t v al ue { p ri nt va lu e,1 0); } in li ne vo id pr in t(i nt va lu e) pr in t(v al ue 10 However, overloading makes it less obvious to the reader that the intent is to have a single print function plus a shorthand. A default argument is type checked at the time of the function declaration and evaluated at the time of the call. Default arguments may be provided for trailing arguments only. For example: i nt f in t, i nt =0 c ha r* =0 ; in t f(i nt in t 0, ch ar 0) // ok i nt g in t =0 i nt =0 c ha r*); in t g(i nt 0, in t 0, ch ar // error i nt h in t =0 i nt c ha r* =0 ; in t h(i nt 0, in t, ch ar 0) // error Note that the space between the * and the = is significant (*= is an assignment operator; §6.2): i nt n as ty ch ar 0); in t na st y(c ha r*=0 // syntax error A default argument can be repeated in a subsequent declaration in the same scope but not changed. For example:
    164. 154 Functions Chapter 7 v oi d f in t x = 7 ; vo id f(i nt 7) v oi d f in t = 7 ; vo id f(i nt 7) // ok v oi d f in t = 8 ; vo id f(i nt 8) // error: different default arguments v oi d g vo id g() { v oi d f in t x = 9 ; vo id f(i nt 9) // ok: this declaration hides the outer one // ... } Declaring a name in a nested scope so that the name hides a declaration of the same name in an outer scope is error prone. 7.6 Unspecified Number of Arguments [fct.stdarg] For some functions, it is not possible to specify the number and type of all arguments expected in a call. Such a function is declared by terminating the list of argument declarations with the ellipsis (...), which means ‘‘and maybe some more arguments.’’ For example: i nt p ri nt f(c on st c ha r* ...); in t pr in tf co ns t ch ar This specifies that a call of the C standard library function p ri nt f() (§21.8) must have at least one pr in tf argument, a c ha r*, but may or may not have others. For example: ch ar p ri nt f(\"H el lo w or ld \\n ; pr in tf He ll o, wo rl d!\\ n\") p ri nt f(\"M y n am e i s %s %s \\n f ir st _n am e, s ec on d_ na me ; pr in tf My na me is s s\\ n\", fi rs t_ na me se co nd _n am e) p ri nt f(\"%d + %d = %d \\n 2,3 5); pr in tf d d d\\ n\",2 3,5 Such a function must rely on information not available to the compiler when interpreting its argu- ment list. In the case of p ri nt f(), the first argument is a format string containing special character pr in tf sequences that allow p ri nt f() to handle other arguments correctly; %s means ‘‘expect a c ha r* pr in tf s ch ar argument’’ and %d means ‘‘expect an i nt argument.’’ However, the compiler cannot in general d in t know that, so it cannot ensure that the expected arguments are really there or that an argument is of the proper type. For example, #i nc lu de <s td io h> in cl ud e st di o.h i nt m ai n() in t ma in { p ri nt f(\"M y n am e i s %s %s \\n 2); pr in tf My na me is s s\\ n\",2 } will compile and (at best) cause some strange-looking output (try it!). Clearly, if an argument has not been declared, the compiler does not have the information needed to perform the standard type checking and type conversion for it. In that case, a c ha r or a ch ar s ho rt is passed as an i nt and a f lo at is passed as a d ou bl e. This is not necessarily what the pro- sh or t in t fl oa t do ub le grammer expects. A well-designed program needs at most a few functions for which the argument types are not completely specified. Overloaded functions and functions using default arguments can be used to
    165. Section 7.6 Unspecified Number of Arguments 155 take care of type checking in most cases when one would otherwise consider leaving argument types unspecified. Only when both the number of arguments and the type of arguments vary is the ellipsis necessary. The most common use of the ellipsis is to specify an interface to C library func- tions that were defined before C++ provided alternatives: // from <cstdio> i nt f pr in tf FI LE in t fp ri nt f(F IL E*, c on st c ha r* ...); co ns t ch ar i nt e xe cl co ns t c ha r* ...); in t ex ec l(c on st ch ar // from UNIX header A standard set of macros for accessing the unspecified arguments in such functions can be found in <c st da rg cs td ar g>. Consider writing an error function that takes one integer argument indicating the severity of the error followed by an arbitrary number of strings. The idea is to compose the error message by passing each word as a separate string argument. The list of string arguments should be terminated by a null pointer to c ha r: ch ar e xt er n v oi d e rr or in t ...); ex te rn vo id er ro r(i nt e xt er n c ha r* i to a(i nt c ha r[]); ex te rn ch ar it oa in t, ch ar // see §6.6[17] co ns t ch ar Nu ll _c p 0; c on st c ha r* N ul l_ cp = 0 i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv { s wi tc h (a rg c) { sw it ch ar gc c as e 1 ca se 1: er ro r(0 ar gv 0] Nu ll _c p) e rr or 0,a rg v[0 ,N ul l_ cp ; b re ak br ea k; c as e 2 ca se 2: er ro r(0 ar gv 0] ar gv 1] Nu ll _c p) e rr or 0,a rg v[0 ,a rg v[1 ,N ul l_ cp ; b re ak br ea k; d ef au lt de fa ul t: c ha r b uf fe r[8 ; ch ar bu ff er 8] Nu ll _c p) e rr or 1,a rg v[0 , \"w it h\",i to a(a rg c-1 bu ff er ,\"a rg um en ts er ro r(1 ar gv 0] wi th it oa ar gc 1,b uf fe r) ar gu me nt s\", N ul l_ cp ; } // ... } The function i to a() returns the character string representing its integer argument. it oa Note that using the integer 0 as the terminator would not have been portable: on some imple- mentations, the integer zero and the null pointer do not have the same representation. This illus- trates the subtleties and extra work that face the programmer once type checking has been sup- pressed using the ellipsis. The error function could be defined like this: v oi d e rr or in t s ev er it y ...) // \"severity\" followed by a zero-terminated list of char*s vo id er ro r(i nt se ve ri ty { va _l is t ap v a_ li st a p; va _s ta rt ap se ve ri ty v a_ st ar t(a p,s ev er it y); // arg startup
    166. 156 Functions Chapter 7 f or (;;) { fo r va _a rg ap ch ar c ha r* p = v a_ ar g(a p,c ha r*); ch ar i f (p == 0 b re ak if p 0) br ea k; c er r << p << ´ ´; ce rr } va _e nd ap v a_ en d(a p); // arg cleanup c er r << ´\\ n´; ce rr \\n i f (s ev er it y) e xi t(s ev er it y); if se ve ri ty ex it se ve ri ty } va _l is t va _s ta rt va _s ta rt First, a v a_ li st is defined and initialized by a call of v a_ st ar t(). The macro v a_ st ar t takes the va _l is t va _a rg name of the v a_ li st and the name of the last formal argument as arguments. The macro v a_ ar g() is used to pick the unnamed arguments in order. In each call, the programmer must supply a type; va _a rg v a_ ar g() assumes that an actual argument of that type has been passed, but it typically has no way va _s ta rt va _e nd of ensuring that. Before returning from a function in which v a_ st ar t() has been used, v a_ en d() va _s ta rt must be called. The reason is that v a_ st ar t() may modify the stack in such a way that a return va _e nd cannot successfully be done; v a_ en d() undoes any such modifications. 7.7 Pointer to Function [fct.pf] There are only two things one can do to a function: call it and take its address. The pointer obtained by taking the address of a function can then be used to call the function. For example: v oi d e rr or st ri ng s { /* ... */ } vo id er ro r(s tr in g s) v oi d (*e fc t)(s tr in g); vo id ef ct st ri ng // pointer to function v oi d f vo id f() { e fc t = &e rr or ef ct er ro r; // efct points to error e fc t(\"e rr or ; ef ct er ro r\") // call error through efct } The compiler will discover that e fc t is a pointer and call the function pointed to. That is, derefer- ef ct encing of a pointer to function using * is optional. Similarly, using & to get the address of a func- tion is optional: v oi d (*f 1)(s tr in g) = &e rr or vo id f1 st ri ng er ro r; // ok v oi d (*f 2)(s tr in g) = e rr or vo id f2 st ri ng er ro r; // also ok; same meaning as &error v oi d g vo id g() { f 1(\"V as a\"); f1 Va sa // ok (*f 1)(\"M ar y R os e\"); f1 Ma ry Ro se // also ok } Pointers to functions have argument types declared just like the functions themselves. In pointer assignments, the complete function type must match exactly. For example:
    167. Section 7.7 Pointer to Function 157 v oi d (*p f)(s tr in g); vo id pf st ri ng // pointer to void(string) v oi d f 1(s tr in g); vo id f1 st ri ng // void(string) i nt f 2(s tr in g); in t f2 st ri ng // int(string) v oi d f 3(i nt ; vo id f3 in t*) // void(int*) v oi d f vo id f() { p f = &f 1; pf f1 // ok p f = &f 2; pf f2 // error: bad return type p f = &f 3; pf f3 // error: bad argument type p f(\"H er a\"); pf He ra // ok p f(1 ; pf 1) // error: bad argument type i nt i = p f(\"Z eu s\"); // error: void assigned to int in t pf Ze us } The rules for argument passing are the same for calls directly to a function and for calls to a func- tion through a pointer. It is often convenient to define a name for a pointer-to-function type to avoid using the some- what nonobvious declaration syntax all the time. Here is an example from a UNIX system header: t yp ed ef v oi d (*S IG _T YP in t); // from <signal.h> ty pe de f vo id SI G_ TY P)(i nt t yp ed ef v oi d (*S IG _A RG _T YP in t); ty pe de f vo id SI G_ AR G_ TY P)(i nt S IG _T YP s ig na l(i nt S IG _A RG _T YP ; SI G_ TY P si gn al in t, SI G_ AR G_ TY P) An array of pointers to functions is often useful. For example, the menu system for my mouse- based editor is implemented using arrays of pointers to functions to represent operations. The sys- tem cannot be described in detail here, but this is the general idea: t yp ed ef v oi d (*P F)(); ty pe de f vo id PF PF ed it _o ps P F e di t_ op s[] = { // edit operations &c ut &p as te &c op y, &s ea rc h cu t, pa st e, co py se ar ch }; PF fi le _o ps P F f il e_ op s[] = { // file management &o pe n, &a pp en d, &c lo se &w ri te op en ap pe nd cl os e, wr it e }; We can then define and initialize the pointers that control actions selected from a menu associated with the mouse buttons: PF bu tt on 2 ed it _o ps P F* b ut to n2 = e di t_ op s; PF bu tt on 3 fi le _o ps P F* b ut to n3 = f il e_ op s; In a complete implementation, more information is needed to define each menu item. For example, a string specifying the text to be displayed must be stored somewhere. As the system is used, the meaning of mouse buttons changes frequently with the context. Such changes are performed (partly) by changing the value of the button pointers. When a user selects a menu item, such as item 3 for button 2, the associated operation is executed: b ut to n2 2](); // call button2’s 3rd function bu tt on 2[2
    168. 158 Functions Chapter 7 One way to gain appreciation of the expressive power of pointers to functions is to try to write such code without them – and without using their better-behaved cousins, the virtual functions (§12.2.6). A menu can be modified at run-time by inserting new functions into the operator table. It is also easy to construct new menus at run-time. Pointers to functions can be used to provide a simple form of polymorphic routines, that is, rou- tines that can be applied to objects of many different types: t yp ed ef i nt (*C FT co ns t v oi d*, c on st v oi d*); ty pe de f in t CF T)(c on st vo id co ns t vo id vo id ss or t(v oi d* ba se si ze _t n, si ze _t sz CF T cm p) v oi d s so rt vo id b as e, s iz e_ t n s iz e_ t s z, C FT c mp /* Sort the \"n\" elements of vector \"base\" into increasing order using the comparison function pointed to by \"cmp\". The elements are of size \"sz\". Shell sort (Knuth, Vol3, pg84) */ { f or (i nt g ap n/2 0 ga p; g ap 2) fo r in t ga p=n 2; 0<g ap ga p/=2 f or (i nt i ga p; i n; i fo r in t i=g ap i<n i++) f or (i nt j i-g ap 0 j; j ga p) { fo r in t j=i ga p; 0<=j j-=g ap st at ic _c as t<c ha r*>(b as e); // necessary cast c ha r* b = s ta ti c_ ca st ch ar ch ar ba se c ha r* p j = b j*s z; ch ar pj b+j sz // &base[j] c ha r* p jg = b j+g ap sz ch ar pj g b+(j ga p)*s z; // &base[j+gap] i f (c mp pj pj g)<0 { if cm p(p j,p jg 0) // swap base[j] and base[j+gap]: f or (i nt k 0; k sz k fo r in t k=0 k<s z; k++) { c ha r t em p = p j[k ; ch ar te mp pj k] p j[k = p jg k]; pj k] pj g[k p jg k] = t em p; pj g[k te mp } } } } The s so rt routine does not know the type of the objects it sorts, only the number of elements (the ss or t() array size), the size of each element, and the function to call to perform a comparison. The type of s so rt was chosen to be the same as the type of the standard C library sort routine, q so rt ss or t() qs or t(). Real qs or t(), the C++ standard library algorithm s or t (§18.7.1), or a specialized sort rou- programs use q so rt so rt tine. This style of code is common in C, but it is not the most elegant way of expressing this algo- rithm in C++ (see §13.3, §13.5.2). Such a sort function could be used to sort a table such as this: s tr uc t U se r { st ru ct Us er c ha r* n am e; ch ar na me c ha r* i d; ch ar id i nt d ep t; in t de pt };
    169. Section 7.7 Pointer to Function 159 U se r h ea ds Us er he ad s[] = { \"R it ch ie D M\", Ri tc hi e D.M \"d mr dm r\", 1 12 71 11 27 1, \"S et hi R Se th i R.\", \"r av i\", ra vi 1 12 72 11 27 2, \"S zy ma ns ki T G.\", \"t gs Sz ym an sk i T.G tg s\", 1 12 73 11 27 3, \"S ch ry er N L.\", Sc hr ye r N.L \"n ls nl s\", 1 12 74 11 27 4, \"S ch ry er N L.\", Sc hr ye r N.L \"n ls nl s\", 1 12 75 11 27 5, \"K er ni gh an B W.\", \"b wk Ke rn ig ha n B.W bw k\", 1 12 76 11 27 6 }; v oi d p ri nt _i d(U se r* v i nt n vo id pr in t_ id Us er v, in t n) { f or (i nt i 0; i n; i fo r in t i=0 i<n i++) c ou t << v i].n am e << ´\\ t´ << v i].i d << ´\\ t´ << v i].d ep t << ´\\ n´; co ut v[i na me \\t v[i id \\t v[i de pt \\n } To be able to sort, we must first define appropriate comparison functions. A comparison function must return a negative value if its first argument is less than the second, zero if the arguments are equal, and a positive number otherwise: i nt c mp 1(c on st v oi d* p c on st v oi d* q in t cm p1 co ns t vo id p, co ns t vo id q) // Compare name strings { re tu rn st rc mp st at ic _c as t<c on st Us er na me st at ic _c as t<c on st Us er r et ur n s tr cm p(s ta ti c_ ca st co ns t U se r*>(p p)->n am e,s ta ti c_ ca st co ns t U se r*>(q q)->n am e); na me } i nt c mp 2(c on st v oi d* p c on st v oi d* q in t cm p2 co ns t vo id p, co ns t vo id q) // Compare dept numbers { re tu rn st at ic _c as t<c on st Us er de pt st at ic _c as t<c on st Us er r et ur n s ta ti c_ ca st co ns t U se r*>(p p)->d ep t - s ta ti c_ ca st co ns t U se r*>(q q)->d ep t; de pt } This program sorts and prints: i nt m ai n() in t ma in { c ou t << \"H ea ds i n a lp ha be ti ca l o rd er \\n co ut He ad s in al ph ab et ic al or de r:\\ n\"; s so rt he ad s,6 si ze of Us er ,c mp 1); ss or t(h ea ds 6,s iz eo f(U se r) cm p1 p ri nt _i d(h ea ds 6); pr in t_ id he ad s,6 c ou t << \"\\ n\"; co ut \\n c ou t << \"H ea ds i n o rd er o f d ep ar tm en t n um be r:\\ n\"; co ut He ad s in or de r of de pa rt me nt nu mb er \\n s so rt he ad s,6 si ze of Us er ,c mp 2); ss or t(h ea ds 6,s iz eo f(U se r) cm p2 p ri nt _i d(h ea ds 6); pr in t_ id he ad s,6 } You can take the address of an overloaded function by assigning to or initializing a pointer to func- tion. In that case, the type of the target is used to select from the set of overloaded functions. For example: v oi d f in t); vo id f(i nt i nt f ch ar ; in t f(c ha r) v oi d (*p f1 in t) = &f vo id pf 1)(i nt f; // void f(int) i nt (*p f2 ch ar = &f in t pf 2)(c ha r) f; // int f(char) v oi d (*p f3 ch ar = &f vo id pf 3)(c ha r) f; // error: no void f(char)
    170. 160 Functions Chapter 7 A function must be called through a pointer to function with exactly the right argument and return types. There is no implicit conversion of argument or return types when pointers to functions are assigned or initialized. This means that i nt c mp 3(c on st m yt yp e*,c on st m yt yp e*); in t cm p3 co ns t my ty pe co ns t my ty pe is not a suitable argument for s so rt ss or t(). The reason is that accepting c mp 3 as an argument to cm p3 s so rt ss or t() would violate the guarantee that c mp 3 will be called with arguments of type m yt yp e* (see cm p3 my ty pe also §9.2.5). 7.8 Macros [fct.macro] Macros are very important in C but have far fewer uses in C++. The first rule about macros is: Don’t use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer. Because they rearrange the program text before the compiler proper sees it, macros are also a major problem for many programming tools. So when you use macros, you should expect inferior service from tools such as debuggers, cross- reference tools, and profilers. If you must use macros, please read the reference manual for your own implementation of the C++ preprocessor carefully and try not to be too clever. Also to warn readers, follow the convention to name macros using lots of capital letters. The syntax of macros is presented in §A.11. A simple macro is defined like this: #d ef in e N AM E r es t o f l in e de fi ne NA ME re st of li ne Where N AM E is encountered as a token, it is replaced by r es t o f l in e. For example, NA ME re st of li ne n am ed = N AM E na me d NA ME will expand into n am ed = r es t o f l in e na me d re st of li ne A macro can also be defined to take arguments. For example: #d ef in e M AC x,y a rg um en t1 x a rg um en t2 y de fi ne MA C(x y) ar gu me nt 1: ar gu me nt 2: When M AC is used, two argument strings must be presented. They will replace x and y when MA C M AC is expanded. For example, MA C() e xp an de d = M AC fo o b ar y uk y uk ex pa nd ed MA C(f oo ba r, yu k yu k) will be expanded into e xp an de d = a rg um en t1 f oo b ar a rg um en t2 y uk y uk ex pa nd ed ar gu me nt 1: fo o ba r ar gu me nt 2: yu k yu k Macro names cannot be overloaded, and the macro preprocessor cannot handle recursive calls: #d ef in e P RI NT a,b c ou t<<(a de fi ne PR IN T(a b) co ut a)<<(b b) #d ef in e P RI NT a,b c) c ou t<<(a de fi ne PR IN T(a b,c co ut a)<<(b b)<<(c /* trouble?: redefines, does not overload */ c) #d ef in e F AC n) (n 1)?n FA C(n 1):1 de fi ne FA C(n n>1 n*F AC n-1 1 /* trouble: recursive macro */
    171. Section 7.8 Macros 161 Macros manipulate character strings and know little about C++ syntax and nothing about C++ types or scope rules. Only the expanded form of a macro is seen by the compiler, so an error in a macro will be reported when the macro is expanded, not when it is defined. This leads to very obscure error messages. Here are some plausible macros: #d ef in e C AS E b re ak ca se de fi ne CA SE br ea k;c as e #d ef in e F OR EV ER f or de fi ne FO RE VE R fo r(;;) Here are some completely unnecessary macros: #d ef in e P I 3 14 15 93 de fi ne PI 3.1 41 59 3 #d ef in e B EG IN { de fi ne BE GI N #d ef in e E ND } de fi ne EN D Here are some dangerous macros: #d ef in e S QU AR E(a a a de fi ne SQ UA RE a) a*a de fi ne IN CR _x x xx #d ef in e I NC R_ xx (x x)++ To see why they are dangerous, try expanding this: i nt x x = 0 in t xx 0; // global counter v oi d f vo id f() { i nt x x = 0 in t xx 0; // local variable i nt y = S QU AR E(x x+2 ; in t SQ UA RE xx 2) // y=xx+2*xx+2; that is y=xx+(2*xx)+2 IN CR _x x; I NC R_ xx // increments local xx } If you must use a macro, use the scope resolution operator :: when referring to global names (§4.9.4) and enclose occurrences of a macro argument name in parentheses whenever possible. For example: #d ef in e M IN a,b (((a de fi ne MI N(a b) a)<(b b))?(a a):(b b)) If you must write macros complicated enough to require comments, it is wise to use /* */ com- ments because C preprocessors that do not know about // comments are sometimes used as part of C++ tools. For example: #d ef in e M 2(a s om et hi ng a) de fi ne M2 a) so me th in g(a /* thoughtful comment */ Using macros, you can design your own private language. Even if you prefer this ‘‘enhanced lan- guage’’ to plain C++, it will be incomprehensible to most C++ programmers. Furthermore, the C preprocessor is a very simple macro processor. When you try to do something nontrivial, you are likely to find it either impossible or unnecessarily hard to do. The c on st i nl in e, t em pl at e, and co ns t, in li ne te mp la te n am es pa ce mechanisms are intended as alternatives to many traditional uses of preprocessor con- na me sp ac e structs. For example: c on st i nt a ns we r = 4 2; co ns t in t an sw er 42 t em pl at e<c la ss T i nl in e T m in T a T b { r et ur n (a b)?a b; } te mp la te cl as s T> in li ne mi n(T a, b) re tu rn a<b a:b
    172. 162 Functions Chapter 7 When writing a macro, it is not unusual to need a new name for something. A string can be created by concatenating two strings using the ## macro operator. For example, #d ef in e N AM E2 a,b a b de fi ne NA ME 2(a b) a##b i nt N AM E2 ha ck ca h)(); in t NA ME 2(h ac k,c ah will produce i nt h ac kc ah ; in t ha ck ca h() for the compiler to read. The directive #u nd ef X un de f ensures that no macro called X is defined – whether or not one was before the directive. This affords some protection against undesired macros. However, it is not always easy to know what the effects of X on a piece of code were supposed to be. 7.8.1 Conditional Compilation [fct.cond] One use of macros is almost impossible to avoid. The directive #i fd ef i de nt if ie r conditionally if de f id en ti fi er causes all input to be ignored until a #e nd if directive is seen. For example, en di f i nt f in t a in t f(i nt #i fd ef a rg _t wo if de f ar g_ tw o ,i nt b in t #e nd if en di f ); produces i nt f in t a in t f(i nt ); for the compiler to see unless a macro called a rg _t wo has been #d ef in ed. This example confuses ar g_ tw o de fi ne tools that assume sane behavior from the programmer. Most uses of #i fd ef are less bizarre, and when used with restraint, #i fd ef does little harm. See if de f if de f also §9.3.3. Names of the macros used to control #i fd ef should be chosen carefully so that they don’t clash if de f with ordinary identifiers. For example: st ru ct Ca ll _i nf o s tr uc t C al l_ in fo { N od e* a rg _o ne No de ar g_ on e; N od e* a rg _t wo No de ar g_ tw o; // ... }; This innocent-looking source text will cause some confusion should someone write: #d ef in e a rg _t wo x de fi ne ar g_ tw o Unfortunately, common and unavoidable headers contain many dangerous and unnecessary macros.
    173. Section 7.9 Advice 163 7.9 Advice [dcl.advice] [1] Be suspicious of non-c on st reference arguments; if you want the function to modify its argu- co ns t ments, use pointers and value return instead; §5.5. [2] Use c on st reference arguments when you need to minimize copying of arguments; §5.5. co ns t [3] Use c on st extensively and consistently; §7.2. co ns t [4] Avoid macros; §7.8. [5] Avoid unspecified numbers of arguments; §7.6. [6] Don’t return pointers or references to local variables; §7.3. [7] Use overloading when functions perform conceptually the same task on different types; §7.4. [8] When overloading on integers, provide functions to eliminate common ambiguities; §7.4.3. [9] When considering the use of a pointer to function, consider whether a virtual function (§2.5.5) or a template (§2.7.2) would be a better alternative; §7.7. [10] If you must use macros, use ugly names with lots of capital letters; §7.8. 7.10 Exercises [fct.exercises] 1. (∗1) Write declarations for the following: a function taking arguments of type pointer to charac- ter and reference to integer and returning no value; a pointer to such a function; a function tak- ing such a pointer as an argument; and a function returning such a pointer. Write the definition of a function that takes such a pointer as an argument and returns its argument as the return value. Hint: Use t yp ed ef ty pe de f. 2. (∗1) What does the following mean? What would it be good for? t yp ed ef i nt (&r if ii (i nt i nt ; ty pe de f in t ri fi i) in t, in t) 3. (∗1.5) Write a program like ‘‘Hello, world!’’ that takes a name as a command-line argument and writes ‘‘Hello, name !’’. Modify this program to take any number of names as arguments and to say hello to each. 4. (∗1.5) Write a program that reads an arbitrary number of files whose names are given as command-line arguments and writes them one after another on c ou t. Because this program co ut concatenates its arguments to produce its output, you might call it c atca t. 5. (∗2) Convert a small C program to C++. Modify the header files to declare all functions called and to declare the type of every argument. Where possible, replace #d ef in es with e nu m, c on st de fi ne en um co ns t, or i nl in e. Remove e xt er n declarations from .c files and if necessary convert all function defi- in li ne ex te rn c nitions to C++ function definition syntax. Replace calls of m al lo c() and f re e() with n ew and ma ll oc fr ee ne w d el et e. Remove unnecessary casts. de le te 6. (∗2) Implement s so rt (§7.7) using a more efficient sorting algorithm. Hint: q so rt ss or t() qs or t(). 7. (∗2.5) Consider: s tr uc t T no de { st ru ct Tn od e s tr in g w or d; st ri ng wo rd i nt c ou nt in t co un t; T no de l ef t; Tn od e* le ft T no de r ig ht Tn od e* ri gh t; };
    174. 164 Functions Chapter 7 Write a function for entering new words into a tree of T no de Write a function to write out a Tn od es. tree of T no de Write a function to write out a tree of T no de with the words in alphabetical Tn od es. Tn od es order. Modify T no de so that it stores (only) a pointer to an arbitrarily long word stored as an Tn od e array of characters on free store using n ew Modify the functions to use the new definition of ne w. T no de Tn od e. 8. (∗2.5) Write a function to invert a two-dimensional array. Hint: §C.7. 9. (∗2) Write an encryption program that reads from c in and writes the encoded characters to c ou t. ci n co ut You might use this simple encryption scheme: the encrypted form of a character c is c ke y[ic^k ey i], where k ey is a string passed as a command-line argument. The program uses the characters in ke y k ey in a cyclic manner until all the input has been read. Re-encrypting encoded text with the ke y same key produces the original text. If no key (or a null string) is passed, then no encryption is done. 10. (∗3.5) Write a program to help decipher messages encrypted with the method described in §7.10[9] without knowing the key. Hint: See David Kahn: The Codebreakers, Macmillan, 1967, New York, pp. 207-213. 11. (∗3) Write an e rr or function that takes a p ri nt f-style format string containing %s %c and %d er ro r pr in tf s, c, d directives and an arbitrary number of arguments. Don’t use p ri nt f(). Look at §21.8 if you pr in tf don’t know the meaning of %s %c and %d Use <c st da rg s, c, d. cs td ar g>. 12. (∗1) How would you choose names for pointer to function types defined using t yp ed ef ty pe de f? 13. (∗2) Look at some programs to get an idea of the diversity of styles of names actually used. How are uppercase letters used? How is the underscore used? When are short names such as i and x used? 14. (∗1) What is wrong with these macro definitions? #d ef in e P I = 3 14 15 93 de fi ne PI 3.1 41 59 3; #d ef in e M AX a,b a b?a b de fi ne MA X(a b) a>b a:b #d ef in e f ac a) (a fa c((a 1) de fi ne fa c(a a)*f ac a)-1 15. (∗3) Write a macro processor that defines and expands simple macros (like the C preprocessor does). Read from c in and write to c ou t. At first, don’t try to handle macros with arguments. ci n co ut Hint: The desk calculator (§6.1) contains a symbol table and a lexical analyzer that you could modify. 16. (∗2) Implement p ri nt from §7.5. pr in t() 17. (∗2) Add functions such as s qr t(), l og sq rt lo g(), and s in si n() to the desk calculator from §6.1. Hint: Predefine the names and call the functions through an array of pointers to functions. Don’t for- get to check the arguments in a function call. 18. (∗1) Write a factorial function that does not use recursion. See also §11.14[6]. 19. (∗2) Write functions to add one day, one month, and one year to a D at e as defined in §5.9[13]. Da te Write a function that gives the day of the week for a given D at e. Write a function that gives the Da te D at e of the first Monday following a given D at e. Da te Da te
    175. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 8 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Namespaces and Exceptions The year is 787! A.D.? – Monty Python No rule is so general, which admits not some exception. – Robert Burton Modularity, interfaces, and exceptions — namespaces — u si ng — u si ng n am es pa ce — us in g us in g na me sp ac e avoiding name clashes — name lookup — namespace composition — namespace aliases — namespaces and C code — exceptions — t hr ow and c at ch — exceptions and pro- th ro w ca tc h gram structure — advice — exercises. 8.1 Modularization and Interfaces [name.module] Any realistic program consists of a number of separate parts. For example, even the simple ‘‘Hello, world!’’ program involves at least two parts: the user code requests H el lo w or ld to be printed, He ll o, wo rl d! and the I/O system does the printing. Consider the desk calculator example from §6.1. It can be viewed as being composed of five parts: [1] The parser, doing syntax analysis [2] The lexer, composing tokens out of characters [3] The symbol table, holding (string,value) pairs [4] The driver, m ai n() ma in [5] The error handler This can be represented graphically:
    176. 166 Namespaces and Exceptions Chapter 8 driver parser lexer symbol table error handler where an arrow means ‘‘using.’’ To simplify the picture, I have not represented the fact that every part relies on error handling. In fact, the calculator was conceived as three parts, with the driver and error handler added for completeness. When one module uses another, it doesn’t need to know everything about the module used. Ideally, most of the details of a module are unknown to its users. Consequently, we make a distinc- tion between a module and its interface. For example, the parser directly relies on the lexer’s inter- face (only), rather than on the complete lexer. The lexer simply implements the services advertised in its interface. This can be presented graphically like this: driver parser interface parser implementation lexer interface lexer implementation symbol table interface symbol table implementation error handler Dashed lines means ‘‘implements.’’ I consider this to be the real structure of the program, and our job as programmers is to represent this faithfully in code. That done, the code will be simple, effi- cient, comprehensible, maintainable, etc., because it will directly reflect our fundamental design. The following sections show how the logical structure of the desk calculator program can be made clear, and §9.3 shows how the program source text can be physically organized to take advan- tage of it. The calculator is a tiny program, so in ‘‘real life’’ I wouldn’t bother using namespaces and separate compilation (§2.4.1, §9.1) to the extent I do here. It is simply used to present tech- niques useful for larger programs without our drowning in code. In real programs, each ‘‘module’’ represented by a separate namespace will often have hundreds of functions, classes, templates, etc. To demonstrate a variety of techniques and language features, I develop the modularization of
    177. Section 8.1 Modularization and Interfaces 167 the calculator in stages. In ‘‘real life,’’ a program is unlikely to grow through all of these stages. An experienced programmer might pick a design that is ‘‘about right’’ from the start. However, as a program evolves over the years, dramatic structural changes are not uncommon. Error handling permeates the structure of a program. When breaking up a program into mod- ules or (conversely) when composing a program out of modules, we must take care to minimize dependencies between modules caused by error handling. C++ provides exceptions to decouple the detection and reporting of errors from the handling of errors. Therefore, the discussion of how to represent modules as namespaces (§8.2) is followed by a demonstration of how we can use excep- tions to further improve modularity (§8.3). There are many more notions of modularity than the ones discussed in this chapter and the next. For example, we might use concurrently executing and communicating processes to represent important aspects of modularity. Similarly, the use of separate address spaces and the communica- tion of information between address spaces are important topics not discussed here. I consider these notions of modularity largely independent and orthogonal. Interestingly, in each case, sepa- rating a system into modules is easy. The hard problem is to provide safe, convenient, and efficient communication across module boundaries. 8.2 Namespaces [name.namespace] A namespace is a mechanism for expressing logical grouping. That is, if some declarations logi- cally belong together according to some criteria, they can be put in a common namespace to express that fact. For example, the declarations of the parser from the desk calculator (§6.1.1) may be placed in a namespace P ar se r: Pa rs er n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e e xp r(b oo l); do ub le ex pr bo ol d ou bl e p ri m(b oo l g et { /* ... */ } do ub le pr im bo ol ge t) d ou bl e t er m(b oo l g et { /* ... */ } do ub le te rm bo ol ge t) d ou bl e e xp r(b oo l g et { /* ... */ } do ub le ex pr bo ol ge t) } The function e xp r() must be declared first and then later defined to break the dependency loop ex pr described in §6.1.1. The input part of the desk calculator could be also placed in its own namespace: n am es pa ce L ex er { na me sp ac e Le xe r e nu m T ok en _v al ue { en um To ke n_ va lu e N AM E, NA ME N UM BE R, NU MB ER E ND EN D, P LU S=´+´, PL US M IN US MI NU S=´-´, M UL MU L=´*´, D IV DI V=´/´, P RI NT PR IN T=´;´, A SS IG N=´=´, AS SI GN L P=´(´, LP R P=´)´ RP }; T ok en _v al ue c ur r_ to k; To ke n_ va lu e cu rr _t ok do ub le nu mb er _v al ue d ou bl e n um be r_ va lu e; st ri ng st ri ng _v al ue s tr in g s tr in g_ va lu e; T ok en _v al ue g et _t ok en To ke n_ va lu e ge t_ to ke n() { /* ... */ } }
    178. 168 Namespaces and Exceptions Chapter 8 This use of namespaces makes it reasonably obvious what the lexer and the parser provide to a user. However, had I included the source code for the functions, this structure would have been obscured. If function bodies are included in the declaration of a realistically-sized namespace, you typically have to wade through pages or screenfuls of information to find what services are offered, that is, to find the interface. An alternative to relying on separately specified interfaces is to provide a tool that extracts an interface from a module that includes implementation details. I don’t consider that a good solution. Specifying interfaces is a fundamental design activity (see §23.4.3.4), a module can provide differ- ent interfaces to different users, and often an interface is designed long before the implementation details are made concrete. Here is a version of the P ar se r with the interface separated from the implementation: Pa rs er n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e p ri m(b oo l); do ub le pr im bo ol d ou bl e t er m(b oo l); do ub le te rm bo ol d ou bl e e xp r(b oo l); do ub le ex pr bo ol } d ou bl e P ar se r::p ri m(b oo l g et { /* ... */ } do ub le Pa rs er pr im bo ol ge t) d ou bl e P ar se r::t er m(b oo l g et { /* ... */ } do ub le Pa rs er te rm bo ol ge t) d ou bl e P ar se r::e xp r(b oo l g et { /* ... */ } do ub le Pa rs er ex pr bo ol ge t) Note that as a result of separating the implementation of the interface, each function now has exactly one declaration and one definition. Users will see only the interface containing declarations. The implementation – in this case, the function bodies – will be placed ‘‘somewhere else’’ where a user need not look. As shown, a member can be declared within a namespace definition and defined later using the namespace-name::member-name notation. Members of a namespace must be introduced using this notation: n am es pa ce n am es pa ce na me { na me sp ac e na me sp ac e-n am e // declaration and definitions } We cannot declare a new member of a namespace outside a namespace definition using the quali- fier syntax. For example: v oi d P ar se r::l og ic al bo ol ; vo id Pa rs er lo gi ca l(b oo l) // error: no logical() in Parser The idea is to make it reasonably easy to find all names in a namespace declaration and also to catch errors such as misspellings and type mismatches. For example: d ou bl e P ar se r::t re m(b oo l); do ub le Pa rs er tr em bo ol // error: no trem() in Parser d ou bl e P ar se r::p ri m(i nt ; do ub le Pa rs er pr im in t) // error: Parser::prim() takes a bool argument A namespace is a scope. Thus, ‘‘namespace’’ is a very fundamental and relatively simple concept. The larger a program is, the more useful namespaces are to express logical separations of its parts. Ordinary local scopes, global scopes, and classes are namespaces (§C.10.3). Ideally, every entity in a program belongs to some recognizable logical unit (‘‘module’’). Therefore, every declaration in a nontrivial program should ideally be in some namespace named to
    179. Section 8.2 Namespaces 169 indicate its logical role in the program. The exception is m ai n(), which must be global in order ma in for the run-time environment to recognize it as special (§8.3.3). 8.2.1 Qualified Names [name.qualified] A namespace is a scope. The usual scope rules hold for namespaces, so if a name is previously declared in the namespace or in an enclosing scope, it can be used without further fuss. A name from another namespace can be used when qualified by the name of its namespace. For example: d ou bl e P ar se r::t er m(b oo l g et do ub le Pa rs er te rm bo ol ge t) // note Parser:: qualification { d ou bl e l ef t = p ri m(g et ; do ub le le ft pr im ge t) // no qualification needed f or (;;) fo r sw it ch Le xe r: cu rr _t ok s wi tc h (L ex er :c ur r_ to k) { // note Lexer:: qualification c as e L ex er :M UL ca se Le xe r: MU L: // note Lexer:: qualification l ef t *= p ri m(t ru e); le ft pr im tr ue // no qualification needed // ... } // ... } The P ar se r qualifier is necessary to state that this t er m() is the one declared in P ar se r and not Pa rs er te rm Pa rs er some unrelated global function. Because t er m() is a member of P ar se r, it need not use a qualifier te rm Pa rs er cu rr _t ok for p ri m(). However, had the L ex er qualifier not been present, c ur r_ to k would have been consid- pr im Le xe r ered undeclared because the members of namespace L ex er are not in scope from within the P ar se r Le xe r Pa rs er namespace. 8.2.2 Using Declarations [name.using.dcl] When a name is frequently used outside its namespace, it can be a bother to repeatedly qualify it with its namespace name. Consider: d ou bl e P ar se r::p ri m(b oo l g et do ub le Pa rs er pr im bo ol ge t) // handle primaries { i f (g et L ex er :g et _t ok en ; if ge t) Le xe r: ge t_ to ke n() sw it ch Le xe r: cu rr _t ok s wi tc h (L ex er :c ur r_ to k) { c as e L ex er :N UM BE R: ca se Le xe r: NU MB ER // floating-point constant L ex er :g et _t ok en ; Le xe r: ge t_ to ke n() re tu rn Le xe r: nu mb er _v al ue r et ur n L ex er :n um be r_ va lu e; c as e L ex er :N AM E: ca se Le xe r: NA ME ta bl e[L ex er st ri ng _v al ue d ou bl e& v = t ab le Le xe r::s tr in g_ va lu e]; do ub le { i f (L ex er :g et _t ok en == L ex er :A SS IG N) v = e xp r(t ru e); if Le xe r: ge t_ to ke n() Le xe r: AS SI GN ex pr tr ue r et ur n v re tu rn v; } c as e L ex er :M IN US ca se Le xe r: MI NU S: // unary minus r et ur n -p ri m(t ru e); re tu rn pr im tr ue
    180. 170 Namespaces and Exceptions Chapter 8 c as e L ex er :L P: ca se Le xe r: LP d ou bl e e = e xp r(t ru e); do ub le ex pr tr ue { if Le xe r: cu rr _t ok i f (L ex er :c ur r_ to k != L ex er :R P) r et ur n E rr or :e rr or Le xe r: RP re tu rn Er ro r: er ro r(\") e xp ec te d\"); ex pe ct ed L ex er :g et _t ok en ; Le xe r: ge t_ to ke n() // eat ’)’ r et ur n e re tu rn e; } c as e L ex er :E ND ca se Le xe r: EN D: r et ur n 1 re tu rn 1; d ef au lt de fa ul t: r et ur n E rr or :e rr or pr im ar y e xp ec te d\"); re tu rn Er ro r: er ro r(\"p ri ma ry ex pe ct ed } } The repeated qualification L ex er is tedious and distracting. This redundancy can be eliminated by Le xe r a using-declaration to state in one place that the g et _t ok en used in this scope is L ex er g et _t ok en ge t_ to ke n Le xe r’s ge t_ to ke n. For example: d ou bl e P ar se r::p ri m(b oo l g et do ub le Pa rs er pr im bo ol ge t) // handle primaries { u si ng L ex er :g et _t ok en us in g Le xe r: ge t_ to ke n; // use Lexer’s get_token us in g Le xe r: cu rr _t ok u si ng L ex er :c ur r_ to k; // use Lexer’s curr_tok u si ng E rr or :e rr or us in g Er ro r: er ro r; // use Error’s error i f (g et g et _t ok en ; if ge t) ge t_ to ke n() sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { c as e L ex er :N UM BE R: ca se Le xe r: NU MB ER // floating-point constant g et _t ok en ; ge t_ to ke n() re tu rn Le xe r: nu mb er _v al ue r et ur n L ex er :n um be r_ va lu e; c as e L ex er :N AM E: ca se Le xe r: NA ME ta bl e[L ex er st ri ng _v al ue d ou bl e& v = t ab le Le xe r::s tr in g_ va lu e]; do ub le { i f (g et _t ok en == L ex er :A SS IG N) v = e xp r(t ru e); if ge t_ to ke n() Le xe r: AS SI GN ex pr tr ue r et ur n v re tu rn v; } c as e L ex er :M IN US ca se Le xe r: MI NU S: // unary minus r et ur n -p ri m(t ru e); re tu rn pr im tr ue c as e L ex er :L P: ca se Le xe r: LP d ou bl e e = e xp r(t ru e); do ub le ex pr tr ue { if cu rr _t ok i f (c ur r_ to k != L ex er :R P) r et ur n e rr or Le xe r: RP re tu rn er ro r(\") e xp ec te d\"); ex pe ct ed g et _t ok en ; ge t_ to ke n() // eat ’)’ r et ur n e re tu rn e; } c as e L ex er :E ND ca se Le xe r: EN D: r et ur n 1 re tu rn 1; d ef au lt de fa ul t: r et ur n e rr or pr im ar y e xp ec te d\"); re tu rn er ro r(\"p ri ma ry ex pe ct ed } } A using-declaration introduces a local synonym. It is often a good idea to keep local synonyms as local as possible to avoid confusion.
    181. Section 8.2.2 Using Declarations 171 However, all parser functions use similar sets of names from other modules. We can therefore place the using-declarations in the P ar se r’s namespace definition: Pa rs er n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e p ri m(b oo l); do ub le pr im bo ol d ou bl e t er m(b oo l); do ub le te rm bo ol d ou bl e e xp r(b oo l); do ub le ex pr bo ol u si ng L ex er :g et _t ok en us in g Le xe r: ge t_ to ke n; // use Lexer’s get_token us in g Le xe r: cu rr _t ok u si ng L ex er :c ur r_ to k; // use Lexer’s curr_tok u si ng E rr or :e rr or us in g Er ro r: er ro r; // use Error’s error } This allows us to simplify the P ar se r functions almost to our original version (§6.1.1): Pa rs er d ou bl e P ar se r::t er m(b oo l g et do ub le Pa rs er te rm bo ol ge t) // multiply and divide { d ou bl e l ef t = p ri m(g et ; do ub le le ft pr im ge t) f or (;;) fo r sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { c as e L ex er :M UL ca se Le xe r: MU L: l ef t *= p ri m(t ru e); le ft pr im tr ue b re ak br ea k; c as e L ex er :D IV ca se Le xe r: DI V: i f (d ou bl e d = p ri m(t ru e)) { if do ub le pr im tr ue l ef t /= d le ft d; b re ak br ea k; } r et ur n e rr or di vi de b y 0 ; re tu rn er ro r(\"d iv id e by 0\") d ef au lt de fa ul t: r et ur n l ef t; re tu rn le ft } } I could have introduced the token names into the P ar se r’s namespace. However, I left them Pa rs er explicitly qualified as a reminder of P ar se r’s dependency on L ex er Pa rs er Le xe r. 8.2.3 Using Directives [name.using.dir] What if our aim were to simplify the P ar se r functions to be exactly our original versions? This Pa rs er would be a reasonable aim for a large program that was being converted to using namespaces from a previous version with less explicit modularity. A using-directive makes names from a namespace available almost as if they had been declared outside their namespace (§8.2.8). For example: n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e p ri m(b oo l); do ub le pr im bo ol d ou bl e t er m(b oo l); do ub le te rm bo ol d ou bl e e xp r(b oo l); do ub le ex pr bo ol
    182. 172 Namespaces and Exceptions Chapter 8 u si ng n am es pa ce L ex er us in g na me sp ac e Le xe r; // make all names from Lexer available u si ng n am es pa ce E rr or us in g na me sp ac e Er ro r; // make all names from Error available } This allows us to write P ar se r’s functions exactly as we originally did (§6.1.1): Pa rs er d ou bl e P ar se r::t er m(b oo l g et do ub le Pa rs er te rm bo ol ge t) // multiply and divide { d ou bl e l ef t = p ri m(g et ; do ub le le ft pr im ge t) f or (;;) fo r sw it ch cu rr _t ok s wi tc h (c ur r_ to k) { // Lexer’s curr_tok c as e M UL ca se MU L: // Lexer’s MUL l ef t *= p ri m(t ru e); le ft pr im tr ue b re ak br ea k; c as e D IV ca se DI V: // Lexer’s DIV i f (d ou bl e d = p ri m(t ru e)) { if do ub le pr im tr ue l ef t /= d le ft d; b re ak br ea k; } r et ur n e rr or di vi de b y 0 ; re tu rn er ro r(\"d iv id e by 0\") // Error’s error d ef au lt de fa ul t: r et ur n l ef t; re tu rn le ft } } Global using-directives are a tool for transition (§8.2.9) and are otherwise best avoided. In a name- space, a u si ng di re ct iv e is a tool for namespace composition (§8.2.8). In a function (only), a us in g-d ir ec ti ve u si ng di re ct iv e can be safely used as a notational convenience (§8.3.3.1). us in g-d ir ec ti ve 8.2.4 Multiple Interfaces [name.multi] It should be clear that the namespace definition we evolved for P ar se r is not the interface that the Pa rs er P ar se r presents to its users. Instead, it is the set of declarations that is needed to write the individ- Pa rs er ual parser functions conveniently. The P ar se r’s interface to its users should be far simpler: Pa rs er n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e e xp r(b oo l); do ub le ex pr bo ol } Fortunately, the two namespace-definitions for P ar se r can coexist so that each can be used where it Pa rs er is most appropriate. We see the namespace P ar se r used to provide two things: Pa rs er [1] The common environment for the functions implementing the parser [2] The external interface offered by the parser to its users Thus, the driver code, m ai n(), should see only: ma in n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for users d ou bl e e xp r(b oo l); do ub le ex pr bo ol } The functions implementing the parser should see whichever interface we decided on as the best for expressing those functions’ shared environment. That is:
    183. Section 8.2.4 Multiple Interfaces 173 n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for implementers d ou bl e p ri m(b oo l); do ub le pr im bo ol d ou bl e t er m(b oo l); do ub le te rm bo ol d ou bl e e xp r(b oo l); do ub le ex pr bo ol u si ng L ex er :g et _t ok en us in g Le xe r: ge t_ to ke n; // use Lexer’s get_token us in g Le xe r: cu rr _t ok u si ng L ex er :c ur r_ to k; // use Lexer’s curr_tok u si ng E rr or :e rr or us in g Er ro r: er ro r; // use Error’s error } or graphically: P ar se r’ Pa rs er P ar se r Pa rs er . . D ri ve r Dr iv er P ar se r implementation Pa rs er The arrows represent ‘‘relies on the interface provided by’’ relations. P ar se r´ is the small interface offered to users. The name P ar se r´ (Parser prime) is not a C++ Pa rs er Pa rs er identifier. It was chosen deliberately to indicate that this interface doesn’t have a separate name in the program. The lack of a separate name need not lead to confusion because programmers natu- rally invent different and obvious names for the different interfaces and because the physical layout of the program (see §9.3.2) naturally provides separate (file) names. The interface offered to implementers is larger than the interface offered to users. Had this interface been for a realistically-sized module in a real system, it would change more often than the interface seen by users. It is important that the users of a module (in this case, m ai n() using ma in P ar se r) are insulated from such changes. Pa rs er We don’t need to use two separate namespaces to express the two different interfaces, but if we wanted to, we could. Designing interfaces is one of the most fundamental design activities and one in which major benefits can be gained and lost. Consequently, it is worthwhile to consider what we are really trying to achieve and to discuss a number of alternatives. Please keep in mind that the solution presented is the simplest of those we consider, and often the best. Its main weaknesses are that the two interfaces don’t have separate names and that the compiler doesn’t necessarily have sufficient information to check the consistency of the two defini- tions of the namespace. However, even though the compiler doesn’t always get the opportunity to check the consistency, it usually does. Furthermore, the linker catches most errors missed by the compiler. The solution presented here is the one I use for the discussion of physical modularity (§9.3) and the one I recommend in the absence of further logical constraints (see also §8.2.7). 8.2.4.1 Interface Design Alternatives [name.alternatives] The purpose of interfaces is to minimize dependencies between different parts of a program. Mini- mal interfaces lead to systems that are easier to understand, have better data hiding properties, are easier to modify, and compile faster.
    184. 174 Namespaces and Exceptions Chapter 8 When dependencies are considered, it is important to remember that compilers and program- mers tend to take a somewhat simple-minded approach to them: ‘‘If a definition is in scope at point X, then anything written at point X depends on anything stated in that definition.’’ Typically, things are not really that bad because most definitions are irrelevant to most code. Given the defi- nitions we have used, consider: n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for implementers // ... d ou bl e e xp r(b oo l); do ub le ex pr bo ol // ... } i nt m ai n() in t ma in { // ... P ar se r::e xp r(f al se ; Pa rs er ex pr fa ls e) // ... } The function m ai n() depends on P ar se r::e xp r() only, but it takes time, brain power, computa- ma in Pa rs er ex pr tion, etc., to figure that out. Consequently, for realistically-sized programs people and compilation systems often play it safe and assume that where there might be a dependency, there is one. This is typically a perfectly reasonable approach. Thus, our aim is to express our program so that the set of potential dependencies is reduced to the set of actual dependencies. First, we try the obvious: define a user interface to the parser in terms of the implementer inter- face we already have: n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for implementers // ... d ou bl e e xp r(b oo l); do ub le ex pr bo ol // ... } na me sp ac e Pa rs er _i nt er fa ce n am es pa ce P ar se r_ in te rf ac e { // interface for users u si ng P ar se r::e xp r; us in g Pa rs er ex pr } Pa rs er _i nt er fa ce Clearly, users of P ar se r_ in te rf ac e depend only, and indirectly, on P ar se r::e xp r(). However, a Pa rs er ex pr crude look at the dependency graph gives us this: P ar se r Pa rs er Pa rs er _i nt er fa ce P ar se r_ in te rf ac e . . . . D ri ve r Dr iv er P ar se r implementation Pa rs er
    185. Section 8.2.4.1 Interface Design Alternatives 175 Now the d ri ve r appears vulnerable to any change in the P ar se r interface from which it was sup- dr iv er Pa rs er posed to be insulated. Even this appearance of a dependency is undesirable, so we explicitly Pa rs er _i nt er fa ce restrict P ar se r_ in te rf ac e’s dependency on P ar se r by having only the relevant part of the imple- Pa rs er menter interface to parser (that was called P ar se r´ earlier) in scope where we define Pa rs er Pa rs er _i nt er fa ce P ar se r_ in te rf ac e: n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for users d ou bl e e xp r(b oo l); do ub le ex pr bo ol } na me sp ac e Pa rs er _i nt er fa ce n am es pa ce P ar se r_ in te rf ac e { // separately named interface for users u si ng P ar se r::e xp r; us in g Pa rs er ex pr } or graphically: P ar se r’ Pa rs er P ar se r Pa rs er Pa rs er _i nt er fa ce P ar se r_ in te rf ac e . . . . P ar se r implementation Pa rs er D ri ve r Dr iv er To ensure the consistency of P ar se r and P ar se r´, we again rely on the compilation system as a Pa rs er Pa rs er whole, rather than on just the compiler working on a single compilation unit. This solution differs Pa rs er _i nt er fa ce from the one in §8.2.4 only by the extra namespace P ar se r_ in te rf ac e. If we wanted to, we could Pa rs er _i nt er fa ce give P ar se r_ in te rf ac e a concrete representation by giving it its own e xp r() function: ex pr na me sp ac e Pa rs er _i nt er fa ce n am es pa ce P ar se r_ in te rf ac e { d ou bl e e xp r(b oo l); do ub le ex pr bo ol } Pa rs er _i nt er fa ce Now P ar se r need not be in scope in order to define P ar se r_ in te rf ac e. It needs to be in scope only Pa rs er Pa rs er _i nt er fa ce ex pr where P ar se r_ in te rf ac e::e xp r() is defined: do ub le Pa rs er _i nt er fa ce ex pr bo ol ge t) d ou bl e P ar se r_ in te rf ac e::e xp r(b oo l g et { r et ur n P ar se r::e xp r(g et ; re tu rn Pa rs er ex pr ge t) } This last variant can be represented graphically like this:
    186. 176 Namespaces and Exceptions Chapter 8 Pa rs er _i nt er fa ce P ar se r_ in te rf ac e P ar se r Pa rs er Pa rs er _i nt er fa ce P ar se r_ in te rf ac e implementation . . . . D ri ve r Dr iv er P ar se r implementation Pa rs er Now all dependencies are minimized. Everything is concrete and properly named. However, for most problems I face, this solution is also massive overkill. 8.2.5 Avoiding Name Clashes [name.clash] Namespaces are intended to express logical structure. The simplest such structure is the distinction between code written by one person vs. code written by someone else. This simple distinction can be of great practical importance. When we use only a single global scope, it is unnecessarily difficult to compose a program out of separate parts. The problem is that the supposedly-separate parts each define the same names. When combined into the same program, these names clash. Consider: // my.h: c ha r f ch ar ; ch ar f(c ha r) i nt f in t); in t f(i nt c la ss S tr in g { /* ... */ }; cl as s St ri ng // your.h: c ha r f ch ar ; ch ar f(c ha r) d ou bl e f do ub le ; do ub le f(d ou bl e) c la ss S tr in g { /* ... */ }; cl as s St ri ng Given these definitions, a third party cannot easily use both m y.h and y ou r.h The obvious solu- my h yo ur h. tion is to wrap each set of declarations in its own namespace: n am es pa ce M y { na me sp ac e My c ha r f ch ar ; ch ar f(c ha r) i nt f in t); in t f(i nt c la ss S tr in g { /* ... */ }; cl as s St ri ng } n am es pa ce Y ou r { na me sp ac e Yo ur c ha r f ch ar ; ch ar f(c ha r) d ou bl e f do ub le ; do ub le f(d ou bl e) c la ss S tr in g { /* ... */ }; cl as s St ri ng } Now we can use declarations from M y and Y ou r through explicit qualification (§8.2.1), using- My Yo ur declarations (§8.2.2), or using-directives (§8.2.3).
    187. Section 8.2.5.1 Unnamed Namespaces 177 8.2.5.1 Unnamed Namespaces [name.unnamed] It is often useful to wrap a set of declarations in a namespace simply to protect against the possibil- ity of name clashes. That is, the aim is to preserve locality of code rather than to present an inter- face to users. For example: #i nc lu de \"h ea de r.h in cl ud e he ad er h\" n am es pa ce M in e { na me sp ac e Mi ne i nt a in t a; v oi d f vo id f() { /* ... */ } i nt g in t g() { /* ... */ } } Since we don’t want the name M in e to be known outside a local context, it simply becomes a Mi ne bother to invent a redundant global name that might accidentally clash with someone else’s names. In that case, we can simply leave the namespace without a name: #i nc lu de \"h ea de r.h in cl ud e he ad er h\" n am es pa ce { na me sp ac e i nt a in t a; v oi d f vo id f() { /* ... */ } i nt g in t g() { /* ... */ } } Clearly, there has to be some way of accessing members of an unnamed namespace from the out- side. Consequently, an unnamed namespace has an implied using-directive. The previous declara- tion is equivalent to n am es pa ce $$$ { na me sp ac e i nt a in t a; v oi d f vo id f() { /* ... */ } i nt g in t g() { /* ... */ } } u si ng n am es pa ce $$$; us in g na me sp ac e where $$$ is some name unique to the scope in which the namespace is defined. In particular, unnamed namespaces in different translation units are different. As desired, there is no way of naming a member of an unnamed namespace from another translation unit. 8.2.6 Name Lookup [name.koenig] A function taking an argument of type T is more often than not defined in the same namespace as T Consequently, if a function isn’t found in the context of its use, we look in the namespaces of its T. arguments. For example: n am es pa ce C hr on o { na me sp ac e Ch ro no c la ss D at e { /* ... */ }; cl as s Da te b oo l o pe ra to r==(c on st D at e&, c on st s td :s tr in g&); bo ol op er at or co ns t Da te co ns t st d: st ri ng
    188. 178 Namespaces and Exceptions Chapter 8 s td :s tr in g f or ma t(c on st D at e&); st d: st ri ng fo rm at co ns t Da te // make string representation // ... } v oi d f Ch ro no :D at e d i nt i vo id f(C hr on o: Da te d, in t i) { s td :s tr in g s = f or ma t(d ; st d: st ri ng fo rm at d) // Chrono::format() s td :s tr in g t = f or ma t(i ; st d: st ri ng fo rm at i) // error: no format() in scope } This lookup rule saves the programmer a lot of typing compared to using explicit qualification, yet it doesn’t pollute the namespace the way a using-directive (§8.2.3) can. It is especially useful for operator operands (§11.2.4) and template arguments (§C.13.8.4), where explicit qualification can be quite cumbersome. Note that the namespace itself needs to be in scope and the function must be declared before it can be found and used. Naturally, a function can take arguments from more than one namespace. For example: v oi d f Ch ro no :D at e d s td :s tr in g s vo id f(C hr on o: Da te d, st d: st ri ng s) { i f (d == s { if d s) // ... } e ls e i f (d == \"A ug us t 4 1 91 4\") { el se if d Au gu st 4, 19 14 // ... } } In such cases, we look for the function in the scope of the call (as ever) and in the namespaces of every argument (including each argument’s class and base classes) and do the usual overload reso- lution (§7.4) of all functions we find. In particular, for the call d s, we look for o pe ra to r== in d==s op er at or the scope surrounding f f(), in the s td namespace (where == is defined for s tr in g), and in the st d st ri ng C hr on o namespace. There is a s td :o pe ra to r==(), but it doesn’t take a D at e argument, so we Ch ro no st d: op er at or Da te use C hr on o::o pe ra to r==(), which does. See also §11.2.4. Ch ro no op er at or When a class member invokes a function, other members of the same class and its base classes are preferred over functions potentially found based on the argument types (§11.2.4). 8.2.7 Namespace Aliases [name.alias] If users give their namespaces short names, the names of different namespaces will clash: n am es pa ce A { // short name, will clash (eventually) na me sp ac e // ... } A :S tr in g s 1 = \"G ri eg A: St ri ng s1 Gr ie g\"; A :S tr in g s 2 = \"N ie ls en A: St ri ng s2 Ni el se n\"; However, long namespace names can be impractical in real code:
    189. Section 8.2.7 Namespace Aliases 179 na me sp ac e Am er ic an _T el ep ho ne _a nd _T el eg ra ph n am es pa ce A me ri ca n_ Te le ph on e_ an d_ Te le gr ap h { // too long // ... } Am er ic an _T el ep ho ne _a nd _T el eg ra ph St ri ng s3 A me ri ca n_ Te le ph on e_ an d_ Te le gr ap h::S tr in g s 3 = \"G ri eg Gr ie g\"; Am er ic an _T el ep ho ne _a nd _T el eg ra ph St ri ng s4 A me ri ca n_ Te le ph on e_ an d_ Te le gr ap h::S tr in g s 4 = \"N ie ls en Ni el se n\"; This dilemma can be resolved by providing a short alias for a longer namespace name: // use namespace alias to shorten names: na me sp ac e AT T Am er ic an _T el ep ho ne _a nd _T el eg ra ph n am es pa ce A TT = A me ri ca n_ Te le ph on e_ an d_ Te le gr ap h; A TT :S tr in g s 3 = \"G ri eg AT T: St ri ng s3 Gr ie g\"; A TT :S tr in g s 4 = \"N ie ls en AT T: St ri ng s4 Ni el se n\"; Namespace aliases also allow a user to refer to ‘‘the library’’ and have a single declaration defining what library that really is. For example: na me sp ac e Li b Fo un da ti on _l ib ra ry _v 2r 11 n am es pa ce L ib = F ou nd at io n_ li br ar y_ v2 r1 1; // ... L ib :s et s Li b: se t s; L ib :S tr in g s 5 = \"S ib el iu s\"; Li b: St ri ng s5 Si be li us This can immensely simplify the task of replacing one version of a library with another. By using Fo un da ti on _l ib ra ry _v 2r 11 L ib rather than F ou nd at io n_ li br ar y_ v2 r1 1 directly, you can update to version ‘‘v3r02’’ by chang- Li b ing the initialization of the alias L ib and recompiling. The recompile will catch source level incom- Li b patibilities. On the other hand, overuse of aliases (of any kind) can lead to confusion. 8.2.8 Namespace Composition [name.compose] Often, we want to compose an interface out of existing interfaces. For example: n am es pa ce H is _s tr in g { na me sp ac e Hi s_ st ri ng c la ss S tr in g { /* ... */ }; cl as s St ri ng S tr in g o pe ra to r+(c on st S tr in g&, c on st S tr in g&); St ri ng op er at or co ns t St ri ng co ns t St ri ng S tr in g o pe ra to r+(c on st S tr in g&, c on st c ha r*); St ri ng op er at or co ns t St ri ng co ns t ch ar v oi d f il l(c ha r); vo id fi ll ch ar // ... } n am es pa ce H er _v ec to r { na me sp ac e He r_ ve ct or t em pl at e<c la ss T c la ss V ec to r { /* ... */ }; te mp la te cl as s T> cl as s Ve ct or // ... } na me sp ac e My _l ib n am es pa ce M y_ li b { u si ng n am es pa ce H is _s tr in g; us in g na me sp ac e Hi s_ st ri ng u si ng n am es pa ce H er _v ec to r; us in g na me sp ac e He r_ ve ct or vo id my _f ct St ri ng v oi d m y_ fc t(S tr in g&); }
    190. 180 Namespaces and Exceptions Chapter 8 My _l ib Given this, we can now write the program in terms of M y_ li b: v oi d f vo id f() { My _l ib St ri ng M y_ li b::S tr in g s = \"B yr on By ro n\"; // finds My_lib::His_string::String // ... } us in g na me sp ac e My _l ib u si ng n am es pa ce M y_ li b; v oi d g Ve ct or St ri ng vo id g(V ec to r<S tr in g>& v s) vs { // ... my _f ct vs 5]) m y_ fc t(v s[5 ; // ... } My _l ib St ri ng If an explicitly qualified name (such as M y_ li b::S tr in g) isn’t declared in the namespace men- tioned, the compiler looks in namespaces mentioned in using-directives (such as H is _s tr in g). Hi s_ st ri ng Only if we need to define something, do we need to know the real namespace of an entity: vo id My _l ib fi ll v oi d M y_ li b::f il l() // error: no fill() declared in My_lib { // ... } v oi d H is _s tr in g::f il l() vo id Hi s_ st ri ng fi ll // ok: fill() declared in His_string { // ... } vo id My _l ib my _f ct My _l ib Ve ct or My _l ib St ri ng v oi d M y_ li b::m y_ fc t(M y_ li b::V ec to r<M y_ li b::S tr in g>& v // ok v) { // ... } Ideally, a namespace should [1] express a logically coherent set of features, [2] not give users access to unrelated features, and [3] not impose a significant notational burden on users. The composition techniques presented here and in the following subsections – together with the #i nc lu de mechanism (§9.2.1) – provide strong support for this. in cl ud e 8.2.8.1 Selection [name.select] Occasionally, we want access to only a few names from a namespace. We could do that by writing a namespace declaration containing only those names we want. For example, we could declare a version of H is _s tr in g that provided the S tr in g itself and the concatenation operator only: Hi s_ st ri ng St ri ng
    191. Section 8.2.8.1 Selection 181 n am es pa ce H is _s tr in g { na me sp ac e Hi s_ st ri ng // part of His_string only c la ss S tr in g { /* ... */ }; cl as s St ri ng S tr in g o pe ra to r+(c on st S tr in g&, c on st S tr in g&); St ri ng op er at or co ns t St ri ng co ns t St ri ng S tr in g o pe ra to r+(c on st S tr in g&, c on st c ha r*); St ri ng op er at or co ns t St ri ng co ns t ch ar } However, unless I am the designer or maintainer of H is _s tr in g, this can easily get messy. A Hi s_ st ri ng change to the ‘‘real’’ definition of H is _s tr in g will not be reflected in this declaration. Selection of Hi s_ st ri ng features from a namespace is more explicitly made with using-declarations: na me sp ac e My _s tr in g n am es pa ce M y_ st ri ng { u si ng H is _s tr in g::S tr in g; us in g Hi s_ st ri ng St ri ng u si ng H is _s tr in g::o pe ra to r+; us in g Hi s_ st ri ng op er at or // use any + from His_string } A using-declaration brings every declaration with a given name into scope. In particular, a single using-declaration can bring in every variant of an overloaded function. In this way, if the maintainer of H is _s tr in g adds a member function to S tr in g or an overloaded Hi s_ st ri ng St ri ng version of the concatenation operator, that change will automatically become available to users of M y_ st ri ng Conversely, if a feature is removed from H is _s tr in g or has its interface changed, My _s tr in g. Hi s_ st ri ng My _s tr in g affected uses of M y_ st ri ng will be detected by the compiler (see also §15.2.2). 8.2.8.2 Composition and Selection [name.comp] Combining composition (by using-directives) with selection (by using-declarations) yields the flexibility needed for most real-world examples. With these mechanisms, we can provide access to a variety of facilities in such a way that we resolve name clashes and ambiguities arising from their composition. For example: n am es pa ce H is _l ib { na me sp ac e Hi s_ li b c la ss S tr in g { /* ... */ }; cl as s St ri ng t em pl at e<c la ss T c la ss V ec to r { /* ... */ }; te mp la te cl as s T> cl as s Ve ct or // ... } n am es pa ce H er _l ib { na me sp ac e He r_ li b t em pl at e<c la ss T c la ss V ec to r { /* ... */ }; te mp la te cl as s T> cl as s Ve ct or c la ss S tr in g { /* ... */ }; cl as s St ri ng // ... } na me sp ac e My _l ib n am es pa ce M y_ li b { u si ng n am es pa ce H is _l ib // everything from His_lib us in g na me sp ac e Hi s_ li b; u si ng n am es pa ce H er _l ib // everything from Her_lib us in g na me sp ac e He r_ li b; u si ng H is _l ib :S tr in g; us in g Hi s_ li b: St ri ng // resolve potential clash in favor of His_lib u si ng H er _l ib :V ec to r; us in g He r_ li b: Ve ct or // resolve potential clash in favor of Her_lib t em pl at e<c la ss T c la ss L is t { /* ... */ }; // additional stuff te mp la te cl as s T> cl as s Li st // ... }
    192. 182 Namespaces and Exceptions Chapter 8 When looking into a namespace, names explicitly declared there (including names declared by using-declarations) take priority over names made accessible in another scope by a using-directive My _l ib (see also §C.10.1). Consequently, a user of M y_ li b will see the name clashes for S tr in g and V ec to r St ri ng Ve ct or resolved in favor of H is _l ib :S tr in g and H er _l ib :V ec to r. Also, M y_ li b::L is t will be used by My _l ib Li st Hi s_ li b: St ri ng He r_ li b: Ve ct or default independently of whether H is _l ib or H er _l ib are providing a L is t. Hi s_ li b He r_ li b Li st Usually, I prefer to leave a name unchanged when including it into a new namespace. In that way, I don’t have to remember two different names for the same entity. However, sometimes a new name is needed or simply nice to have. For example: n am es pa ce L ib 2 { na me sp ac e Li b2 u si ng n am es pa ce H is _l ib // everything from His_lib us in g na me sp ac e Hi s_ li b; u si ng n am es pa ce H er _l ib // everything from Her_lib us in g na me sp ac e He r_ li b; u si ng H is _l ib :S tr in g; us in g Hi s_ li b: St ri ng // resolve potential clash in favor of His_lib u si ng H er _l ib :V ec to r; us in g He r_ li b: Ve ct or // resolve potential clash in favor of Her_lib t yp ed ef H er _l ib :S tr in g H er _s tr in g; ty pe de f He r_ li b: St ri ng He r_ st ri ng // rename t em pl at e<c la ss T c la ss H is _v ec te mp la te cl as s T> cl as s Hi s_ ve c // ‘‘rename’’ : p ub li c H is _l ib :V ec to r<T { /* ... */ }; pu bl ic Hi s_ li b: Ve ct or T> t em pl at e<c la ss T c la ss L is t { /* ... */ }; // additional stuff te mp la te cl as s T> cl as s Li st // ... } There is no specific language mechanism for renaming. Instead, the general mechanisms for defin- ing new entities are used. 8.2.9 Namespaces and Old Code [name.get] Millions of lines of C and C++ code rely on global names and existing libraries. How can we use namespaces to alleviate problems in such code? Redesigning existing code isn’t always a viable option. Fortunately, it is possible to use C libraries as if they were defined in a namespace. How- ever, this cannot be done for libraries written in C++ (§9.2.4). On the other hand, namespaces are designed so that they can be introduced with minimal disruption into an older C++ program. 8.2.9.1 Namespaces and C [name.c] Consider the canonical first C program: #i nc lu de <s td io h> in cl ud e st di o.h i nt m ai n() in t ma in { p ri nt f(\"H el lo w or ld \\n ; pr in tf He ll o, wo rl d!\\ n\") } Breaking this program wouldn’t be a good idea. Making standard libraries special cases isn’t a good idea either. Consequently, the language rules for namespaces are designed to make it rela- tively easy to take a program written without namespaces and turn it into a more explicitly struc- tured one using namespaces. In fact, the calculator program (§6.1) is an example of this.
    193. Section 8.2.9.1 Namespaces and C 183 The using-directive is the key to achieving this. For example, the declarations of the standard C I/O facilities from the C header s td io h are wrapped in a namespace like this: st di o.h // stdio.h: n am es pa ce s td { na me sp ac e st d // ... i nt p ri nt f(c on st c ha r* ... ); in t pr in tf co ns t ch ar // ... } u si ng n am es pa ce s td us in g na me sp ac e st d; This achieves backwards compatibility. Also, a new header file c st di o is defined for people who cs td io don’t want the names implicitly available: // cstdio: n am es pa ce s td { na me sp ac e st d // ... i nt p ri nt f(c on st c ha r* ... ); in t pr in tf co ns t ch ar // ... } C++ standard library implementers who worry about replication of declarations will, of course, define s td io h by including c st di o: st di o.h cs td io // stdio.h: #i nc lu de cs td io in cl ud e<c st di o> u si ng n am es pa ce s td us in g na me sp ac e st d; I consider nonlocal using-directives primarily a transition tool. Most code referring to names from other namespaces can be expressed more clearly with explicit qualification and using-declarations. The relationship between namespaces and linkage is described in §9.2.4. 8.2.9.2 Namespaces and Overloading [name.over] Overloading (§7.4) works across namespaces. This is essential to allow us to migrate existing libraries to use namespaces with minimal source code changes. For example: // old A.h: v oi d f in t); vo id f(i nt // ... // old B.h: v oi d f ch ar ; vo id f(c ha r) // ... // old user.c: #i nc lu de \"A h\" in cl ud e A.h #i nc lu de \"B h\" in cl ud e B.h
    194. 184 Namespaces and Exceptions Chapter 8 v oi d g vo id g() { f a´); f(´a // calls the f() from B.h } This program can be upgraded to a version using namespaces without changing the actual code: // new A.h: n am es pa ce A { na me sp ac e v oi d f in t); vo id f(i nt // ... } // new B.h: n am es pa ce B { na me sp ac e v oi d f ch ar ; vo id f(c ha r) // ... } // new user.c: #i nc lu de \"A h\" in cl ud e A.h #i nc lu de \"B h\" in cl ud e B.h u si ng n am es pa ce A us in g na me sp ac e A; u si ng n am es pa ce B us in g na me sp ac e B; v oi d g vo id g() { f a´); f(´a // calls the f() from B.h } Had we wanted to keep u se r.c completely unchanged, we would have placed the using-directives us er c in the header files. 8.2.9.3 Namespaces Are Open [name.open] A namespace is open; that is, you can add names to it from several namespace declarations. For example: n am es pa ce A { na me sp ac e i nt f ; // now A has member f() in t f() } n am es pa ce A { na me sp ac e i nt g ; // now A has two members, f() and g() in t g() } In this way, we can support large program fragments within a single namespace the way an older library or application lives within the single global namespace. To do this, we must distribute the namespace definition over several header and source code files. As shown by the calculator exam- ple (§8.2.4), the openness of namespaces allows us to present different interfaces to different kinds
    195. Section 8.2.9.3 Namespaces Are Open 185 of users by presenting different parts of a namespace. This openness is also an aid to transition. For example, // my header: v oi d f ; // my function vo id f() // ... #i nc lu de st di o.h in cl ud e<s td io h> i nt g ; // my function in t g() // ... can be rewritten without reordering of the declarations: // my header: n am es pa ce M in e { na me sp ac e Mi ne v oi d f ; // my function vo id f() // ... } #i nc lu de st di o.h in cl ud e<s td io h> n am es pa ce M in e { na me sp ac e Mi ne i nt g ; // my function in t g() // ... } When writing new code, I prefer to use many smaller namespaces (see §8.2.8) rather than putting really major pieces of code into a single namespace. However, that is often impractical when con- verting major pieces of software to use namespaces. When defining a previously declared member of a namespace, it is safer to use the M in e:: syn- Mi ne tax than to re-open M in e. For example: Mi ne v oi d M in e::f f() vo id Mi ne ff // error: no ff() declared in Mine { // ... } A compiler catches this error. However, because new functions can be defined within a namespace, a compiler cannot catch the equivalent error in a re-opened namespace: n am es pa ce M in e { // re-opening Mine to define functions na me sp ac e Mi ne v oi d f f() // oops! no ff() declared in Mine; ff() is added to Mine by this definition vo id ff { // ... } // ... } The compiler has no way of knowing that you didn’t want that new f f(). ff
    196. 186 Namespaces and Exceptions Chapter 8 8.3 Exceptions [name.except] When a program is composed of separate modules, and especially when those modules come from separately developed libraries, error handling needs to be separated into two distinct parts: [1] The reporting of error conditions that cannot be resolved locally [2] The handling of errors detected elsewhere The author of a library can detect run-time errors but does not in general have any idea what to do about them. The user of a library may know how to cope with such errors but cannot detect them – or else they would be handled in the user’s code and not left for the library to find. In the calculator example, we bypassed this problem by designing the program as a whole. By doing that, we could fit error handling into our overall framework. However, when we separate the logical parts of the calculator into separate namespaces, we see that every namespace depends on namespace E rr or (§8.2.2) and that the error handling in E rr or relies on every module behaving Er ro r Er ro r appropriately after an error. Let’s assume that we don’t have the freedom to design the calculator as a whole and don’t want the tight coupling between E rr or and all other modules. Instead, assume Er ro r that the parser, etc., are written without knowledge of how a driver might like to handle errors. Even though e rr or was very simple, it embodied a strategy for error handling: er ro r() n am es pa ce E rr or { na me sp ac e Er ro r i nt n o_ of _e rr or s; in t no _o f_ er ro rs d ou bl e e rr or co ns t c ha r* s do ub le er ro r(c on st ch ar s) { s td :c er r << \"e rr or \" << s << ´\\ n´; st d: ce rr er ro r: \\n n o_ of _e rr or s++; no _o f_ er ro rs r et ur n 1 re tu rn 1; } } The e rr or er ro r() function writes out an error message, supplies a default value that allows its caller to continue a computation, and keeps track of a simple error state. Importantly, every part of the pro- gram knows that e rr or er ro r() exists, how to call it, and what to expect from it. For a program com- posed of separately-developed libraries, that would be too much to assume. Exceptions are C++’s means of separating error reporting from error handling. In this section, exceptions are briefly described in the context of their use in the calculator example. Chapter 14 provides a more extensive discussion of exceptions and their uses. 8.3.1 Throw and Catch [name.throw] The notion of an exception is provided to help deal with error reporting. For example: s tr uc t R an ge _e rr or { st ru ct Ra ng e_ er ro r i nt i in t i; R an ge _e rr or in t i i) { i = i i; } // constructor (§2.5.2, §10.2.3) Ra ng e_ er ro r(i nt ii ii };
    197. Section 8.3.1 Throw and Catch 187 ch ar to _c ha r(i nt i) c ha r t o_ ch ar in t i { if i<n um er ic _l im it s<c ha r>: mi n() || n um er ic _l im it s<c ha r>::m ax i f (i nu me ri c_ li mi ts ch ar :m in nu me ri c_ li mi ts ch ar ma x()<i / see §22.2 i)/ t hr ow R an ge _E rr or ; th ro w Ra ng e_ Er ro r() r et ur n c re tu rn c; } The t o_ ch ar function either returns the c ha r with the numeric value i or throws a R an ge _e rr or to _c ha r() ch ar Ra ng e_ er ro r. The fundamental idea is that a function that finds a problem it cannot cope with throws an excep- tion, hoping that its (direct or indirect) caller can handle the problem. A function that wants to han- dle a problem can indicate that it is willing to catch exceptions of the type used to report the prob- to _c ha r() lem. For example, to call t o_ ch ar and catch the exception it might throw, we could write: v oi d g in t i vo id g(i nt i) { t ry { tr y to _c ha r(i c ha r c = t o_ ch ar i); ch ar // ... } c at ch (R an ge _e rr or { ca tc h Ra ng e_ er ro r) c er r << \"o op s\\ n\"; ce rr oo ps \\n } } The construct c at ch ( /* ... */ ) { ca tc h // ... } is called an exception handler. It can be used only immediately after a block prefixed with the key- word t ry or immediately after another exception handler; c at ch is also a keyword. The parentheses tr y ca tc h contain a declaration that is used in a way similar to how a function argument declaration is used. That is, it specifies the type of the objects that can be caught by this handler and optionally names the object caught. For example, if we wanted to know the value of the R an ge _e rr or thrown, we Ra ng e_ er ro r would provide a name for the argument to c at ch exactly the way we name function arguments. For ca tc h example: v oi d h in t i vo id h(i nt i) { t ry { tr y to _c ha r(i c ha r c = t o_ ch ar i); ch ar // ... } c at ch (R an ge _e rr or x { ca tc h Ra ng e_ er ro r x) oo ps to _c ha r(\" c er r << \"o op s: t o_ ch ar << x i << \")\\ n\"; ce rr x.i \\n } } If any code in a try-block – or called from it – throws an exception, the try-block’s handlers will be
    198. 188 Namespaces and Exceptions Chapter 8 examined. If the exception thrown is of a type specified for a handler, that handler is executed. If not, the exception handlers are ignored and the try-block acts just like an ordinary block. Basically, C++ exception handling is a way to transfer control to designated code in a calling function. Where needed, some information about the error can be passed along to the caller. C programmers can think of exception handling as a well-behaved mechanism replacing s et jm p/l on gj mp (§16.1.2). The important interaction between exception handling and classes is se tj mp lo ng jm p described in Chapter 14. 8.3.2 Discrimination of Exceptions [name.discrimination] Typically, a program will have several different possible run-time errors. Such errors can be mapped into exceptions with distinct names. I prefer to define types with no other purpose than exception handling. This minimizes confusion about their purpose. In particular, I never use a built-in type, such as i nt as an exception. In a large program, I would have no effective way to in t, find unrelated uses of i nt exceptions. Thus, I could never be sure that such other uses didn’t inter- in t fere with my use. Our calculator (§6.1) must handle two kinds of run-time errors: syntax errors and attempts to divide by zero. No values need to be passed to a handler from the code that detects an attempt to divide by zero, so zero divide can be represented by a simple empty type: st ru ct Ze ro _d iv id e s tr uc t Z er o_ di vi de { }; On the other hand, a handler would most likely prefer to get an indication of what kind of syntax error occurred. Here, we pass a string along: st ru ct Sy nt ax _e rr or s tr uc t S yn ta x_ er ro r { c on st c ha r* p co ns t ch ar p; Sy nt ax _e rr or co ns t ch ar q) S yn ta x_ er ro r(c on st c ha r* q { p = q } q; }; For notational convenience, I added a constructor (§2.5.2, §10.2.3) to the s tr uc t. st ru ct A user of the parser can discriminate between the two exceptions by adding handlers for both to a t ry block. Where needed, the appropriate handler will be entered. If we ‘‘fall through the bot- tr y tom’’ of a handler, the execution continues at the end of the list of handlers: t ry { tr y // ... e xp r(f al se ; ex pr fa ls e) // we get here if and only if expr() didn’t cause an exception // ... } ca tc h Sy nt ax _e rr or c at ch (S yn ta x_ er ro r) { // handle syntax error }
    199. Section 8.3.2 Discrimination of Exceptions 189 ca tc h Ze ro _d iv id e) c at ch (Z er o_ di vi de { // handle divide by zero } // we get here if expr didn’t cause an exception or if a Syntax_error // or Zero_divide exception was caught (and its handler didn’t return, // throw an exception, or in some other way alter the flow of control). A list of handlers looks a bit like a s wi tc h statement, but there is no need for b re ak statements. The sw it ch br ea k syntax of a list of handlers differs from the syntax of a list of cases partly for that reason and partly to indicate that each handler is a scope (§4.9.4). A function need not catch all possible exceptions. For example, the previous try-block didn’t try to catch exceptions potentially generated by the parser’s input operations. Those exceptions simply ‘‘pass through,’’ searching for a caller with an appropriate handler. From the language’s point of view, an exception is considered handled immediately upon entry into its handler so that any exceptions thrown while executing a handler must be dealt with by the callers of the try-block. For example, this does not cause an infinite loop: c la ss i np ut _o ve rf lo w { /* ... */ }; cl as s in pu t_ ov er fl ow v oi d f vo id f() { t ry { tr y // ... } c at ch (i np ut _o ve rf lo w) { ca tc h in pu t_ ov er fl ow // ... t hr ow i np ut _o ve rf lo w(); th ro w in pu t_ ov er fl ow } } Exception handlers can be nested. For example: c la ss X XI I { /* ... */ }; cl as s XX II v oi d f vo id f() { // ... t ry { tr y // ... } c at ch (X XI I) { ca tc h XX II t ry { tr y // something complicated } c at ch (X XI I) { ca tc h XX II // complicated handler code failed } } // ... }
    200. 190 Namespaces and Exceptions Chapter 8 However, such nesting is rare in human-written code and is more often than not an indication of poor style. 8.3.3 Exceptions in the Calculator [name.calc] Given the basic exception-handling mechanism, we can rework the calculator example from §6.1 to separate the handling of errors found at run-time from the main logic of the calculator. This will result in an organization of the program that more realistically matches what is found in programs built from separate, loosely connected parts. First, e rr or can be eliminated. Instead, the parser functions know only the types used to sig- er ro r() nal errors: n am es pa ce E rr or { na me sp ac e Er ro r st ru ct Ze ro _d iv id e s tr uc t Z er o_ di vi de { }; st ru ct Sy nt ax _e rr or s tr uc t S yn ta x_ er ro r { c on st c ha r* p co ns t ch ar p; Sy nt ax _e rr or co ns t ch ar q) S yn ta x_ er ro r(c on st c ha r* q { p = q } q; }; } The parser detects three syntax errors: T ok en _v al ue L ex er :g et _t ok en To ke n_ va lu e Le xe r: ge t_ to ke n() { u si ng n am es pa ce s td us in g na me sp ac e st d; // to use cin, isalpha(), etc. // ... d ef au lt de fa ul t: // NAME, NAME =, or error i f (i sa lp ha ch { if is al ph a(c h)) c in pu tb ac k(c h); ci n.p ut ba ck ch st ri ng _v al ue c in >> s tr in g_ va lu e; ci n re tu rn cu rr _t ok NA ME r et ur n c ur r_ to k=N AM E; } th ro w Er ro r: Sy nt ax _e rr or ba d to ke n\") t hr ow E rr or :S yn ta x_ er ro r(\"b ad t ok en ; } } d ou bl e P ar se r::p ri m(b oo l g et do ub le Pa rs er pr im bo ol ge t) // handle primaries { // ... c as e L ex er :L P: ca se Le xe r: LP d ou bl e e = e xp r(t ru e); do ub le ex pr tr ue { if cu rr _t ok Le xe r: RP th ro w Er ro r: Sy nt ax _e rr or i f (c ur r_ to k != L ex er :R P) t hr ow E rr or :S yn ta x_ er ro r(\"‘)´ e xp ec te d\"); ex pe ct ed g et _t ok en ; ge t_ to ke n() // eat ’)’ r et ur n e re tu rn e; }
    201. Section 8.3.3 Exceptions in the Calculator 191 c as e L ex er :E ND ca se Le xe r: EN D: r et ur n 1 re tu rn 1; d ef au lt de fa ul t: th ro w Er ro r: Sy nt ax _e rr or pr im ar y ex pe ct ed t hr ow E rr or :S yn ta x_ er ro r(\"p ri ma ry e xp ec te d\"); } } When a syntax error is detected, t hr ow is used to transfer control to a handler defined in some th ro w (direct or indirect) caller. The t hr ow operator also passes a value to the handler. For example, th ro w th ro w Sy nt ax _e rr or pr im ar y ex pe ct ed t hr ow S yn ta x_ er ro r(\"p ri ma ry e xp ec te d\"); Sy nt ax _e rr or passes a S yn ta x_ er ro r object containing a pointer to the string p ri ma ry e xp ec te d to the handler. pr im ar y ex pe ct ed Reporting a divide-by-zero error doesn’t require any data to be passed along: d ou bl e P ar se r::t er m(b oo l g et do ub le Pa rs er te rm bo ol ge t) // multiply and divide { // ... c as e L ex er :D IV ca se Le xe r: DI V: i f (d ou bl e d = p ri m(t ru e)) { if do ub le pr im tr ue l ef t /= d le ft d; b re ak br ea k; } th ro w Er ro r: Ze ro _d iv id e() t hr ow E rr or :Z er o_ di vi de ; // ... } Ze ro _d iv id e Sy nt ax _e rr or The driver can now be defined to handle Z er o_ di vi de and S yn ta x_ er ro r exceptions. For example: i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv { // ... w hi le (*i np ut { wh il e in pu t) t ry { tr y L ex er :g et _t ok en ; Le xe r: ge t_ to ke n() if Le xe r: cu rr _t ok i f (L ex er :c ur r_ to k == L ex er :E ND b re ak Le xe r: EN D) br ea k; if Le xe r: cu rr _t ok i f (L ex er :c ur r_ to k == L ex er :P RI NT c on ti nu e; Le xe r: PR IN T) co nt in ue c ou t << P ar se r::e xp r(f al se << ´\\ n´; co ut Pa rs er ex pr fa ls e) \\n } ca tc h(E rr or Ze ro _d iv id e) c at ch Er ro r::Z er o_ di vi de { c er r << \"a tt em pt t o d iv id e b y z er o\\ n\"; ce rr at te mp t to di vi de by ze ro \\n s ki p(); sk ip } ca tc h(E rr or Sy nt ax _e rr or e) c at ch Er ro r::S yn ta x_ er ro r e { c er r << \"s yn ta x e rr or ce rr sy nt ax er ro r:\" << e p << \"\\ n\"; e.p \\n s ki p(); sk ip } } i f (i np ut != &c in d el et e i np ut if in pu t ci n) de le te in pu t; r et ur n n o_ of _e rr or s; re tu rn no _o f_ er ro rs }
    202. 192 Namespaces and Exceptions Chapter 8 The function s ki p() tries to bring the parser into a well-defined state after an error by skipping sk ip tokens until it finds an end-of-line or a semicolon. It, n o_ of _e rr or s,and i np ut are obvious candi- no _o f_ er ro rs in pu t dates for a D ri ve r namespace: Dr iv er n am es pa ce D ri ve r { na me sp ac e Dr iv er i nt n o_ of _e rr or s; in t no _o f_ er ro rs s td :i st re am i np ut st d: is tr ea m* in pu t; v oi d s ki p(); vo id sk ip } v oi d D ri ve r::s ki p() vo id Dr iv er sk ip { n o_ of _e rr or s++; no _o f_ er ro rs w hi le (*i np ut { wh il e in pu t) c ha r c h; ch ar ch i np ut ge t(c h); in pu t->g et ch s wi tc h (c h) { sw it ch ch c as e ´\\ n´: ca se \\n c as e ´;´: ca se i np ut ge t(c h); in pu t->g et ch r et ur n; re tu rn } } } The code for s ki p() is deliberately written at a lower level of abstraction than the parser code so as sk ip to avoid being caught by exceptions from the parser while handling parser exceptions. I retained the idea of counting the number of errors and reporting that number as the program’s return value. It is often useful to know if a program encountered an error even if it was able to recover from it. I did not put m ai n() in the D ri ve r namespace. The global m ai n() is the initial function of a ma in Dr iv er ma in program (§3.2); a m ai n() in another namespace has no special meaning. ma in 8.3.3.1 Alternative Error-Handling Strategies [name.strategy] The original error-handling code was shorter and more elegant than the version using exceptions. However, it achieved that elegance by tightly coupling all parts of the program. That approach doesn’t scale well to programs composed of separately developed libraries. We could consider eliminating the separate error-handling function s ki p() by introducing a sk ip state variable in m ai n(). For example: ma in i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv // example of poor style { // ... bo ol in _e rr or fa ls e; b oo l i n_ er ro r = f al se
    203. Section 8.3.3.1 Alternative Error-Handling Strategies 193 w hi le (*D ri ve r::i np ut { wh il e Dr iv er in pu t) t ry { tr y L ex er :g et _t ok en ; Le xe r: ge t_ to ke n() if Le xe r: cu rr _t ok i f (L ex er :c ur r_ to k == L ex er :E ND b re ak Le xe r: EN D) br ea k; if Le xe r: cu rr _t ok i f (L ex er :c ur r_ to k == L ex er :P RI NT { Le xe r: PR IN T) in _e rr or fa ls e; i n_ er ro r = f al se c on ti nu e; co nt in ue } if in _e rr or i f (i n_ er ro r == f al se c ou t << P ar se r::e xp r(f al se << ´\\ n´; fa ls e) co ut Pa rs er ex pr fa ls e) \\n } ca tc h(E rr or Ze ro _d iv id e) c at ch Er ro r::Z er o_ di vi de { c er r << \"a tt em pt t o d iv id e b y z er o\\ n\"; ce rr at te mp t to di vi de by ze ro \\n in _e rr or tr ue i n_ er ro r = t ru e; } ca tc h(E rr or Sy nt ax _e rr or e) c at ch Er ro r::S yn ta x_ er ro r e { c er r << \"s yn ta x e rr or << e p << \"\\ n\"; ce rr sy nt ax er ro r:\" e.p \\n in _e rr or tr ue i n_ er ro r = t ru e; } } i f (D ri ve r::i np ut != s td :c in d el et e D ri ve r::i np ut if Dr iv er in pu t st d: ci n) de le te Dr iv er in pu t; r et ur n D ri ve r::n o_ of _e rr or s; re tu rn Dr iv er no _o f_ er ro rs } I consider this a bad idea for several reasons: [1] State variables are a common source of confusion and errors, especially if they are allowed to proliferate and affect larger sections of a program. In particular, I consider the version of in _e rr or m ai n() using i n_ er ro r less readable than the version using s ki p(). ma in sk ip [2] It is generally a good strategy to keep error handling and ‘‘normal’’ code separate. [3] Doing error handling using the same level of abstraction as the code that caused the error is hazardous; the error-handling code might repeat the same error that triggered the error han- dling in the first place. I leave it as an exercise to find how that can happen for the version in _e rr or of m ai n() using i n_ er ro r (§8.5[7]). ma in [4] It is more work to modify the ‘‘normal’’ code to add error-handling code than to add sepa- rate error-handling routines. Exception handling is intended for dealing with nonlocal problems. If an error can be handled locally, it almost always should be. For example, there is no reason to use an exception to handle the too-many-arguments error: i nt m ai n(i nt a rg c, c ha r* a rg v[]) in t ma in in t ar gc ch ar ar gv { u si ng n am es pa ce s td us in g na me sp ac e st d; u si ng n am es pa ce D ri ve r; us in g na me sp ac e Dr iv er s wi tc h (a rg c) { sw it ch ar gc c as e 1 ca se 1: // read from standard input i np ut = &c in in pu t ci n; b re ak br ea k;
    204. 194 Namespaces and Exceptions Chapter 8 c as e 2 ca se 2: // read argument string i np ut = n ew i st ri ng st re am ar gv 1]); in pu t ne w is tr in gs tr ea m(a rg v[1 b re ak br ea k; d ef au lt de fa ul t: c er r << \"t oo m an y a rg um en ts \\n ce rr to o ma ny ar gu me nt s\\ n\"; r et ur n 1 re tu rn 1; } // as before } Exceptions are discussed further in Chapter 14. 8.4 Advice [name.advice] [1] Use namespaces to express logical structure; §8.2. [2] Place every nonlocal name, except m ai n(), in some namespace; §8.2. ma in [3] Design a namespace so that you can conveniently use it without accidentally gaining access to unrelated namespaces; §8.2.4. [4] Avoid very short names for namespaces; §8.2.7. [5] If necessary, use namespace aliases to abbreviate long namespaces names; §8.2.7. [6] Avoid placing heavy notational burdens on users of your namespaces; §8.2.2, §8.2.3. [7] Use the N am es pa ce :m em be r notation when defining namespace members; §8.2.8. Na me sp ac e: me mb er [8] Use u si ng n am es pa ce only for transition or within a local scope; §8.2.9. us in g na me sp ac e [9] Use exceptions to decouple the treatment of ‘‘errors’’ from the code dealing with the ordinary processing; §8.3.3. [10] Use user-defined rather than built-in types as exceptions; §8.3.2. [11] Don’t use exceptions when local control structures are sufficient; §8.3.3.1. 8.5 Exercises [name.exercises] 1. (∗2.5) Write a doubly-linked list of s tr in g module in the style of the S ta ck module from §2.4. st ri ng St ac k Exercise it by creating a list of names of programming languages. Provide a s or t() function so rt for that list, and provide a function that reverses the order of the strings in it. 2. (∗2) Take some not-too-large program that uses at least one library that does not use name- spaces and modify it to use a namespace for that library. Hint: §8.2.9. 3. (∗2) Modify the desk calculator program into a module in the style of §2.4 using namespaces. Don’t use any global using-directives. Keep a record of the mistakes you made. Suggest ways of avoiding such mistakes in the future. 4. (∗1) Write a program that throws an exception in one function and catches it in another. 5. (∗2) Write a program consisting of functions calling each other to a calling depth of 10. Give each function an argument that determines at which level an exception is thrown. Have m ai n() catch these exceptions and print out which exception is caught. Don’t forget the case ma in in which an exception is caught in the function that throws it.
    205. Section 8.5 Exercises 195 6. (∗2) Modify the program from §8.5[5] to measure if there is a difference in the cost of catching exceptions depending on where in a class stack the exception is thrown. Add a string object to each function and measure again. 7. (∗1) Find the error in the first version of m ai n() in §8.3.3.1. ma in 8. (∗2) Write a function that either returns a value or that throws that value based on an argument. Measure the difference in run-time between the two ways. 9. (∗2) Modify the calculator version from §8.5[3] to use exceptions. Keep a record of the mis- takes you make. Suggest ways of avoiding such mistakes in the future. 10. (∗2.5) Write p lu s(), m in us pl us mi nu s(), m ul ti pl y(), and d iv id e() functions that check for possible mu lt ip ly di vi de overflow and underflow and that throw exceptions if such errors happen. 11. (∗2) Modify the calculator to use the functions from §8.5[10].
    206. 196 Namespaces and Exceptions Chapter 8
    207. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 9 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Source Files and Programs Form must follow function. – Le Corbusier Separate compilation — linking — header files — standard library headers — the one- definition rule — linkage to non-C++ code — linkage and pointers to functions — using headers to express modularity — single-header organization — multiple-header organi- zation — include guards — programs — advice — exercises. 9.1 Separate Compilation [file.separate] A file is the traditional unit of storage (in a file system) and the traditional unit of compilation. There are systems that do not store, compile, and present C++ programs to the programmer as sets of files. However, the discussion here will concentrate on systems that employ the traditional use of files. Having a complete program in one file is usually impossible. In particular, the code for the standard libraries and the operating system is typically not supplied in source form as part of a user’s program. For realistically-sized applications, even having all of the user’s own code in a sin- gle file is both impractical and inconvenient. The way a program is organized into files can help emphasize its logical structure, help a human reader understand the program, and help the compiler to enforce that logical structure. Where the unit of compilation is a file, all of a file must be recom- piled whenever a change (however small) has been made to it or to something on which it depends. For even a moderately sized program, the amount of time spent recompiling can be significantly reduced by partitioning the program into files of suitable size. A user presents a source file to the compiler. The file is then preprocessed; that is, macro pro- cessing (§7.8) is done and #i nc lu de directives bring in headers (§2.4.1, §9.2.1). The result of pre- in cl ud e processing is called a translation unit. This unit is what the compiler proper works on and what the C++ language rules describe. In this book, I differentiate between source file and translation unit
    208. 198 Source Files and Programs Chapter 9 only where necessary to distinguish what the programmer sees from what the compiler considers. To enable separate compilation, the programmer must supply declarations providing the type information needed to analyze a translation unit in isolation from the rest of the program. The declarations in a program consisting of many separately compiled parts must be consistent in exactly the same way the declarations in a program consisting of a single source file must be. Your system will have tools to help ensure this. In particular, the linker can detect many kinds of incon- sistencies. The linker is the program that binds together the separately compiled parts. A linker is sometimes (confusingly) called a loader. Linking can be done completely before a program starts to run. Alternatively, new code can be added to the program (‘‘dynamically linked’’) later. The organization of a program into source files is commonly called the physical structure of a program. The physical separation of a program into separate files should be guided by the logical structure of the program. The same dependency concerns that guide the composition of programs out of namespaces guide its composition into source files. However, the logical and physical struc- ture of a program need not be identical. For example, it can be useful to use several source files to store the functions from a single namespace, to store a collection of namespace definitions in a sin- gle file, and to scatter the definition of a namespace over several files (§8.2.4). Here, we will first consider some technicalities relating to linking and then discuss two ways of breaking the desk calculator (§6.1, §8.2) into files. 9.2 Linkage [file.link] Names of functions, classes, templates, variables, namespaces, enumerations, and enumerators must be used consistently across all translation units unless they are explicitly specified to be local. It is the programmer’s task to ensure that every namespace, class, function, etc. is properly declared in every translation unit in which it appears and that all declarations referring to the same entity are consistent. For example, consider two files: // file1.c: i nt x = 1 in t 1; i nt f in t f() { /* do something */ } // file2.c: e xt er n i nt x ex te rn in t x; i nt f ; in t f() v oi d g vo id g() { x = f ; } f() The x and f f() used by g g() in f il e2 c are the ones defined in f il e1 c. The keyword e xt er n indi- fi le 2.c fi le 1.c ex te rn cates that the declaration of x in f il e2 c is (just) a declaration and not a definition (§4.9). Had x fi le 2.c been initialized, e xt er n would simply be ignored because a declaration with an initializer is always ex te rn a definition. An object must be defined exactly once in a program. It may be declared many times, but the types must agree exactly. For example: // file1.c: i nt x = 1 in t 1; i nt b = 1 in t 1; e xt er n i nt c ex te rn in t c;
    209. Section 9.2 Linkage 199 // file2.c: i nt x in t x; // meaning int x = 0; e xt er n d ou bl e b ex te rn do ub le b; e xt er n i nt c ex te rn in t c; There are three errors here: x is defined twice, b is declared twice with different types, and c is declared twice but not defined. These kinds of errors (linkage errors) cannot be detected by a com- piler that looks at only one file at a time. Most, however, are detectable by the linker. Note that a variable defined without an initializer in the global or a namespace scope is initialized by default. This is not the case for local variables (§4.9.5, §10.4.2) or objects created on the free store (§6.2.6). For example, the following program fragment contains two errors: // file1.c: i nt x in t x; i nt f in t f() { r et ur n x } re tu rn x; // file2.c: i nt x in t x; i nt g in t g() { r et ur n f ; } re tu rn f() The call of f f() in f il e2 c is an error because f fi le 2.c f() has not been declared in f il e2 c. Also, the pro- fi le 2.c gram will not link because x is defined twice. Note that these are not errors in C (§B.2.2). A name that can be used in translation units different from the one in which it was defined is said to have external linkage. All the names in the previous examples have external linkage. A name that can be referred to only in the translation unit in which it is defined is said to have internal linkage. An i nl in e function (§7.1.1, §10.2.9) must be defined – by identical definitions (§9.2.3) – in in li ne every translation unit in which it is used. Consequently, the following example isn’t just bad taste; it is illegal: // file1.c: i nl in e i nt f in t i { r et ur n i } in li ne in t f(i nt i) re tu rn i; // file2.c: i nl in e i nt f in t i { r et ur n i 1; } in li ne in t f(i nt i) re tu rn i+1 Unfortunately, this error is hard for an implementation to catch, and the following – otherwise per- fectly logical – combination of external linkage and inlining is banned to make life simpler for compiler writers: // file1.c: e xt er n i nl in e i nt g in t i ; ex te rn in li ne in t g(i nt i) i nt h in t i { r et ur n g i); } in t h(i nt i) re tu rn g(i // error: g() undefined in this translation unit // file2.c: e xt er n i nl in e i nt g in t i { r et ur n i 1; } ex te rn in li ne in t g(i nt i) re tu rn i+1 By default, c on st (§5.4) and t yp ed ef (§4.9.7) have internal linkage. Consequently, this example co ns ts ty pe de fs is legal (although potentially confusing):
    210. 200 Source Files and Programs Chapter 9 // file1.c: t yp ed ef i nt T ty pe de f in t T; c on st i nt x = 7 co ns t in t 7; // file2.c: t yp ed ef v oi d T ty pe de f vo id T; c on st i nt x = 8 co ns t in t 8; Global variables that are local to a single compilation unit are a common source of confusion and are best avoided. To ensure consistency, you should usually place global c on st and i nl in es in co ns ts in li ne header files only (§9.2.1). A c on st can be given external linkage by an explicit declaration: co ns t // file1.c: e xt er n c on st i nt a = 7 7; ex te rn co ns t in t 77 // file2.c: e xt er n c on st i nt a ex te rn co ns t in t a; v oi d g vo id g() { c ou t << a << ´\\ n´; co ut \\n } Here, g will print 7 7. g() 77 An unnamed namespace (§8.2.5) can be used to make names local to a compilation unit. The effect of an unnamed namespace is very similar to that of internal linkage. For example: // file 1.c: n am es pa ce { na me sp ac e c la ss X { /* ... */ }; cl as s v oi d f ; vo id f() i nt i in t i; // ... } // file2.c: c la ss X { /* ... */ }; cl as s v oi d f ; vo id f() i nt i in t i; // ... The function f in f il e1 c is not the same function as the f f() fi le 1.c f() in f il e2 c. Having a name local to fi le 2.c a translation unit and also using that same name elsewhere for an entity with external linkage is asking for trouble. In C and older C++ programs, the keyword s ta ti c is (confusingly) used to mean ‘‘use internal st at ic linkage’’ (§B.2.3). Don’t use s ta ti c except inside functions (§7.1.2) and classes (§10.2.4). st at ic
    211. Section 9.2.1 Header Files 201 9.2.1 Header Files [file.header] The types in all declarations of the same object, function, class, etc., must be consistent. Conse- quently, the source code submitted to the compiler and later linked together must be consistent. One imperfect but simple method of achieving consistency for declarations in different translation units is to #i nc lu de header files containing interface information in source files containing exe- in cl ud e cutable code and/or data definitions. The #i nc lu de mechanism is a text manipulation facility for gathering source program fragments in cl ud e together into a single unit (file) for compilation. The directive #i nc lu de \"t o_ be _i nc lu de d\" in cl ud e to _b e_ in cl ud ed replaces the line in which the #i nc lu de appears with the contents of the file t o_ be _i nc lu de d. The to _b e_ in cl ud ed in cl ud e content should be C++ source text because the compiler will proceed to read it. To include standard library headers, use the angle brackets < and > around the name instead of quotes. For example: #i nc lu de <i os tr ea m> in cl ud e io st re am // from standard include directory #i nc lu de \"m yh ea de r.h in cl ud e my he ad er h\" // from current directory Unfortunately, spaces are significant within the < > or \" \" of an include directive: // will not find <iostream> #i nc lu de < i os tr ea m > in cl ud e io st re am It may seem extravagant to recompile a file each time it is included somewhere, but the included files typically contain only declarations and not code needing extensive analysis by the compiler. Furthermore, most modern C++ implementations provide some form of precompiling of header files to minimize the work needed to handle repeated compilation of the same header. As a rule of thumb, a header may contain: _ _______________________________________________________________________  Named namespaces  n am es pa ce N { /* . .. */ } na me sp ac e .. .  Type definitions  s tr uc t P oi nt { i nt x y }; st ru ct Po in t in t x, y;    Template declarations t em pl at e<c la ss T c la ss Z te mp la te cl as s T> cl as s Z;   Template definitions t em pl at e<c la ss T c la ss V { /* . .. */ };  te mp la te cl as s T> cl as s .. .  Function declarations  e xt er n i nt s tr le n(c on st c ha r*); ex te rn in t st rl en co ns t ch ar  Inline function definitions in li ne ch ar ge t(c ha r* p) re tu rn p++; }  i nl in e c ha r g et ch ar p { r et ur n *p  Data declarations  e xt er n i nt a ex te rn in t a;    Constant definitions c on st f lo at p i = 3 .1 41 59 3; co ns t fl oa t pi 3. 14 15 93   Enumerations  e nu m L ig ht { r ed y el lo w, g re en }; en um Li gh t re d, ye ll ow gr ee n  Name declarations  c la ss M at ri x; cl as s Ma tr ix  Include directives  #i nc lu de <a lg or it hm in cl ud e al go ri th m>  Macro definitions  #d ef in e V ER SI ON 1 2 de fi ne VE RS IO N 12   #i fd ef _ _c pl us pl us if de f __ cp lu sp lu s  Conditional compilation directives   _Comments _______________________________________________________________________ /* c he ck f or e nd o f f il e */ ch ec k fo r en d of fi le This rule of thumb for what may be placed in a header is not a language requirement. It is simply a reasonable way of using the #i nc lu de mechanism to express the physical structure of a program. in cl ud e Conversely, a header should never contain:
    212. 202 Source Files and Programs Chapter 9 _ _____________________________________________________________________  Ordinary function definitions  c ha r g et ch ar p { r et ur n *p ch ar ge t(c ha r* p) re tu rn p++; }  Data definitions  i nt a in t a;    Aggregate definitions s ho rt t bl sh or t tb l[] = { 1 2 3 }; 1, 2,   Unnamed namespaces  n am es pa ce { /* . .. */ } na me sp ac e .. .  e xp or t t em pl at e<c la ss T f T t { /* . .. */ }   _Exported template definitions _____________________________________________________________________ ex po rt te mp la te cl as s T> f(T t) .. . Header files are conventionally suffixed by .h and files containing function or data definitions are h, suffixed by .c They are therefore often referred to as ‘‘.h files’’ and ‘‘.c files,’’ respectively. c. Other conventions, such as .C .c xx .c pp and .c c, are also found. The manual for your com- C, cx x, cp p, cc piler will be quite specific about this issue. The reason for recommending that the definition of simple constants, but not the definition of aggregates, be placed in header files is that it is hard for implementations to avoid replication of aggregates presented in several translation units. Furthermore, the simple cases are far more com- mon and therefore more important for generating good code. It is wise not to be too clever about the use of #i nc lu de My recommendation is to #i nc lu de in cl ud e. in cl ud e only complete declarations and definitions and to do so only in the global scope, in linkage specifi- cation blocks, and in namespace definitions when converting old code (§9.2.2). As usual, it is wise to avoid macro magic. One of my least favorite activities is tracking down an error caused by a name being macro-substituted into something completely different by a macro defined in an indi- rectly #i nc lu de header that I have never even heard of. in cl ud ed 9.2.2 Standard Library Headers [file.std.header] The facilities of the standard library are presented through a set of standard headers (§16.1.2). No suffix is needed for standard library headers; they are known to be headers because they are included using the #i nc lu de in cl ud e<...> syntax rather than #i nc lu de in cl ud e\"...\". The absence of a .h suf- h fix does not imply anything about how the header is stored. A header such as <m ap may be ma p> stored as a text file called m ap h in a standard directory. On the other hand, standard headers are ma p.h not required to be stored in a conventional manner. An implementation is allowed to take advan- tage of knowledge of the standard library definition to optimize the standard library implementation and the way standard headers are handled. For example, an implementation might have knowledge of the standard math library (§22.3) built in and treat #i nc lu de cm at h> as a switch that makes the in cl ud e<c ma th standard math functions available without reading any file. For each C standard-library header <X h>, there is a corresponding standard C++ header <c X>. X.h cX For example, #i nc lu de cs td io provides what #i nc lu de st di o.h does. A typical s td io h will in cl ud e<c st di o> in cl ud e<s td io h> st di o.h look something like this: #i fd ef _ _c pl us pl us if de f __ cp lu sp lu s // for C++ compliers only (§9.2.4) n am es pa ce s td { na me sp ac e st d // the standard library is defined in namespace std (§8.2.9) e xt er n \"C { ex te rn C\" // stdio functions have C linkage (§9.2.4) #e nd if en di f // ... i nt p ri nt f(c on st c ha r* ...); in t pr in tf co ns t ch ar // ...
    213. Section 9.2.2 Standard Library Headers 203 #i fd ef _ _c pl us pl us if de f __ cp lu sp lu s } } u si ng n am es pa ce s td us in g na me sp ac e st d; // make stdio available in global namespace #e nd if en di f That is, the actual declarations are (most likely) shared, but linkage and namespace issues must be addressed to allow C and C++ to share a header. 9.2.3 The One-Definition Rule [file.odr] A given class, enumeration, and template, etc., must be defined exactly once in a program. From a practical point of view, this means that there must be exactly one definition of, say, a class residing in a single file somewhere. Unfortunately, the language rule cannot be that simple. For example, the definition of a class may be composed through macro expansion (ugh!), while a definition of a class may be textually included in two source files by #i nc lu de directives (§9.2.1). in cl ud e Worse, a ‘‘file’’ isn’t a concept that is part of the C and C++ language definitions; there exist imple- mentations that do not store programs in source files. Consequently, the rule in the standard that says that there must be a unique definition of a class, template, etc., is phrased in a somewhat more complicated and subtle manner. This rule is com- monly referred to as ‘‘the one-definition rule,’’ the ODR. That is, two definitions of a class, tem- plate, or inline function are accepted as examples of the same unique definition if and only if [1] they appear in different translation units, and [2] they are token-for-token identical, and [3] the meanings of those tokens are the same in both translation units. For example: // file1.c: s tr uc t S { i nt a c ha r b }; st ru ct in t a; ch ar b; v oi d f S*); vo id f(S // file2.c: s tr uc t S { i nt a c ha r b }; st ru ct in t a; ch ar b; v oi d f S* p { /* ... */ } vo id f(S p) The ODR says that this example is valid and that S refers to the same class in both source files. However, it is unwise to write out a definition twice like that. Someone maintaining f il e2 c will fi le 2.c naturally assume that the definition of S in f il e2 c is the only definition of S and so feel free to fi le 2.c change it. This could introduce a hard-to-detect error. The intent of the ODR is to allow inclusion of a class definition in different translation units from a common source file. For example: // file s.h: s tr uc t S { i nt a c ha r b }; st ru ct in t a; ch ar b; v oi d f S*); vo id f(S
    214. 204 Source Files and Programs Chapter 9 // file1.c: #i nc lu de \"s h\" in cl ud e s.h // use f() here // file2.c: #i nc lu de \"s h\" in cl ud e s.h v oi d f S* p { /* ... */ } vo id f(S p) or graphically: s .h : s. h: s tr uc t S { i nt a c ha r b }; st ru ct in t a; ch ar b; v oi d f S*); vo id f(S f il e1 .c : fi le 1. c: f il e2 .c : fi le 2. c: #i nc lu de \" s. h\" in cl ud e \"s .h \" #i nc lu de \" s. h\" in cl ud e \"s .h \" // use f() here v oi d f S* p { /* . .. */ } vo id f(S p) .. . Here are examples of the three ways of violating the ODR: // file1.c: s tr uc t S 1 { i nt a c ha r b }; st ru ct S1 in t a; ch ar b; s tr uc t S 1 { i nt a c ha r b }; st ru ct S1 in t a; ch ar b; // error: double definition This is an error because a s tr uc t may not be defined twice in a single translation unit. st ru ct // file1.c: s tr uc t S 2 { i nt a c ha r b }; st ru ct S2 in t a; ch ar b; // file2.c: s tr uc t S 2 { i nt a c ha r b b; }; // error st ru ct S2 in t a; ch ar bb This is an error because S 2 is used to name classes that differ in a member name. S2 // file1.c: t yp ed ef i nt X ty pe de f in t X; s tr uc t S 3 { X a c ha r b }; st ru ct S3 a; ch ar b; // file2.c: t yp ed ef c ha r X ty pe de f ch ar X; s tr uc t S 3 { X a c ha r b }; st ru ct S3 a; ch ar b; // error Here the two definitions of S 3 are token-for-token identical, but the example is an error because the S3 meaning of the name X has sneakily been made to differ in the two files. Checking against inconsistent class definitions in separate translation units is beyond the ability of most C++ implementations. Consequently, declarations that violate the ODR can be a source of subtle errors. Unfortunately, the technique of placing shared definitions in headers and #i nc lu di ng in cl ud in g them doesn’t protect against this last form of ODR violation. Local typedefs and macros can change the meaning of #i nc lu de declarations: in cl ud ed
    215. Section 9.2.3 The One-Definition Rule 205 // file s.h: s tr uc t S { P oi nt a c ha r b }; st ru ct Po in t a; ch ar b; // file1.c: #d ef in e P oi nt i nt de fi ne Po in t in t #i nc lu de \"s h\" in cl ud e s.h // ... // file2.c: c la ss P oi nt { /* ... */ }; cl as s Po in t #i nc lu de \"s h\" in cl ud e s.h // ... The best defense against this kind of hackery is to make headers as self-contained as possible. For example, if class P oi nt had been declared in the s h header the error would have been detected. Po in t s.h A template definition can be #i nc lu de in several translation units as long as the ODR is in cl ud ed adhered to. In addition, an exported template can be used given only a declaration: // file1.c: e xp or t t em pl at e<c la ss T T t wi ce T t { r et ur n t t; } ex po rt te mp la te cl as s T> tw ic e(T t) re tu rn t+t // file2.c: t em pl at e<c la ss T T t wi ce T t ; te mp la te cl as s T> tw ic e(T t) // declaration i nt g in t i { r et ur n t wi ce i); } in t g(i nt i) re tu rn tw ic e(i The keyword e xp or t means ‘‘accessible from another translation unit’’ (§13.7). ex po rt 9.2.4 Linkage to Non-C++ Code [file.c] Typically, a C++ program contains parts written in other languages. Similarly, it is common for C++ code fragments to be used as parts of programs written mainly in some other language. Coop- eration can be difficult between program fragments written in different languages and even between fragments written in the same language but compiled with different compilers. For example, differ- ent languages and different implementations of the same language may differ in their use of machine registers to hold arguments, the layout of arguments put on a stack, the layout of built-in types such as strings and integers, the form of names passed by the compiler to the linker, and the amount of type checking required from the linker. To help, one can specify a linkage convention to be used in an e xt er n declaration. For example, this declares the C and C++ standard library func- ex te rn tion s tr cp y() and specifies that it should be linked according to the C linkage conventions: st rc py e xt er n \"C c ha r* s tr cp y(c ha r*, c on st c ha r*); ex te rn C\" ch ar st rc py ch ar co ns t ch ar The effect of this declaration differs from the effect of the ‘‘plain’’ declaration e xt er n c ha r* s tr cp y(c ha r*, c on st c ha r*); ex te rn ch ar st rc py ch ar co ns t ch ar only in the linkage convention used for calling s tr cp y(). st rc py The e xt er n \" C\" directive is particularly useful because of the close relationship between C and ex te rn \"C \" C++. Note that the C in e xt er n \" C\" names a linkage convention and not a language. Often, e xt er n ex te rn \"C \" ex te rn \" C\" is used to link to Fortran and assembler routines that happen to conform to the conventions of a \"C \" C implementation.
    216. 206 Source Files and Programs Chapter 9 An e xt er n \" C\" directive specifies the linkage convention (only) and does not affect the seman- ex te rn \"C \" tics of calls to the function. In particular, a function declared e xt er n \" C\" still obeys the C++ type ex te rn \"C \" checking and argument conversion rules and not the weaker C rules. For example: e xt er n \"C i nt f ; ex te rn C\" in t f() i nt g in t g() { r et ur n f 1); re tu rn f(1 // error: no argument expected } Adding e xt er n \" C\" to a lot of declarations can be a nuisance. Consequently, there is a mechanism ex te rn \"C \" to specify linkage to a group of declarations. For example: e xt er n \"C { ex te rn C\" c ha r* s tr cp y(c ha r*, c on st c ha r*); ch ar st rc py ch ar co ns t ch ar i nt s tr cm p(c on st c ha r*, c on st c ha r*); in t st rc mp co ns t ch ar co ns t ch ar i nt s tr le n(c on st c ha r*); in t st rl en co ns t ch ar // ... } This construct, commonly called a linkage block, can be used to enclose a complete C header to make a header suitable for C++ use. For example: e xt er n \"C { ex te rn C\" #i nc lu de <s tr in g.h in cl ud e st ri ng h> } This technique is commonly used to produce a C++ header from a C header. Alternatively, condi- tional compilation (§7.8.1) can be used to create a common C and C++ header: #i fd ef _ _c pl us pl us if de f __ cp lu sp lu s e xt er n \"C { ex te rn C\" #e nd if en di f c ha r* s tr cp y(c ha r*, c on st c ha r*); ch ar st rc py ch ar co ns t ch ar i nt s tr cm p(c on st c ha r*, c on st c ha r*); in t st rc mp co ns t ch ar co ns t ch ar i nt s tr le n(c on st c ha r*); in t st rl en co ns t ch ar // ... #i fd ef _ _c pl us pl us if de f __ cp lu sp lu s } #e nd if en di f The predefined macro name _ _c pl us pl us is used to ensure that the C++ constructs are edited out __ cp lu sp lu s when the file is used as a C header. Any declaration can appear within a linkage block: e xt er n \"C { ex te rn C\" // any declaration here, for example: i nt g 1; in t g1 // definition e xt er n i nt g 2; // declaration, not definition ex te rn in t g2 } In particular, the scope and storage class of variables are not affected, so g 1 is still a global variable g1
    217. Linkage to Non-C++ Code Section 9.2.4 207 – and is still defined rather than just declared. To declare but not define a variable, you must apply the keyword e xt er n directly in the declaration. For example: ex te rn e xt er n \"C i nt g 3; ex te rn C\" in t g3 // declaration, not definition This looks odd at first glance. However, it is a simple consequence of keeping the meaning unchanged when adding \" C\" to an extern declaration and the meaning of a file unchanged when \"C \" enclosing it in a linkage block. A name with C linkage can be declared in a namespace. The namespace will affect the way the name is accessed in the C++ program, but not the way a linker sees it. The p ri nt f() from s td is a pr in tf st d typical example: #i nc lu de cs td io in cl ud e<c st di o> v oi d f vo id f() { s td :p ri nt f(\"H el lo \"); st d: pr in tf He ll o, // ok p ri nt f(\"w or ld \\n ; pr in tf wo rl d!\\ n\") // error: no global printf() } Even when called s td :p ri nt f, it is still the same old C p ri nt f() (§21.8). st d: pr in tf pr in tf Note that this allows us to include libraries with C linkage into a namespace of our choice rather than polluting the global namespace. Unfortunately, the same flexibility is not available to us for headers defining functions with C++ linkage in the global namespace. The reason is that linkage of C++ entities must take namespaces into account so that the object files generated will reflect the use or lack of use of namespaces. 9.2.5 Linkage and Pointers to Functions [file.ptof] When mixing C and C++ code fragments in one program, we sometimes want to pass pointers to functions defined in one language to functions defined in the other. If the two implementations of the two languages share linkage conventions and function-call mechanisms, such passing of point- ers to functions is trivial. However, such commonality cannot in general be assumed, so care must be taken to ensure that a function is called the way it expects to be called. When linkage is specified for a declaration, the specified linkage applies to all function types, function names, and variable names introduced by the declaration(s). This makes all kinds of strange – and occasionally essential – combinations of linkage possible. For example: t yp ed ef i nt (*F T)(c on st v oi d*, c on st v oi d*); ty pe de f in t FT co ns t vo id co ns t vo id // FT has C++ linkage e xt er n \"C { ex te rn C\" t yp ed ef i nt (*C FT co ns t v oi d*, c on st v oi d*); ty pe de f in t CF T)(c on st vo id co ns t vo id // CFT has C linkage vo id qs or t(v oi d* p, si ze _t n, si ze _t sz CF T cm p) v oi d q so rt vo id p s iz e_ t n s iz e_ t s z, C FT c mp ; // cmp has C linkage } vo id is or t(v oi d* p, si ze _t n, si ze _t sz FT cm p) v oi d i so rt vo id p s iz e_ t n s iz e_ t s z, F T c mp ; // cmp has C++ linkage vo id xs or t(v oi d* p, si ze _t n, si ze _t sz CF T cm p) v oi d x so rt vo id p s iz e_ t n s iz e_ t s z, C FT c mp ; // cmp has C linkage ex te rn C\" vo id ys or t(v oi d* p, si ze _t n, si ze _t sz FT cm p) e xt er n \"C v oi d y so rt vo id p s iz e_ t n s iz e_ t s z, F T c mp ; // cmp has C++ linkage i nt c om pa re co ns t v oi d*, c on st v oi d*); in t co mp ar e(c on st vo id co ns t vo id // compare() has C++ linkage e xt er n \"C i nt c cm p(c on st v oi d*, c on st v oi d*); ex te rn C\" in t cc mp co ns t vo id co ns t vo id // ccmp() has C linkage
    218. 208 Source Files and Programs Chapter 9 v oi d f ch ar v i nt s z) vo id f(c ha r* v, in t sz { q so rt v,s z,1 co mp ar e); // error qs or t(v sz 1,&c om pa re q so rt v,s z,1 cc mp ; qs or t(v sz 1,&c cm p) // ok i so rt v,s z,1 co mp ar e); // ok is or t(v sz 1,&c om pa re i so rt v,s z,1 cc mp ; is or t(v sz 1,&c cm p) // error } An implementation in which C and C++ use the same calling conventions might accept the cases marked error as a language extension. 9.3 Using Header Files [file.using] To illustrate the use of headers, I present a few alternative ways of expressing the physical structure of the calculator program (§6.1, §8.2). 9.3.1 Single Header File [file.single] The simplest solution to the problem of partitioning a program into several files is to put the defini- tions in a suitable number of .c files and to declare the types needed for them to communicate in a c single .h file that each .c file #i nc lu de For the calculator program, we might use five .c files – h c in cl ud es. c l ex er c, p ar se r.c t ab le c, e rr or c, and m ai n.c – to hold function and data definitions, plus the le xe r.c pa rs er c, ta bl e.c er ro r.c ma in c header d c.h to hold the declarations of every name used in more than one .c file. dc h c The header d c.h would look like this: dc h // dc.h: n am es pa ce E rr or { na me sp ac e Er ro r st ru ct Ze ro _d iv id e s tr uc t Z er o_ di vi de { }; st ru ct Sy nt ax _e rr or s tr uc t S yn ta x_ er ro r { c on st c ha r* p co ns t ch ar p; Sy nt ax _e rr or co ns t ch ar q) S yn ta x_ er ro r(c on st c ha r* q { p = q } q; }; } #i nc lu de <s tr in g> in cl ud e st ri ng n am es pa ce L ex er { na me sp ac e Le xe r e nu m T ok en _v al ue { en um To ke n_ va lu e N AM E, NA ME N UM BE R, NU MB ER E ND EN D, P LU S=´+´, PL US M IN US MI NU S=´-´, M UL MU L=´*´, D IV DI V=´/´, P RI NT PR IN T=´;´, A SS IG N=´=´, L P=´(´, AS SI GN LP R P=´)´ RP }; e xt er n T ok en _v al ue c ur r_ to k; ex te rn To ke n_ va lu e cu rr _t ok ex te rn do ub le nu mb er _v al ue e xt er n d ou bl e n um be r_ va lu e; ex te rn st d: st ri ng st ri ng _v al ue e xt er n s td :s tr in g s tr in g_ va lu e;
    219. Section 9.3.1 Single Header File 209 T ok en _v al ue g et _t ok en ; To ke n_ va lu e ge t_ to ke n() } n am es pa ce P ar se r { na me sp ac e Pa rs er d ou bl e p ri m(b oo l g et ; do ub le pr im bo ol ge t) // handle primaries d ou bl e t er m(b oo l g et ; do ub le te rm bo ol ge t) // multiply and divide d ou bl e e xp r(b oo l g et ; do ub le ex pr bo ol ge t) // add and subtract u si ng L ex er :g et _t ok en us in g Le xe r: ge t_ to ke n; us in g Le xe r: cu rr _t ok u si ng L ex er :c ur r_ to k; } #i nc lu de <m ap in cl ud e ma p> e xt er n s td :m ap st d::s tr in g,d ou bl e> t ab le ex te rn st d: ma p<s td st ri ng do ub le ta bl e; n am es pa ce D ri ve r { na me sp ac e Dr iv er e xt er n i nt n o_ of _e rr or s; ex te rn in t no _o f_ er ro rs e xt er n s td :i st re am i np ut ex te rn st d: is tr ea m* in pu t; v oi d s ki p(); vo id sk ip } The keyword e xt er n is used for every declaration of a variable to ensure that multiple definitions do ex te rn not occur as we #i nc lu de d c.h in the various .c files. The corresponding definitions are found in in cl ud e dc h c the appropriate .c files. c Leaving out the actual code, l ex er c will look something like this: le xe r.c // lexer.c: #i nc lu de \"d c.h in cl ud e dc h\" #i nc lu de <i os tr ea m> in cl ud e io st re am #i nc lu de <c ct yp e> in cl ud e cc ty pe L ex er :T ok en _v al ue L ex er :c ur r_ to k; Le xe r: To ke n_ va lu e Le xe r: cu rr _t ok do ub le Le xe r: nu mb er _v al ue d ou bl e L ex er :n um be r_ va lu e; st d: st ri ng Le xe r: st ri ng _v al ue s td :s tr in g L ex er :s tr in g_ va lu e; L ex er :T ok en _v al ue L ex er :g et _t ok en Le xe r: To ke n_ va lu e Le xe r: ge t_ to ke n() { /* ... */ } Using headers in this manner ensures that every declaration in a header will at some point be included in the file containing its definition. For example, when compiling l ex er c the compiler le xe r.c will be presented with: n am es pa ce L ex er { // from dc.h na me sp ac e Le xe r // ... T ok en _v al ue g et _t ok en ; To ke n_ va lu e ge t_ to ke n() } // ... L ex er :T ok en _v al ue L ex er :g et _t ok en Le xe r: To ke n_ va lu e Le xe r: ge t_ to ke n() { /* ... */ } This ensures that the compiler will detect any inconsistencies in the types specified for a name. For example, had g et _t ok en ge t_ to ke n() been declared to return a T ok en _v al ue but defined to return an i nt the To ke n_ va lu e, in t, compilation of l ex er c would have failed with a type-mismatch error. If a definition is missing, le xe r.c
    220. 210 Source Files and Programs Chapter 9 the linker will catch the problem. If a declaration is missing, some .c file will fail to compile. c File p ar se r.c will look like this: pa rs er c // parser.c: #i nc lu de \"d c.h in cl ud e dc h\" d ou bl e P ar se r::p ri m(b oo l g et { /* ... */ } do ub le Pa rs er pr im bo ol ge t) d ou bl e P ar se r::t er m(b oo l g et { /* ... */ } do ub le Pa rs er te rm bo ol ge t) d ou bl e P ar se r::e xp r(b oo l g et { /* ... */ } do ub le Pa rs er ex pr bo ol ge t) File t ab le c will look like this: ta bl e.c // table.c: #i nc lu de \"d c.h in cl ud e dc h\" s td :m ap st d::s tr in g,d ou bl e> t ab le st d: ma p<s td st ri ng do ub le ta bl e; The symbol table is simply a variable of the standard library m ap type. This defines t ab le to be ma p ta bl e global. In a realistically-sized program, this kind of minor pollution of the global namespace builds up and eventually causes problems. I left this sloppiness here simply to get an opportunity to warn against it. Finally, file m ai n.c will look like this: ma in c // main.c: #i nc lu de \"d c.h in cl ud e dc h\" #i nc lu de <s st re am in cl ud e ss tr ea m> i nt D ri ve r::n o_ of _e rr or s = 0 in t Dr iv er no _o f_ er ro rs 0; s td :i st re am D ri ve r::i np ut = 0 st d: is tr ea m* Dr iv er in pu t 0; v oi d D ri ve r::s ki p() { /* ... */ } vo id Dr iv er sk ip i nt m ai n(i nt a rg c, c ha r* a rg v[]) { /* ... */ } in t ma in in t ar gc ch ar ar gv To be recognized as the m ai n() of the program, m ai n() must be a global function, so no name- ma in ma in space is used here. The physical structure of the system can be presented like this: . . . . . . . . < ss tr ea m> <s st re am > < ma p> <m ap > < st ri ng > <s tr in g> < cc ty pe > <c ct yp e> < io st re am > <i os tr ea m> . . . dc.h . . . . . . d ri ve r. c dr iv er .c p ar se r. c pa rs er .c t ab le .c ta bl e. c l ex er .c le xe r. c Note that the headers on the top are all headers for standard library facilities. For many forms of program analysis, these libraries can be ignored because they are well known and stable. For tiny
    221. Section 9.3.1 Single Header File 211 programs, the structure can be simplified by moving all #i nc lu de directives to the common header. in cl ud e This single-header style of physical partitioning is most useful when the program is small and its parts are not intended to be used separately. Note that when namespaces are used, the logical structure of the program is still represented within d c.h If namespaces are not used, the structure dc h. is obscured, although comments can be a help. For larger programs, the single header file approach is unworkable in a conventional file-based development environment. A change to the common header forces recompilation of the whole pro- gram, and updates of that single header by several programmers are error-prone. Unless strong emphasis is placed on programming styles relying heavily on namespaces and classes, the logical structure deteriorates as the program grows. 9.3.2 Multiple Header Files [file.multi] An alternative physical organization lets each logical module have its own header defining the facilities it provides. Each .c file then has a corresponding .h file specifying what it provides (its c h interface). Each .c file includes its own .h file and usually also other .h files that specify what it c h h needs from other modules in order to implement the services advertised in the interface. This phys- ical organization corresponds to the logical organization of a module. The interface for users is put into its .h file, the interface for implementers is put into a file suffixed _ im pl h, and the module’s _i mp l.h h definitions of functions, variables, etc. are placed in .c files. In this way, the parser is represented c by three files. The parser’s user interface is provided by p ar se r.h pa rs er h: // parser.h: n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for users d ou bl e e xp r(b oo l g et ; do ub le ex pr bo ol ge t) } pa rs er _i mp l.h The shared environment for the functions implementing the parser is presented by p ar se r_ im pl h: // parser_impl.h: #i nc lu de \"p ar se r.h in cl ud e pa rs er h\" #i nc lu de \"e rr or h\" in cl ud e er ro r.h #i nc lu de \"l ex er h\" in cl ud e le xe r.h n am es pa ce P ar se r { na me sp ac e Pa rs er // interface for implementers d ou bl e p ri m(b oo l g et ; do ub le pr im bo ol ge t) d ou bl e t er m(b oo l g et ; do ub le te rm bo ol ge t) d ou bl e e xp r(b oo l g et ; do ub le ex pr bo ol ge t) u si ng L ex er :g et _t ok en us in g Le xe r: ge t_ to ke n; us in g Le xe r: cu rr _t ok u si ng L ex er :c ur r_ to k; } The user’s header p ar se r.h is #i nc lu de to give the compiler a chance to check consistency pa rs er h in cl ud ed (§9.3.1). The functions implementing the parser are stored in p ar se r.c together with #i nc lu de directives pa rs er c in cl ud e for the headers that the P ar se r functions need: Pa rs er
    222. 212 Source Files and Programs Chapter 9 // parser.c: in cl ud e pa rs er _i mp l.h #i nc lu de \"p ar se r_ im pl h\" #i nc lu de \"t ab le h\" in cl ud e ta bl e.h d ou bl e P ar se r::p ri m(b oo l g et { /* ... */ } do ub le Pa rs er pr im bo ol ge t) d ou bl e P ar se r::t er m(b oo l g et { /* ... */ } do ub le Pa rs er te rm bo ol ge t) d ou bl e P ar se r::e xp r(b oo l g et { /* ... */ } do ub le Pa rs er ex pr bo ol ge t) Graphically, the parser and the driver’s use of it look like this: . . . . . . p ar se r. h pa rs er .h l ex er .h le xe r. h e rr or .h er ro r. h t ab le .h ta bl e. h . . . pa rs er _i mp l. h p ar se r_ im pl .h . . . . d ri ve r. c dr iv er .c p ar se r. c pa rs er .c As intended, this is a rather close match to the logical structure described in §8.3.3. To simplify in cl ud ed ta bl e.h pa rs er _i mp l.h this structure, we could have #i nc lu de t ab le h in p ar se r_ im pl h rather than in p ar se r.c How- pa rs er c. ever, t ab le h is an example of something that is not necessary to express the shared context of the ta bl e.h parser functions; it is needed only by their implementation. In fact, it is used by just one function, e xp r(), so if we were really keen on minimizing dependencies we could place e xp r() in its own ex pr ex pr .c file and #i nc lu de t ab le h there only: c in cl ud e ta bl e.h . . . . . . p ar se r. h pa rs er .h l ex er .h le xe r. h e rr or .h er ro r. h t ab le .h ta bl e. h . . . pa rs er _i mp l. h p ar se r_ im pl .h . . . p ar se r. c pa rs er .c e xp r. c ex pr .c Such elaboration is not appropriate except for larger modules. For realistically-sized modules, it is common to #i nc lu de extra files where needed for individual functions. Furthermore, it is not in cl ud e uncommon to have more than one _ im pl h, since different subsets of the module’s functions need _i mp l.h different shared contexts. Please note that the _ im pl h notation is not a standard or even a common convention; it is sim- _i mp l.h ply the way I like to name things. Why bother with this more complicated scheme of multiple header files? It clearly requires far less thought simply to throw every declaration into a single header, as was done for d c.h dc h. The multiple-header organization scales to modules several magnitudes larger than our toy parser and to programs several magnitudes larger than our calculator. The fundamental reason for using this type of organization is that it provides a better localization of concerns. When analyzing
    223. Section 9.3.2 Multiple Header Files 213 and modifying a large program, it is essential for a programmer to focus on a relatively small chunk of code. The multiple-header organization makes it easy to determine exactly what the parser code depends on and to ignore the rest of the program. The single-header approach forces us to look at every declaration used by any module and decide if it is relevant. The simple fact is that mainte- nance of code is invariably done with incomplete information and from a local perspective. The multiple-header organization allows us to work successfully ‘‘from the inside out’’ with only a local perspective. The single-header approach – like every other organization centered around a global repository of information – requires a top-down approach and will forever leave us wonder- ing exactly what depends on what. The better localization leads to less information needed to compile a module, and thus to faster compiles. The effect can be dramatic. I have seen compile times drop by a factor of ten as the result of a simple dependency analysis leading to a better use of headers. 9.3.2.1 Other Calculator Modules [file.multi.etc] The remaining calculator modules can be organized similarly to the parser. However, those mod- ules are so small that they don’t require their own _ im pl h files. Such files are needed only where _i mp l.h a logical module consists of many functions that need a shared context. The error handler was reduced to the set of exception types so that no e rr or c was needed: er ro r.c // error.h: n am es pa ce E rr or { na me sp ac e Er ro r st ru ct Ze ro _d iv id e s tr uc t Z er o_ di vi de { }; st ru ct Sy nt ax _e rr or s tr uc t S yn ta x_ er ro r { c on st c ha r* p co ns t ch ar p; Sy nt ax _e rr or co ns t ch ar q) S yn ta x_ er ro r(c on st c ha r* q { p = q } q; }; } The lexer provides a rather large and messy interface: // lexer.h: #i nc lu de <s tr in g> in cl ud e st ri ng n am es pa ce L ex er { na me sp ac e Le xe r e nu m T ok en _v al ue { en um To ke n_ va lu e N AM E, NA ME N UM BE R, NU MB ER E ND EN D, P LU S=´+´, PL US M IN US MI NU S=´-´, M UL MU L=´*´, D IV DI V=´/´, P RI NT PR IN T=´;´, A SS IG N=´=´, L P=´(´, AS SI GN LP R P=´)´ RP }; e xt er n T ok en _v al ue c ur r_ to k; ex te rn To ke n_ va lu e cu rr _t ok ex te rn do ub le nu mb er _v al ue e xt er n d ou bl e n um be r_ va lu e; ex te rn st d: st ri ng st ri ng _v al ue e xt er n s td :s tr in g s tr in g_ va lu e; T ok en _v al ue g et _t ok en ; To ke n_ va lu e ge t_ to ke n() }
    224. 214 Source Files and Programs Chapter 9 In addition to l ex er h, the implementation of the lexer depends on e rr or h, <i os tr ea m>, and the le xe r.h er ro r.h io st re am functions determining the kinds of characters declared in <c ct yp e>: cc ty pe // lexer.c: #i nc lu de \"l ex er h\" in cl ud e le xe r.h #i nc lu de \"e rr or h\" in cl ud e er ro r.h #i nc lu de <i os tr ea m> in cl ud e io st re am #i nc lu de <c ct yp e> in cl ud e cc ty pe L ex er :T ok en _v al ue L ex er :c ur r_ to k; Le xe r: To ke n_ va lu e Le xe r: cu rr _t ok do ub le Le xe r: nu mb er _v al ue d ou bl e L ex er :n um be r_ va lu e; st d: st ri ng Le xe r: st ri ng _v al ue s td :s tr in g L ex er :s tr in g_ va lu e; L ex er :T ok en _v al ue L ex er :g et _t ok en Le xe r: To ke n_ va lu e Le xe r: ge t_ to ke n() { /* ... */ } We could have factored out the #i nc lu de statements for e rr or h as the L ex er _ im pl h file. Le xe r’s _i mp l.h in cl ud e er ro r.h However, I considered that excessive for this tiny program. As usual, we #i nc lu de the interface offered by the module – in this case, l ex er h – in the in cl ud e le xe r.h module’s implementation to give the compiler a chance to check consistency. The symbol table is essentially self-contained, although the standard library header <m ap ma p> could drag in all kinds of interesting stuff to implement an efficient m ap template class: ma p // table.h: #i nc lu de <m ap in cl ud e ma p> #i nc lu de <s tr in g> in cl ud e st ri ng e xt er n s td :m ap st d::s tr in g,d ou bl e> t ab le ex te rn st d: ma p<s td st ri ng do ub le ta bl e; Because we assume that every header may be #i nc lu de in several .c files, we must separate the in cl ud ed c declaration of t ab le from its definition, even though the difference between t ab le c and t ab le h is ta bl e ta bl e.c ta bl e.h the single keyword e xt er n: ex te rn // table.c: #i nc lu de \"t ab le h\" in cl ud e ta bl e.h s td :m ap st d::s tr in g,d ou bl e> t ab le st d: ma p<s td st ri ng do ub le ta bl e; Basically, the driver depends on everything: // main.c: #i nc lu de \"p ar se r.h in cl ud e pa rs er h\" #i nc lu de \"l ex er h\" in cl ud e le xe r.h #i nc lu de \"e rr or h\" in cl ud e er ro r.h #i nc lu de \"t ab le h\" in cl ud e ta bl e.h n am es pa ce D ri ve r { na me sp ac e Dr iv er i nt n o_ of _e rr or s; in t no _o f_ er ro rs s td :i st re am i np ut st d: is tr ea m* in pu t; v oi d s ki p(); vo id sk ip }
    225. Section 9.3.2.1 Other Calculator Modules 215 #i nc lu de <s st re am in cl ud e ss tr ea m> i nt m ai n(i nt a rg c, c ha r* a rg v[]) { /* ... */ } in t ma in in t ar gc ch ar ar gv Because the D ri ve r namespace is used exclusively by m ai n(), I placed it in m ai n.c Alterna- Dr iv er ma in ma in c. tively, I could have factored it out as d ri ve r.h and #i nc lu de it. dr iv er h in cl ud ed For a larger system, it is usually worthwhile organizing things so that the driver has fewer direct dependencies. Often, is it also worth minimizing what is done in m ai n() by having m ai n() call a ma in ma in driver function placed in a separate source file. This is particularly important for code intended to be used as a library. Then, we cannot rely on code in m ai n() and must be prepared to be called ma in from a variety of functions (§9.6[8]). 9.3.2.2 Use of Headers [file.multi.use] The number of headers to use for a program is a function of many factors. Many of these factors have more to do with the way files are handled on your system than with C++. For example, if your editor does not have facilities for looking at several files at the same time, then using many headers becomes less attractive. Similarly, if opening and reading 20 files of 50 lines each is noticeably more time-consuming than reading a single file of 1000 lines, you might think twice before using the multiple-header style for a small project. A word of caution: a dozen headers plus the standard headers for the program’s execution envi- ronment (which can often be counted in the hundreds) are usually manageable. However, if you partition the declarations of a large program into the logically minimal-sized headers (putting each structure declaration in its own file, etc.), you can easily get an unmanageable mess of hundreds of files even for minor projects. I find that excessive. For large projects, multiple headers are unavoidable. In such projects, hundreds of files (not counting standard headers) are the norm. The real confusion starts when they start to be counted in the thousands. At that scale, the basic techniques discussed here still apply, but their management becomes a Herculean task. Remember that for realistically-sized programs, the single-header style is not an option. Such programs will have multiple headers. The choice between the two styles of organization occurs (repeatedly) for the parts that make up the program. The single-header style and the multiple-header style are not really alternatives to each other. They are complementary techniques that must be considered whenever a significant module is designed and must be reconsidered as a system evolves. It’s crucial to remember that one interface doesn’t serve all equally well. It is usually worthwhile to distinguish between the implementers’ interface and the users’ interface. In addition, many larger systems are structured so that providing a simple interface for the majority of users and a more extensive interface for expert users is a good idea. The expert users’ interfaces (‘‘complete interfaces’’) tend to #i nc lu de many more features in cl ud e than the average user would ever want to know about. In fact, the average users’ interface can often be identified by eliminating features that require the inclusion of headers that define facilities that would be unknown to the average user. The term ‘‘average user’’ is not derogatory. In the fields in which I don’t have to be an expert, I strongly prefer to be an average user. In that way, I minimize hassles.
    226. 216 Source Files and Programs Chapter 9 9.3.3 Include Guards [file.guards] The idea of the multiple-header approach is to represent each logical module as a consistent, self- contained unit. Viewed from the program as a whole, many of the declarations needed to make each logical module complete are redundant. For larger programs, such redundancy can lead to errors, as a header containing class definitions or inline functions gets #i nc lu de twice in the same in cl ud ed compilation unit (§9.2.3). We have two choices. We can [1] reorganize our program to remove the redundancy, or [2] find a way to allow repeated inclusion of headers. The first approach – which led to the final version of the calculator – is tedious and impractical for realistically-sized programs. We also need that redundancy to make the individual parts of the pro- gram comprehensible in isolation. The benefits of an analysis of redundant #i nc lu de and the resulting simplifications of the pro- in cl ud es gram can be significant both from a logical point of view and by reducing compile times. How- ever, it can rarely be complete, so some method of allowing redundant #i nc lu de must be applied. in cl ud es Preferably, it must be applied systematically, since there is no way of knowing how thorough an analysis a user will find worthwhile. The traditional solution is to insert include guards in headers. For example: // error.h: if nd ef CA LC _E RR OR _H #i fn de f C AL C_ ER RO R_ H de fi ne CA LC _E RR OR _H #d ef in e C AL C_ ER RO R_ H n am es pa ce E rr or { na me sp ac e Er ro r // ... } #e nd if en di f // CALC_ERROR_H The contents of the file between the #i fn de f and #e nd if are ignored by the compiler if if nd ef en di f CA LC _E RR OR _H C AL C_ ER RO R_ H is defined. Thus, the first time e rr or h is seen during a compilation, its con- er ro r.h CA LC _E RR OR _H tents are read and C AL C_ ER RO R_ H is given a value. Should the compiler be presented with e rr or h again during the compilation, the contents are ignored. This is a piece of macro hackery, er ro r.h but it works and it is pervasive in the C and C++ worlds. The standard headers all have include guards. Header files are included in essentially arbitrary contexts, and there is no namespace protection against macro name clashes. Consequently, I choose rather long and ugly names as my include guards. Once people get used to headers and include guards, they tend to include lots of headers directly and indirectly. Even with C++ implementations that optimize the processing of headers, this can be undesirable. It can cause unnecessarily long compile time, and it can bring l ot s of declarations and lo ts macros into scope. The latter might affect the meaning of the program in unpredictable and adverse ways. Headers should be included only when necessary.
    227. Section 9.4 Programs 217 9.4 Programs [file.programs] A program is a collection of separately compiled units combined by a linker. Every function, object, type, etc., used in this collection must have a unique definition (§4.9, §9.2.3). The program must contain exactly one function called m ai n() (§3.2). The main computation performed by the ma in program starts with the invocation of m ai n() and ends with a return from m ai n(). The i nt ma in ma in in t returned by m ai n() is passed to whatever system invoked m ai n() as the result of the program. ma in ma in This simple story must be elaborated on for programs that contain global variables (§10.4.9) or that throw an uncaught exception (§14.7). 9.4.1 Initialization of Nonlocal Variables [file.nonlocal] In principle, a variable defined outside any function (that is, global, namespace, and class s ta ti c st at ic variables) is initialized before m ai n() is invoked. Such nonlocal variables in a translation unit are ma in initialized in their declaration order (§10.4.9). If such a variable has no explicit initializer, it is by default initialized to the default for its type (§10.4.2). The default initializer value for built-in types and enumerations is 0 For example: 0. d ou bl e x = 2 do ub le 2; // nonlocal variables d ou bl e y do ub le y; d ou bl e s qx = s qr t(x y); do ub le sq x sq rt x+y Here, x and y are initialized before s qx so s qr t(2 is called. sq x, sq rt 2) There is no guaranteed order of initialization of global variables in different translation units. Consequently, it is unwise to create order dependencies between initializers of global variables in different compilation units. In addition, it is not possible to catch an exception thrown by the ini- tializer of a global variable (§14.7). It is generally best to minimize the use of global variables and in particular to limit the use of global variables requiring complicated initialization. Several techniques exist for enforcing an order of initialization of global variables in different translation units. However, none are both portable and efficient. In particular, dynamically linked libraries do not coexist happily with global variables that have complicated dependencies. Often, a function returning a reference is a good alternative to a global variable. For example: i nt u se _c ou nt in t& us e_ co un t() { s ta ti c i nt u c = 0 st at ic in t uc 0; r et ur n u c; re tu rn uc } A call u se _c ou nt us e_ co un t() now acts as a global variable except that it is initialized at its first use (§5.5). For example: v oi d f vo id f() { c ou t << ++u se _c ou nt ; co ut us e_ co un t() // read and increment // ... } The initialization of nonlocal static variables is controlled by whatever mechanism an
    228. 218 Source Files and Programs Chapter 9 implementation uses to start up a C++ program. This mechanism is guaranteed to work properly only if m ai n() is executed. Consequently, one should avoid nonlocal variables that require run- ma in time initialization in C++ code intended for execution as a fragment of a non-C++ program. Note that variables initialized by constant expressions (§C.5) cannot depend on the value of objects from other translation units and do not require run-time initialization. Such variables are therefore safe to use in all cases. 9.4.1.1 Program Termination [file.termination] A program can terminate in several ways: – By returning from m ai n() ma in – By calling e xi t() ex it – By calling a bo rt ab or t() – By throwing an uncaught exception In addition, there are a variety of ill-behaved and implementation-dependent ways of making a pro- gram crash. If a program is terminated using the standard library function e xi t(), the destructors for con- ex it structed static objects are called (§10.4.9, §10.2.4). However, if the program is terminated using the standard library function a bo rt ab or t(), they are not. Note that this implies that e xi t() does not ter- ex it minate a program immediately. Calling e xi t() in a destructor may cause an infinite recursion. The ex it type of e xi t() is ex it v oi d e xi t(i nt ; vo id ex it in t) Like the return value of m ai n() (§3.2), e xi t()’s argument is returned to ‘‘the system’’ as the value ma in ex it of the program. Zero indicates successful completion. Calling e xi t() means that the local variables of the calling function and its callers will not have ex it their destructors invoked. Throwing an exception and catching it ensures that local objects are properly destroyed (§14.4.7). Also, a call of e xi t() terminates the program without giving the ex it caller of the function that called e xi t() a chance to deal with the problem. It is therefore often best ex it to leave a context by throwing an exception and letting a handler decide what to do next. The C (and C++) standard library function a te xi t() offers the possibility to have code executed at ex it at program termination. For example: vo id my _c le an up v oi d m y_ cl ea nu p(); v oi d s om ew he re vo id so me wh er e() { if at ex it my _c le an up i f (a te xi t(&m y_ cl ea nu p)==0 { 0) // my_cleanup will be called at normal termination } e ls e { el se // oops: too many atexit functions } } This strongly resembles the automatic invocation of destructors for global variables at program ter- mination (§10.4.9, §10.2.4). Note that an argument to a te xi t() cannot take arguments or return a at ex it
    229. Section 9.4.1.1 Program Termination 219 result. Also, there is an implementation-defined limit to the number of atexit functions; a te xi t() at ex it indicates when that limit is reached by returning a nonzero value. These limitations make a te xi t() at ex it less useful than it appears at first glance. The destructor of an object created before a call of a te xi t(f will be invoked after f is invoked. at ex it f) The destructor of an object created after a call of a te xi t(f will be invoked before f is invoked. at ex it f) The e xi t(), a bo rt ex it ab or t(), and a te xi t() functions are declared in <c st dl ib at ex it cs td li b>. 9.5 Advice [file.advice] [1] Use header files to represent interfaces and to emphasize logical structure; §9.1, §9.3.2. [2] #i nc lu de a header in the source file that implements its functions; §9.3.1. in cl ud e [3] Don’t define global entities with the same name and similar-but-different meanings in differ- ent translation units; §9.2. [4] Avoid non-inline function definitions in headers; §9.2.1. [5] Use #i nc lu de only at global scope and in namespaces; §9.2.1. in cl ud e [6] #i nc lu de only complete declarations; §9.2.1. in cl ud e [7] Use include guards; §9.3.3. [8] #i nc lu de C headers in namespaces to avoid global names; §9.3.2. in cl ud e [9] Make headers self-contained; §9.2.3. [10] Distinguish between users’ interfaces and implementers’ interfaces; §9.3.2. [11] Distinguish between average users’ interfaces and expert users’ interfaces; §9.3.2. [12] Avoid nonlocal objects that require run-time initialization in code intended for use as part of non-C++ programs; §9.4.1. 9.6 Exercises [file.exercises] 1. (∗2) Find where the standard library headers are kept on your system. List their names. Are any nonstandard headers kept together with the standard ones? Can any nonstandard headers be #i nc lu de using the <> notation? in cl ud ed 2. (∗2) Where are the headers for nonstandard library ‘‘foundation’’ libraries kept? 3. (∗2.5) Write a program that reads a source file and writes out the names of files #i nc lu de in cl ud ed. Indent file names to show files #i nc lu de d by included files. Try this program on some real in cl ud ed source files (to get an idea of the amount of information included). 4. (∗3) Modify the program from the previous exercise to print the number of comment lines, the number of non-comment lines, and the number of non-comment, whitespace-separated words for each file #i nc lu de in cl ud ed. 5. (∗2.5) An external include guard is a construct that tests outside the file it is guarding and i nc lu de only once per compilation. Define such a construct, devise a way of testing it, and dis- in cl ud es cuss its advantages and disadvantages compared to the include guards described in §9.3.3. Is there any significant run-time advantage to external include guards on your system. 6. (∗3) How is dynamic linking achieved on your system. What restrictions are placed on dynami- cally linked code? What requirements are placed on code for it to be dynamically linked?
    230. 220 Source Files and Programs Chapter 9 7. (∗3) Open and read 100 files containing 1500 characters each. Open and read one file contain- ing 150,000 characters. Hint: See example in §21.5.1. Is there a performance difference? What is the highest number of files that can be simultaneously open on your system? Consider these questions in relation to the use of #i nc lu de files. in cl ud e 8. (∗2) Modify the desk calculator so that it can be invoked from m ai n() or from other functions ma in as a simple function call. 9. (∗2) Draw the ‘‘module dependency diagrams’’ (§9.3.2) for the version of the calculator that used e rr or instead of exceptions (§8.2.2). er ro r()
    231. Part II Abstraction Mechanisms This part describes C++’s facilities for defining and using new types. Techniques com- monly called object-oriented programming and generic programming are presented. Chapters 10 Classes 11 Operator Overloading 12 Derived Classes 13 Templates 14 Exception Handling 15 Class Hierarchies
    232. 222 Abstraction Mechanisms Part II ‘‘... there is nothing more difficult to carry out, nor more doubtful of success, nor more dangerous to handle, than to initiate a new order of things. For the reformer makes enemies of all those who profit by the old order, and only lukewarm defenders in all those who would profit by the new order...’’ — Nicollo Machiavelli (‘‘The Prince’’ §vi)
    233. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 10 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Classes Those types are not \"abstract\"; they are as real as int and float. – Doug McIlroy Concepts and classes — class members — access control — constructors — s ta ti c st at ic members — default copy — c on st member functions — t hi s — s tr uc ts — in-class func- co ns t th is st ru ct tion definition — concrete classes — member functions and helper functions — over- loaded operators — use of concrete classes — destructors — default construction — local variables — user-defined copy — n ew and d el et e — member objects — arrays — ne w de le te static storage — temporary variables — unions — advice — exercises. 10.1 Introduction [class.intro] The aim of the C++ class concept is to provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types. In addition, derived classes (Chapter 12) and templates (Chapter 13) provide ways of organizing related classes that allow the programmer to take advantage of their relationships. A type is a concrete representation of a concept. For example, the C++ built-in type f lo at with fl oa t its operations +, -, *, etc., provides a concrete approximation of the mathematical concept of a real number. A class is a user-defined type. We design a new type to provide a definition of a concept that has no direct counterpart among the built-in types. For example, we might provide a type T ru nk _l in e in a program dealing with telephony, a type E xp lo si on for a videogame, or a type Tr un k_ li ne Ex pl os io n l is t<P ar ag ra ph for a text-processing program. A program that provides types that closely match li st Pa ra gr ap h> the concepts of the application tends to be easier to understand and easier to modify than a program that does not. A well-chosen set of user-defined types makes a program more concise. In addition, it makes many sorts of code analysis feasible. In particular, it enables the compiler to detect illegal uses of objects that would otherwise remain undetected until the program is thoroughly tested.
    234. 224 Classes Chapter 10 The fundamental idea in defining a new type is to separate the incidental details of the imple- mentation (e.g., the layout of the data used to store an object of the type) from the properties essen- tial to the correct use of it (e.g., the complete list of functions that can access the data). Such a sep- aration is best expressed by channeling all uses of the data structure and internal housekeeping rou- tines through a specific interface. This chapter focuses on relatively simple ‘‘concrete’’ user-defined types that logically don’t dif- fer much from built-in types. Ideally, such types should not differ from built-in types in the way they are used, only in the way they are created. 10.2 Classes [class.class] A class is a user-defined type. This section introduces the basic facilities for defining a class, creat- ing objects of a class, and manipulating such objects. 10.2.1 Member Functions [class.member] Consider implementing the concept of a date using a s tr uc t to define the representation of a D at e st ru ct Da te and a set of functions for manipulating variables of this type: s tr uc t D at e { st ru ct Da te // representation i nt d m y in t d, m, y; }; in it _d at e(D at e& d, in t, in t, in t) v oi d vo id i ni t_ da te Da te d i nt i nt i nt ; // initialize d a dd _y ea r(D at e& d i nt n ; v oi d vo id ad d_ ye ar Da te d, in t n) // add n years to d a dd _m on th Da te d i nt n ; v oi d vo id ad d_ mo nt h(D at e& d, in t n) // add n months to d a dd _d ay Da te d i nt n ; v oi d vo id ad d_ da y(D at e& d, in t n) // add n days to d There is no explicit connection between the data type and these functions. Such a connection can be established by declaring the functions as members: s tr uc t D at e { st ru ct Da te i nt d m y in t d, m, y; v oi d vo id i ni t(i nt d d, i nt m m, i nt y y); in it in t dd in t mm in t yy // initialize a dd _y ea r(i nt n ; v oi d vo id ad d_ ye ar in t n) // add n years a dd _m on th in t n ; v oi d vo id ad d_ mo nt h(i nt n) // add n months a dd _d ay in t n ; v oi d vo id ad d_ da y(i nt n) // add n days }; Functions declared within a class definition (a s tr uc t is a kind of class; §10.2.8) are called member st ru ct functions and can be invoked only for a specific variable of the appropriate type using the standard syntax for structure member access. For example: Da te my _b ir th da y; D at e m y_ bi rt hd ay v oi d f vo id f() { D at e t od ay Da te to da y;
    235. Section 10.2.1 Member Functions 225 t od ay in it 16 10 19 96 ; to da y.i ni t(1 6,1 0,1 99 6) my _b ir th da y.i ni t(3 0,1 2,1 95 0) m y_ bi rt hd ay in it 30 12 19 50 ; D at e t om or ro w = t od ay Da te to mo rr ow to da y; t om or ro w.a dd _d ay 1); to mo rr ow ad d_ da y(1 // ... } Because different structures can have member functions with the same name, we must specify the structure name when defining a member function: v oi d D at e::i ni t(i nt d d, i nt m m, i nt y y) vo id Da te in it in t dd in t mm in t yy { d = d d; dd m = m m; mm y = y y; yy } In a member function, member names can be used without explicit reference to an object. In that case, the name refers to that member of the object for which the function was invoked. For exam- ple, when D at e::i ni t() is invoked for t od ay m mm assigns to t od ay m. On the other hand, Da te in it to da y, m=m m to da y.m my _b ir th da y, m=m m my _b ir th da y.m when D at e::i ni t() is invoked for m y_ bi rt hd ay m mm assigns to m y_ bi rt hd ay m. A class Da te in it member function always ‘‘knows’’ for which object it was invoked. The construct c la ss X { ... }; cl as s is called a class definition because it defines a new type. For historical reasons, a class definition is often referred to as a class declaration. Also, like declarations that are not definitions, a class defi- nition can be replicated in different source files using #i nc lu de without violating the one-definition in cl ud e rule (§9.2.3). 10.2.2 Access Control [class.access] The declaration of D at e in the previous subsection provides a set of functions for manipulating a Da te D at e. However, it does not specify that those functions should be the only ones to depend directly Da te on D at e’s representation and the only ones to directly access objects of class D at e. This restriction Da te Da te can be expressed by using a c la ss instead of a s tr uc t: cl as s st ru ct c la ss D at e { cl as s Da te i nt d m y in t d, m, y; p ub li c: pu bl ic v oi d i ni t(i nt d d, i nt m m, i nt y y); vo id in it in t dd in t mm in t yy // initialize v oi d a dd _y ea r(i nt n ; vo id ad d_ ye ar in t n) // add n years v oi d a dd _m on th in t n ; vo id ad d_ mo nt h(i nt n) // add n months v oi d a dd _d ay in t n ; vo id ad d_ da y(i nt n) // add n days }; The p ub li c label separates the class body into two parts. The names in the first, private, part can be pu bl ic used only by member functions. The second, public, part constitutes the public interface to objects
    236. 226 Classes Chapter 10 of the class. A s tr uc t is simply a c la ss whose members are public by default (§10.2.8); member st ru ct cl as s functions can be defined and used exactly as before. For example: i nl in e v oi d D at e::a dd _y ea r(i nt n in li ne vo id Da te ad d_ ye ar in t n) { y += n n; } However, nonmember functions are barred from using private members. For example: v oi d t im ew ar p(D at e& d vo id ti me wa rp Da te d) { d y -= 2 00 d.y 20 0; // error: Date::y is private } There are several benefits to be obtained from restricting access to a data structure to an explicitly declared list of functions. For example, any error causing a D at e to take on an illegal value (for Da te example, December 36, 1985) must be caused by code in a member function. This implies that the first stage of debugging – localization – is completed before the program is even run. This is a special case of the general observation that any change to the behavior of the type D at e can and Da te must be effected by changes to its members. In particular, if we change the representation of a class, we need only change the member functions to take advantage of the new representation. User code directly depends only on the public interface and need not be rewritten (although it may need to be recompiled). Another advantage is that a potential user need examine only the definition of the member functions in order to learn to use a class. The protection of private data relies on restriction of the use of the class member names. It can therefore be circumvented by address manipulation and explicit type conversion. But this, of course, is cheating. C++ protects against accident rather than deliberate circumvention (fraud). Only hardware can protect against malicious use of a general-purpose language, and even that is hard to do in realistic systems. The i ni t() function was added partially because it is generally useful to have a function that in it sets the value of an object and partly because making the data private forces us to provide it. 10.2.3 Constructors [class.ctor] The use of functions such as i ni t() to provide initialization for class objects is inelegant and error- in it prone. Because it is nowhere stated that an object must be initialized, a programmer can forget to do so – or do so twice (often with equally disastrous results). A better approach is to allow the pro- grammer to declare a function with the explicit purpose of initializing objects. Because such a function constructs values of a given type, it is called a constructor. A constructor is recognized by having the same name as the class itself. For example: c la ss D at e { cl as s Da te // ... D at e(i nt i nt i nt ; Da te in t, in t, in t) // constructor }; When a class has a constructor, all objects of that class will be initialized. If the constructor requires arguments, these arguments must be supplied:
    237. Section 10.2.3 Constructors 227 D at e Da te t od ay = D at e(2 3,6 19 83 ; to da y Da te 23 6,1 98 3) D at e Da te x ma s(2 5,1 2,1 99 0); xm as 25 12 19 90 // abbreviated form my _b ir th da y; D at e Da te m y_ bi rt hd ay // error: initializer missing re le as e1 _0 10 12 D at e Da te r el ea se 1_ 0(1 0,1 2); // error: 3rd argument missing It is often nice to provide several ways of initializing a class object. This can be done by providing several constructors. For example: c la ss D at e { cl as s Da te i nt d m y in t d, m, y; p ub li c: pu bl ic // ... D at e(i nt i nt i nt ; Da te in t, in t, in t) // day, month, year D at e(i nt i nt ; Da te in t, in t) // day, month, today’s year D at e(i nt ; Da te in t) // day, today’s month and year D at e(); Da te // default Date: today D at e(c on st c ha r*); Da te co ns t ch ar // date in string representation }; Constructors obey the same overloading rules as do other functions (§7.4). As long as the construc- tors differ sufficiently in their argument types, the compiler can select the correct one for each use: D at e Da te t od ay 4); to da y(4 D at e Da te j ul y4 Ju ly 4 1 98 3\"); ju ly 4(\"J ul y 4, 19 83 D at e Da te g uy 5 N ov ; gu y(\"5 No v\") D at e Da te n ow no w; // default initialized as today The proliferation of constructors in the D at e example is typical. When designing a class, a pro- Da te grammer is always tempted to add features just because somebody might want them. It takes more thought to carefully decide what features are really needed and to include only those. However, that extra thought typically leads to smaller and more comprehensible programs. One way of reducing the number of related functions is to use default arguments (§7.5). In the D at e, each argu- Da te ment can be given a default value interpreted as ‘‘pick the default: t od ay to da y.’’ c la ss D at e { cl as s Da te i nt d m y in t d, m, y; p ub li c: pu bl ic D at e(i nt d d =0 i nt m m =0 i nt y y =0 ; Da te in t dd 0, in t mm 0, in t yy 0) // ... }; D at e::D at e(i nt d d, i nt m m, i nt y y) Da te Da te in t dd in t mm in t yy { d = d d ? d d : t od ay d; dd dd to da y.d m = m m ? m m : t od ay m; mm mm to da y.m y = y y ? y y : t od ay y; yy yy to da y.y // check that the Date is valid } When an argument value is used to indicate ‘‘pick the default,’’ the value chosen must be outside the set of possible values for the argument. For d ay and m on th this is clearly so, but for y ea r, zero da y mo nt h, ye ar
    238. 228 Classes Chapter 10 may not be an obvious choice. Fortunately, there is no year zero on the European calendar; 1AD (y ea r==1 comes immediately after 1BC (y ea r==-1 ye ar 1) ye ar 1). 10.2.4 Static Members [class.static] The convenience of a default value for D at es was bought at the cost of a significant hidden prob- Da te lem. Our D at e class became dependent on the global variable t od ay This D at e class can be used Da te to da y. Da te only in a context in which t od ay is defined and correctly used by every piece of code. This is the to da y kind of constraint that causes a class to be useless outside the context in which it was first written. Users get too many unpleasant surprises trying to use such context-dependent classes, and mainte- nance becomes messy. Maybe ‘‘just one little global variable’’ isn’t too unmanageable, but that style leads to code that is useless except to its original programmer. It should be avoided. Fortunately, we can get the convenience without the encumbrance of a publicly accessible glo- bal variable. A variable that is part of a class, yet is not part of an object of that class, is called a s ta ti c member. There is exactly one copy of a s ta ti c member instead of one copy per object, as for st at ic st at ic ordinary non-s ta ti c members. Similarly, a function that needs access to members of a class, yet st at ic doesn’t need to be invoked for a particular object, is called a s ta ti c member function. st at ic Here is a redesign that preserves the semantics of default constructor values for D at e without Da te the problems stemming from reliance on a global: c la ss D at e { cl as s Da te i nt d m y in t d, m, y; s ta ti c D at e d ef au lt _d at e; st at ic Da te de fa ul t_ da te p ub li c: pu bl ic D at e(i nt d d =0 i nt m m =0 i nt y y =0 ; Da te in t dd 0, in t mm 0, in t yy 0) // ... s ta ti c v oi d s et _d ef au lt in t, i nt i nt ; st at ic vo id se t_ de fa ul t(i nt in t, in t) }; We can now define the D at e constructor like this: Da te D at e::D at e(i nt d d, i nt m m, i nt y y) Da te Da te in t dd in t mm in t yy { d = d d ? d d : d ef au lt _d at e.d dd dd de fa ul t_ da te d; m = m m ? m m : d ef au lt _d at e.m mm mm de fa ul t_ da te m; y = y y ? y y : d ef au lt _d at e.y yy yy de fa ul t_ da te y; // check that the Date is valid } We can change the default date when appropriate. A static member can be referred to like any other member. In addition, a static member can be referred to without mentioning an object. Instead, its name is qualified by the name of its class. For example: v oi d f vo id f() { D at e::s et _d ef au lt 4,5 19 45 ; Da te se t_ de fa ul t(4 5,1 94 5) } Static members – both function and data members – must be defined somewhere. For example:
    239. Section 10.2.4 Static Members 229 D at e D at e::d ef au lt _d at e(1 6,1 2,1 77 0); Da te Da te de fa ul t_ da te 16 12 17 70 v oi d D at e::s et _d ef au lt in t d i nt m i nt y vo id Da te se t_ de fa ul t(i nt d, in t m, in t y) { D at e::d ef au lt _d at e = D at e(d m,y ; Da te de fa ul t_ da te Da te d,m y) } Now the default value is Beethoven’s birth date – until someone decides otherwise. Note that D at e() serves as a notation for the value of D at e::d ef au lt _d at e. For example: Da te Da te de fa ul t_ da te D at e c op y_ of _d ef au lt _d at e = D at e(); Da te co py _o f_ de fa ul t_ da te Da te Consequently, we don’t need a separate function for reading the default date. 10.2.5 Copying Class Objects [class.default.copy] By default, class objects can be copied. In particular, a class object can be initialized with a copy of another object of the same class. This can be done even where constructors have been declared. For example: D at e d = t od ay Da te to da y; // initialization by copy By default, the copy of a class object is a copy of each member. If that default is not the behavior wanted for a class X a more appropriate behavior can be provided by defining a copy constructor, X, X :X co ns t X X: X(c on st X&). This is discussed further in §10.4.4.1. Similarly, class objects can by default be copied by assignment. For example: v oi d f Da te d vo id f(D at e& d) { d = t od ay to da y; } Again, the default semantics is memberwise copy. If that is not the right choice for a class X the X, user can define an appropriate assignment operator (§10.4.4.1). 10.2.6 Constant Member Functions [class.constmem] The D at e defined so far provides member functions for giving a D at e a value and changing it. Da te Da te Unfortunately, we didn’t provide a way of examining the value of a D at e. This problem can easily Da te be remedied by adding functions for reading the day, month, and year: c la ss D at e { cl as s Da te i nt d m y in t d, m, y; p ub li c: pu bl ic i nt d ay in t da y() c on st { r et ur n d } co ns t re tu rn d; i nt m on th in t mo nt h() c on st { r et ur n m } co ns t re tu rn m; i nt y ea r() c on st in t ye ar co ns t; // ... }; Note the c on st after the (empty) argument list in the function declarations. It indicates that these co ns t functions do not modify the state of a D at e. Da te
    240. 230 Classes Chapter 10 Naturally, the compiler will catch accidental attempts to violate this promise. For example: i nl in e i nt D at e::y ea r() c on st in li ne in t Da te ye ar co ns t { r et ur n y re tu rn y++; // error: attempt to change member value in const function } When a c on st member function is defined outside its class, the c on st suffix is required: co ns t co ns t i nl in e i nt D at e::y ea r() c on st in li ne in t Da te ye ar co ns t // correct { r et ur n y re tu rn y; } i nl in e i nt D at e::y ea r() in li ne in t Da te ye ar // error: const missing in member function type { r et ur n y re tu rn y; } In other words, the c on st is part of the type of D at e::d ay and D at e::y ea r(). co ns t Da te da y() Da te ye ar A c on st member function can be invoked for both c on st and non-c on st objects, whereas a non- co ns t co ns t co ns t c on st member function can be invoked only for non-c on st objects. For example: co ns t co ns t v oi d f Da te d c on st D at e& c d) vo id f(D at e& d, co ns t Da te cd { i nt i = d ye ar ; in t d.y ea r() // ok d.a dd _y ea r(1 d ad d_ ye ar 1); // ok i nt j = c d.y ea r(); in t cd ye ar // ok c d.a dd _y ea r(1 ; cd ad d_ ye ar 1) // error: cannot change value of const cd } 10.2.7 Self-Reference [class.this] The state update functions a dd _y ea r(), a dd _m on th ad d_ mo nt h(), and a dd _d ay were defined not to return ad d_ ye ar ad d_ da y() values. For such a set of related update functions, it is often useful to return a reference to the updated object so that the operations can be chained. For example, we would like to write v oi d f Da te d vo id f(D at e& d) { // ... d.a dd _d ay 1).a dd _m on th 1).a dd _y ea r(1 d ad d_ da y(1 ad d_ mo nt h(1 ad d_ ye ar 1); // ... } to add a day, a month, and a year to d To do this, each function must be declared to return a refer- d. ence to a D at e: Da te c la ss D at e { cl as s Da te // ...
    241. Section 10.2.7 Self-Reference 231 D at e& a dd _y ea r(i nt n ; // add n years Da te ad d_ ye ar in t n) D at e& a dd _m on th in t n ; // add n months Da te ad d_ mo nt h(i nt n) D at e& a dd _d ay in t n ; Da te ad d_ da y(i nt n) // add n days }; Each (nonstatic) member function knows what object it was invoked for and can explictly refer to it. For example: D at e& D at e::a dd _y ea r(i nt n Da te Da te ad d_ ye ar in t n) { i f (d 29 && m 2 && !l ea py ea r(y n)) { // beware of February 29 if d==2 9 m==2 le ap ye ar y+n d=1 1; m=3 3; } y += n n; r et ur n *t hi s; re tu rn th is } The expression *t hi s refers to the object for which a member function is invoked. It is equivalent th is to Simula’s T HI S and Smalltalk’s s el f. TH IS se lf In a nonstatic member function, the keyword t hi s is a pointer to the object for which the func- th is tion was invoked. In a non-c on st member function of class X the type of t hi s is X *c on st The co ns t X, th is co ns t. c on st makes it clear that the user is not supposed to change the value of t hi s. In a c on st member co ns t th is co ns t function of class X the type of t hi s is c on st X *c on st to prevent modification of the object itself X, th is co ns t co ns t (see also §5.4.1). Most uses of t hi s are implicit. In particular, every reference to a nonstatic member from within th is a class relies on an implicit use of t hi s to get the member of the appropriate object. For example, th is the a dd _y ea r function could equivalently, but tediously, have been defined like this: ad d_ ye ar D at e& D at e::a dd _y ea r(i nt n Da te Da te ad d_ ye ar in t n) { i f (t hi s->d 29 && t hi s->m 2 && !l ea py ea r(t hi s->y n)) { if th is d==2 9 th is m==2 le ap ye ar th is y+n t hi s->d = 1 th is d 1; t hi s->m = 3 th is m 3; } t hi s->y += n th is y n; r et ur n *t hi s; re tu rn th is } One common explicit use of t hi s is in linked-list manipulation (e.g., §24.3.7.4). th is 10.2.7.1 Physical and Logical Constness [class.const] Occasionally, a member function is logically c on st but it still needs to change the value of a mem- co ns t, ber. To a user, the function appears not to change the state of its object. However, some detail that the user cannot directly observe is updated. This is often called logical constness. For example, the D at e class might have a function returning a string representation that a user could use for out- Da te put. Constructing this representation could be a relatively expensive operation. Therefore, it would make sense to keep a copy so that repeated requests would simply return the copy, unless the
    242. 232 Classes Chapter 10 D at e’s value had been changed. Caching values like that is more common for more complicated Da te data structures, but let’s see how it can be achieved for a D at e: Da te c la ss D at e { cl as s Da te b oo l c ac he _v al id bo ol ca ch e_ va li d; s tr in g c ac he st ri ng ca ch e; v oi d c om pu te _c ac he _v al ue ; vo id co mp ut e_ ca ch e_ va lu e() // fill cache // ... p ub li c: pu bl ic // ... st ri ng st ri ng _r ep s tr in g s tr in g_ re p() c on st co ns t; // string representation }; st ri ng _r ep From a user’s point of view, s tr in g_ re p doesn’t change the state of its D at e, so it clearly should be Da te a c on st member function. On the other hand, the cache needs to be filled before it can be used. co ns t This can be achieved through brute force: st ri ng Da te st ri ng _r ep s tr in g D at e::s tr in g_ re p() c on st co ns t { i f (c ac he _v al id == f al se { if ca ch e_ va li d fa ls e) D at e* t h = c on st _c as t<D at e*>(t hi s); // cast away const Da te th co ns t_ ca st Da te th is t h->c om pu te _c ac he _v al ue ; th co mp ut e_ ca ch e_ va lu e() t h->c ac he _v al id = t ru e; th ca ch e_ va li d tr ue } r et ur n c ac he re tu rn ca ch e; } That is, the c on st _c as t operator (§15.4.2.1) is used to obtain a pointer of type D at e* to t hi s. This co ns t_ ca st Da te th is is hardly elegant, and it is not guaranteed to work when applied to an object that was originally declared as a c on st For example: co ns t. D at e d 1; Da te d1 c on st D at e d 2; co ns t Da te d2 st ri ng s1 d1 st ri ng _r ep s tr in g s 1 = d 1.s tr in g_ re p(); st ri ng s2 d2 st ri ng _r ep s tr in g s 2 = d 2.s tr in g_ re p(); // undefined behavior d1 st ri ng _r ep In the case of d 1, s tr in g_ re p() simply casts back to d 1’s original type so that the call will work. d1 However, d 2 was defined as a c on st and the implementation could have applied some form of d2 co ns t d2 st ri ng _r ep memory protection to ensure that its value wasn’t corrupted. Consequently, d 2.s tr in g_ re p() is not guaranteed to give a single predictable result on all implementations. 10.2.7.2 Mutable [class.mutable] The explicit type conversion ‘‘casting away c on st and its consequent implementation-dependent co ns t’’ behavior can be avoided by declaring the data involved in the cache management to be m ut ab le mu ta bl e:
    243. Section 10.2.7.2 Mutable 233 c la ss D at e { cl as s Da te m ut ab le b oo l c ac he _v al id mu ta bl e bo ol ca ch e_ va li d; m ut ab le s tr in g c ac he mu ta bl e st ri ng ca ch e; v oi d c om pu te _c ac he _v al ue c on st // fill (mutable) cache vo id co mp ut e_ ca ch e_ va lu e() co ns t; // ... p ub li c: pu bl ic // ... st ri ng st ri ng _r ep s tr in g s tr in g_ re p() c on st co ns t; // string representation }; The storage specifier m ut ab le specifies that a member should be stored in a way that allows updat- mu ta bl e ing – even when it is a member of a c on st object. In other words, m ut ab le means ‘‘can never be co ns t mu ta bl e st ri ng _r ep c on st This can be used to simplify the definition of s tr in g_ re p(): co ns t.’’ st ri ng Da te st ri ng _r ep s tr in g D at e::s tr in g_ re p() c on st co ns t { i f (!c ac he _v al id { if ca ch e_ va li d) c om pu te _c ac he _v al ue ; co mp ut e_ ca ch e_ va lu e() c ac he _v al id = t ru e; ca ch e_ va li d tr ue } r et ur n c ac he re tu rn ca ch e; } st ri ng _r ep and makes reasonable uses of s tr in g_ re p() valid. For example: D at e d 3; Da te d3 c on st D at e d 4; co ns t Da te d4 st ri ng s3 d3 st ri ng _r ep s tr in g s 3 = d 3.s tr in g_ re p(); st ri ng s4 d4 st ri ng _r ep s tr in g s 4 = d 4.s tr in g_ re p(); // ok! Declaring members m ut ab le is most appropriate when (only) part of a representation is allowed to mu ta bl e change. If most of an object changes while the object remains logically c on st it is often better to co ns t, place the changing data in a separate object and access it indirectly. If that technique is used, the string-with-cache example becomes: s tr uc t c ac he { st ru ct ca ch e b oo l v al id bo ol va li d; s tr in g r ep st ri ng re p; }; c la ss D at e { cl as s Da te c ac he c ca ch e* c; // initialize in constructor (§10.4.6) v oi d c om pu te _c ac he _v al ue c on st // fill what cache refers to vo id co mp ut e_ ca ch e_ va lu e() co ns t; // ... p ub li c: pu bl ic // ... st ri ng st ri ng _r ep s tr in g s tr in g_ re p() c on st co ns t; // string representation };
    244. 234 Classes Chapter 10 st ri ng Da te st ri ng _r ep s tr in g D at e::s tr in g_ re p() c on st co ns t { i f (!c va li d) { if c->v al id c om pu te _c ac he _v al ue ; co mp ut e_ ca ch e_ va lu e() c va li d = t ru e; c->v al id tr ue } r et ur n c re p; re tu rn c->r ep } The programming techniques that support a cache generalize to various forms of lazy evaluation. 10.2.8 Structures and Classes [class.struct] By definition, a s tr uc t is a class in which members are by default public; that is, st ru ct s tr uc t s { ... st ru ct is simply shorthand for c la ss s { p ub li c: ... cl as s pu bl ic The access specifier p ri va te can be used to say that the members following are private, just as pr iv at e: p ub li c: says that the members following are public. Except for the different names, the following pu bl ic declarations are equivalent: c la ss D at e1 { cl as s Da te 1 i nt d m y in t d, m, y; p ub li c: pu bl ic D at e1 in t d d, i nt m m, i nt y y); Da te 1(i nt dd in t mm in t yy v oi d a dd _y ea r(i nt n ; vo id ad d_ ye ar in t n) // add n years }; s tr uc t D at e2 { st ru ct Da te 2 p ri va te pr iv at e: i nt d m y in t d, m, y; p ub li c: pu bl ic D at e2 in t d d, i nt m m, i nt y y); Da te 2(i nt dd in t mm in t yy v oi d a dd _y ea r(i nt n ; vo id ad d_ ye ar in t n) // add n years }; Which style you use depends on circumstances and taste. I usually prefer to use s tr uc t for classes st ru ct that have all data public. I think of such classes as ‘‘not quite proper types, just data structures.’’ Constructors and access functions can be quite useful even for such structures, but as a shorthand rather than guarantors of properties of the type (invariants, see §24.3.7.1). It is not a requirement to declare data first in a class. In fact, it often makes sense to place data members last to emphasize the functions providing the public user interface. For example: c la ss D at e3 { cl as s Da te 3 p ub li c: pu bl ic D at e3 in t d d, i nt m m, i nt y y); Da te 3(i nt dd in t mm in t yy
    245. Section 10.2.8 Structures and Classes 235 v oi d a dd _y ea r(i nt n ; vo id ad d_ ye ar in t n) // add n years p ri va te pr iv at e: i nt d m y in t d, m, y; }; In real code, where both the public interface and the implementation details typically are more extensive than in tutorial examples, I usually prefer the style used for D at e3 Da te 3. Access specifiers can be used many times in a single class declaration. For example: c la ss D at e4 { cl as s Da te 4 p ub li c: pu bl ic D at e4 in t d d, i nt m m, i nt y y); Da te 4(i nt dd in t mm in t yy p ri va te pr iv at e: i nt d m y in t d, m, y; p ub li c: pu bl ic v oi d a dd _y ea r(i nt n ; vo id ad d_ ye ar in t n) // add n years }; Having more than one public section, as in D at e4 tends to be messy. So does having more than Da te 4, one private section. However, allowing many access specifiers in a class is useful for machine- generated code. 10.2.9 In-Class Function Definitions [class.inline] A member function defined within the class definition – rather than simply declared there – is taken to be an inline member function. That is, in-class definition of member functions is for small, frequently-used functions. Like the class definition it is part of, a member function defined in-class can be replicated in several translation units using #i nc lu de Like the class itself, its meaning must in cl ud e. be the same wherever it is used (§9.2.3). The style of placing the definition of data members last in a class can lead to a minor problem with public inline functions that refer to the representation. Consider: c la ss D at e { cl as s Da te // potentially confusing p ub li c: pu bl ic i nt d ay c on st { r et ur n d } // return Date::d in t da y() co ns t re tu rn d; // ... p ri va te pr iv at e: i nt d m y in t d, m, y; }; This is perfectly good C++ code because a member function declared within a class can refer to every member of the class as if the class were completely defined before the member function bod- ies were considered. However, this can confuse human readers. Consequently, I usually either place the data first or define the inline member functions after the class itself. For example:
    246. 236 Classes Chapter 10 c la ss D at e { cl as s Da te p ub li c: pu bl ic i nt d ay in t da y() c on st co ns t; // ... p ri va te pr iv at e: i nt d m y in t d, m, y; }; i nl in e i nt D at e::d ay in li ne in t Da te da y() c on st { r et ur n d } co ns t re tu rn d; 10.3 Efficient User-Defined Types [class.concrete] The previous section discussed bits and pieces of the design of a D at e class in the context of intro- Da te ducing the basic language features for defining classes. Here, I reverse the emphasis and discuss the design of a simple and efficient D at e class and show how the language features support this Da te design. Small, heavily-used abstractions are common in many applications. Examples are Latin charac- ters, Chinese characters, integers, floating-point numbers, complex numbers, points, pointers, coor- dinates, transforms, (pointer,offset) pairs, dates, times, ranges, links, associations, nodes, (value,unit) pairs, disk locations, source code locations, B CD characters, currencies, lines, rectan- BC D gles, scaled fixed-point numbers, numbers with fractions, character strings, vectors, and arrays. Every application uses several of these. Often, a few of these simple concrete types are used heav- ily. A typical application uses a few directly and many more indirectly from libraries. C++ and other programming languages directly support a few of these abstractions. However, most are not, and cannot be, supported directly because there are too many of them. Furthermore, the designer of a general-purpose programming language cannot foresee the detailed needs of every application. Consequently, mechanisms must be provided for the user to define small concrete types. Such types are called concrete types or concrete classes to distinguish them from abstract classes (§12.3) and classes in class hierarchies (§12.2.4, §12.4). It was an explicit aim of C++ to support the definition and efficient use of such user-defined data types very well. They are a foundation of elegant programming. As usual, the simple and mundane is statistically far more significant than the complicated and sophisticated. In this light, let us build a better date class: c la ss D at e { cl as s Da te p ub li c: pu bl ic // public interface: e nu m M on th { j an 1, f eb m ar a pr m ay j un j ul a ug s ep o ct n ov d ec }; en um Mo nt h ja n=1 fe b, ma r, ap r, ma y, ju n, ju l, au g, se p, oc t, no v, de c c la ss B ad _d at e { }; // exception class cl as s Ba d_ da te D at e(i nt d d =0 M on th m m =M on th 0), i nt y y =0 ; // 0 means ‘‘pick a default’’ Da te in t dd 0, Mo nt h mm Mo nt h(0 in t yy 0) // functions for examining the Date: i nt d ay c on st in t da y() co ns t; M on th m on th Mo nt h mo nt h() c on st co ns t; i nt y ea r() c on st in t ye ar co ns t;
    247. Section 10.3 Efficient User-Defined Types 237 st ri ng st ri ng _r ep s tr in g s tr in g_ re p() c on st co ns t; // string representation vo id ch ar _r ep ch ar s[]) c on st v oi d c ha r_ re p(c ha r s co ns t; // C-style string representation s ta ti c v oi d s et _d ef au lt in t, M on th i nt ; st at ic vo id se t_ de fa ul t(i nt Mo nt h, in t) // functions for changing the Date: D at e& a dd _y ea r(i nt n ; Da te ad d_ ye ar in t n) // add n years D at e& a dd _m on th in t n ; Da te ad d_ mo nt h(i nt n) // add n months D at e& a dd _d ay in t n ; Da te ad d_ da y(i nt n) // add n days p ri va te pr iv at e: i nt d m y in t d, m, y; // representation s ta ti c D at e d ef au lt _d at e; st at ic Da te de fa ul t_ da te }; This set of operations is fairly typical for a user-defined type: [1] A constructor specifying how objects/variables of the type are to be initialized. [2] A set of functions allowing a user to examine a D at e. These functions are marked c on st to Da te co ns t indicate that they don’t modify the state of the object/variable for which they are called. [3] A set of functions allowing the user to manipulate D at es without actually having to know Da te the details of the representation or fiddle with the intricacies of the semantics. [4] A set of implicitly defined operations to allow D at es to be freely copied. Da te [5] A class, B ad _d at e, to be used for reporting errors as exceptions. Ba d_ da te I defined a M on th type to cope with the problem of remembering, for example, whether the 7th of Mo nt h June is written D at e(6 7) (American style) or D at e(7 6) (European style). I also added a Da te 6,7 Da te 7,6 mechanism for dealing with default arguments. I considered introducing separate types D ay and Y ea r to cope with possible confusion of Da y Ye ar D at e(1 99 5,j ul 27 and D at e(2 7,j ul 19 95 However, these types would not be as useful as Da te 19 95 ju l,2 7) Da te 27 ju l,1 99 5). the M on th type. Almost all such errors are caught at run-time anyway – the 26th of July year 27 is Mo nt h not a common date in my work. How to deal with historical dates before year 1800 or so is a tricky issue best left to expert historians. Furthermore, the day of the month can’t be properly checked in isolation from its month and year. See §11.7.1 for a way of defining a convenient Y ea r type. Ye ar The default date must be defined as a valid D at e somewhere. For example: Da te D at e D at e::d ef au lt _d at e(2 2,j an 19 01 ; Da te Da te de fa ul t_ da te 22 ja n,1 90 1) I omitted the cache technique from §10.2.7.1 as unnecessary for a type this simple. If needed, it can be added as an implementation detail without affecting the user interface. Here is a small – and contrived – example of how D at es can be used: Da te v oi d f Da te d vo id f(D at e& d) { D at e l vb _d ay = D at e(1 6,D at e::d ec d.y ea r()); Da te lv b_ da y Da te 16 Da te de c,d ye ar i f (d da y()==2 9 && d mo nt h()==D at e::f eb { if d.d ay 29 d.m on th Da te fe b) // ... } d.a dd _d ay 1) i f (m id ni gh t()) d ad d_ da y(1 ; if mi dn ig ht c ou t << \"d ay a ft er << d 1 << ´\\ n´; co ut da y af te r:\" d+1 \\n }
    248. 238 Classes Chapter 10 This assumes that the output operator << and the addition operator + have been declared for D at es. Da te I do that in §10.3.3. Note the D at e::f eb notation. The function f is not a member of D at e, so it must specify that Da te fe b f() Da te it is referring to D at e’s f eb and not to some other entity. Da te fe b Why is it worthwhile to define a specific type for something as simple as a date? After all, we could define a structure: s tr uc t D at e { st ru ct Da te i nt d ay m on th y ea r; in t da y, mo nt h, ye ar }; and let programmers decide what to do with it. If we did that, though, every user would either have to manipulate the components of D at es directly or provide separate functions for doing so. In Da te effect, the notion of a date would be scattered throughout the system, which would make it hard to understand, document, or change. Inevitably, providing a concept as only a simple structure causes extra work for every user of the structure. Also, even though the D at e type seems simple, it takes some thought to get right. For example, Da te incrementing a D at e must deal with leap years, with the fact that months are of different lengths, Da te and so on (note: §10.6[1]). Also, the day-month-and-year representation is rather poor for many applications. If we decided to change it, we would need to modify only a designated set of func- tions. For example, to represent a D at e as the number of days before or after January 1, 1970, we Da te would need to change only D at e’s member functions (§10.6[2]). Da te 10.3.1 Member Functions [class.memfct] Naturally, an implementation for each member function must be provided somewhere. For exam- ple, here is the definition of D at e’s constructor: Da te D at e::D at e(i nt d d, M on th m m, i nt y y) Da te Da te in t dd Mo nt h mm in t yy { i f (y y == 0 y y = d ef au lt _d at e.y ea r(); if yy 0) yy de fa ul t_ da te ye ar i f (m m == 0 m m = d ef au lt _d at e.m on th ; if mm 0) mm de fa ul t_ da te mo nt h() i f (d d == 0 d d = d ef au lt _d at e.d ay ; if dd 0) dd de fa ul t_ da te da y() i nt m ax in t ma x; s wi tc h (m m) { sw it ch mm c as e f eb ca se fe b: m ax = 2 8+l ea py ea r(y y); ma x 28 le ap ye ar yy b re ak br ea k; c as e a pr c as e j un c as e s ep c as e n ov ca se ap r: ca se ju n: ca se se p: ca se no v: m ax = 3 0; ma x 30 b re ak br ea k; c as e j an c as e m ar c as e m ay c as e j ul c as e a ug c as e o ct c as e d ec ca se ja n: ca se ma r: ca se ma y: ca se ju l: ca se au g: ca se oc t: ca se de c: m ax = 3 1; ma x 31 b re ak br ea k; d ef au lt de fa ul t: t hr ow B ad _d at e(); // someone cheated th ro w Ba d_ da te }
    249. Section 10.3.1 Member Functions 239 i f (d d<1 || m ax dd t hr ow B ad _d at e(); if dd 1 ma x<d d) th ro w Ba d_ da te y = y y; yy m = m m; mm d = d d; dd } The constructor checks that the data supplied denotes a valid D at e. If not, say for Da te D at e(3 0,D at e::f eb 19 94 it throws an exception (§8.3, Chapter 14), which indicates that Da te 30 Da te fe b,1 99 4), something went wrong in a way that cannot be ignored. If the data supplied is acceptable, the obvi- ous initialization is done. Initialization is a relatively complicated operation because it involves data validation. This is fairly typical. On the other hand, once a D at e has been created, it can be Da te used and copied without further checking. In other words, the constructor establishes the invariant for the class (in this case, that it denotes a valid date). Other member functions can rely on that invariant and must maintain it. This design technique can simplify code immensely (see §24.3.7.1). I’m using the value M on th 0) – which doesn’t represent a month – to represent ‘‘pick the Mo nt h(0 default month.’’ I could have defined an enumerator in M on th specifically to represent that. But I Mo nt h decided that it was better to use an obviously anomalous value to represent ‘‘pick the default month’’ rather than give the appearance that there were 13 months in a year. Note that 0 can be used because it is within the range guaranteed for the enumeration M on th (§4.8). Mo nt h is _d at e(). However, I I considered factoring out the data validation in a separate function i s_ da te found the resulting user code more complicated and less robust than code relying on catching the exception. For example, assuming that >> is defined for D at e: Da te v oi d f il l(v ec to r<D at e>& a a) vo id fi ll ve ct or Da te aa { w hi le (c in { wh il e ci n) D at e d Da te d; t ry { tr y c in >> d ci n d; } c at ch (D at e::B ad _d at e) { ca tc h Da te Ba d_ da te // my error handling c on ti nu e; co nt in ue } aa pu sh _b ac k(d a a.p us h_ ba ck d); // see §3.7.3 } } As is common for such simple concrete types, the definitions of member functions vary between the trivial and the not-too-complicated. For example: i nl in e i nt D at e::d ay c on st in li ne in t Da te da y() co ns t { r et ur n d re tu rn d; }
    250. 240 Classes Chapter 10 D at e& D at e::a dd _m on th in t n Da te Da te ad d_ mo nt h(i nt n) { i f (n 0) r et ur n *t hi s; if n==0 re tu rn th is i f (n 0) { if n>0 i nt d el ta _y = n 12 in t de lt a_ y n/1 2; i nt m m = m n%1 2; in t mm m+n 12 i f (1 2 < m m) { // note: int(dec)==12 if 12 mm d el ta _y de lt a_ y++; m m -= 1 2; mm 12 } // handle the cases where Month(mm) doesn’t have day d y += d el ta _y de lt a_ y; m = M on th mm ; Mo nt h(m m) r et ur n *t hi s; re tu rn th is } // handle negative n r et ur n *t hi s; re tu rn th is } 10.3.2 Helper Functions [class.helper] Typically, a class has a number of functions associated with it that need not be defined in the class itself because they don’t need direct access to the representation. For example: i nt d if f(D at e a D at e b ; // number of days in the range [a,b) or [b,a) in t di ff Da te a, Da te b) b oo l l ea py ea r(i nt y ; bo ol le ap ye ar in t y) Da te ne xt _w ee kd ay Da te d) D at e n ex t_ we ek da y(D at e d ; Da te ne xt _s at ur da y(D at e d) D at e n ex t_ sa tu rd ay Da te d ; Defining such functions in the class itself would complicate the class interface and increase the number of functions that would potentially need to be examined when a change to the representa- tion was considered. How are such functions ‘‘associated’’ with class D at e? Traditionally, their declarations were Da te simply placed in the same file as the declaration of class D at e, and users who needed D at es would Da te Da te make them all available by including the file that defined the interface (§9.2.1). For example: #i nc lu de \"D at e.h in cl ud e Da te h\" In addition to using a specific D at e.h header, or as an alternative, we can make the association Da te h explicit by enclosing the class and its helper functions in a namespace (§8.2): n am es pa ce C hr on o { na me sp ac e Ch ro no // facilities for dealing with time c la ss D at e { /* ... */}; cl as s Da te
    251. Section 10.3.2 Helper Functions 241 i nt d if f(D at e a D at e b ; in t di ff Da te a, Da te b) b oo l l ea py ea r(i nt y ; bo ol le ap ye ar in t y) Da te ne xt _w ee kd ay Da te d) D at e n ex t_ we ek da y(D at e d ; Da te ne xt _s at ur da y(D at e d) D at e n ex t_ sa tu rd ay Da te d ; // ... } The C hr on o namespace would naturally also contain related classes, such as T im e and S to pw at ch Ch ro no Ti me St op wa tc h, and their helper functions. Using a namespace to hold a single class is usually an over-elaboration that leads to inconvenience. 10.3.3 Overloaded Operators [class.over] It is often useful to add functions to enable conventional notation. For example, the o pe ra to r== op er at or function defines the equality operator == to work for D at es: Da te i nl in e b oo l o pe ra to r==(D at e a D at e b // equality in li ne bo ol op er at or Da te a, Da te b) { r et ur n a da y()==b da y() && a mo nt h()==b mo nt h() && a ye ar re tu rn a.d ay b.d ay a.m on th b.m on th a.y ea r()==b ye ar ; b.y ea r() } Other obvious candidates are: b oo l o pe ra to r!=(D at e, D at e); bo ol op er at or Da te Da te // inequality b oo l o pe ra to r<(D at e, D at e); bo ol op er at or Da te Da te // less than b oo l o pe ra to r>(D at e, D at e); bo ol op er at or Da te Da te // greater than // ... D at e& o pe ra to r++(D at e& d ; Da te op er at or Da te d) // increase Date by one day D at e& o pe ra to r--(D at e& d ; Da te op er at or Da te d) // decrease Date by one day D at e& o pe ra to r+=(D at e& d i nt n ; Da te op er at or Da te d, in t n) // add n days D at e& o pe ra to r-=(D at e& d i nt n ; Da te op er at or Da te d, in t n) // subtract n days D at e o pe ra to r+(D at e d i nt n ; Da te op er at or Da te d, in t n) // add n days D at e o pe ra to r-(D at e d i nt n ; Da te op er at or Da te d, in t n) // subtract n days o st re am o pe ra to r<<(o st re am os tr ea m& op er at or os tr ea m&, D at e d ; // output d Da te d) i st re am o pe ra to r>>(i st re am is tr ea m& op er at or is tr ea m&, D at e& d ; // read into d Da te d) For D at e, these operators can be seen as mere conveniences. However, for many types – such as Da te complex numbers (§11.3), vectors (§3.7.1), and function-like objects (§18.4) – the use of conven- tional operators is so firmly entrenched in people’s minds that their definition is almost mandatory. Operator overloading is discussed in Chapter 11. 10.3.4 The Significance of Concrete Classes [class.significance] I call simple user-defined types, such as D at e, concrete types to distinguish them from abstract Da te classes (§2.5.4) and class hierarchies (§12.3) and also to emphasize their similarity to built-in types such as i nt and c ha r. They have also been called value types, and their use value-oriented in t ch ar programming. Their model of use and the ‘‘philosophy’’ behind their design are quite different from what is often advertised as object-oriented programming (§2.6.2).
    252. 242 Classes Chapter 10 The intent of a concrete type is to do a single, relatively small thing well and efficiently. It is not usually the aim to provide the user with facilities to modify the behavior of a concrete type. In particular, concrete types are not intended to display polymorphic behavior (see §2.5.5, §12.2.6). If you don’t like some detail of a concrete type, you build a new one with the desired behavior. If you want to ‘‘reuse’’ a concrete type, you use it in the implementation of your new type exactly as you would have used an i nt For example: in t. cl as s Da te _a nd _t im e c la ss D at e_ an d_ ti me { p ri va te pr iv at e: D at e d Da te d; T im e t Ti me t; p ub li c: pu bl ic Da te _a nd _t im e(D at e d, Ti me t) D at e_ an d_ ti me Da te d T im e t ; Da te _a nd _t im e(i nt d, Da te Mo nt h m, in t y, Ti me t) D at e_ an d_ ti me in t d D at e::M on th m i nt y T im e t ; // ... }; The derived class mechanism discussed in Chapter 12 can be used to define new types from a con- crete class by describing the desired differences. The definition of V ec from v ec to r (§3.7.2) is an Ve c ve ct or example of this. With a reasonably good compiler, a concrete class such as D at e incurs no hidden overhead in Da te time or space. The size of a concrete type is known at compile time so that objects can be allocated on the run-time stack (that is, without free-store operations). The layout of each object is known at compile time so that inlining of operations is trivially achieved. Similarly, layout compatibility with other languages, such as C and Fortran, comes without special effort. A good set of such types can provide a foundation for applications. Lack of suitable ‘‘small efficient types’’ in an application can lead to gross run-time and space inefficiencies when overly general and expensive classes are used. Alternatively, lack of concrete types can lead to obscure programs and time wasted when each programmer writes code to directly manipulate ‘‘simple and frequently used’’ data structures. 10.4 Objects [class.objects] Objects can be created in several ways. Some are local variables, some are global variables, some are members of classes, etc. This section discusses these alternatives, the rules that govern them, the constructors used to initialize objects, and the destructors used to clean up objects before they become unusable. 10.4.1 Destructors [class.dtor] A constructor initializes an object. In other words, it creates the environment in which the member functions operate. Sometimes, creating that environment involves acquiring a resource – such as a file, a lock, or some memory – that must be released after use (§14.4.7). Thus, some classes need a function that is guaranteed to be invoked when an object is destroyed in a manner similar to the way a constructor is guaranteed to be invoked when an object is created. Inevitably, such functions are called destructors. They typically clean up and release resources. Destructors are called
    253. Section 10.4.1 Destructors 243 implicitly when an automatic variable goes out of scope, an object on the free store is deleted, etc. Only in very unusual circumstances does the user need to call a destructor explicitly (§10.4.11). The most common use of a destructor is to release memory acquired in a constructor. Consider a simple table of elements of some type N am e. The constructor for T ab le must allocate memory to Na me Ta bl e hold the elements. When the table is somehow deleted, we must ensure that this memory is reclaimed for further use elsewhere. We do this by providing a special function to complement the constructor: c la ss N am e { cl as s Na me c on st c ha r* s co ns t ch ar s; // ... }; c la ss T ab le { cl as s Ta bl e N am e* p Na me p; si ze _t sz s iz e_ t s z; p ub li c: pu bl ic T ab le si ze _t s = 1 5) { p = n ew N am e[s z = s ; }// constructor Ta bl e(s iz e_ t 15 ne w Na me sz s] ~T ab le { d el et e[] p } Ta bl e() de le te p; // destructor N am e* l oo ku p(c on st c ha r *); Na me lo ok up co ns t ch ar b oo l i ns er t(N am e*); bo ol in se rt Na me }; The destructor notation ~T ab le Ta bl e() uses the complement symbol ~ to hint at the destructor’s rela- tion to the T ab le constructor. Ta bl e() A matching constructor/destructor pair is the usual mechanism for implementing the notion of a variably-sized object in C++. Standard library containers, such as m ap use a variant of this tech- ma p, nique for providing storage for their elements, so the following discussion illustrates techniques you rely on every time you use a standard container (including a standard s tr in g). The discussion st ri ng applies to types without a destructor, also. Such types are seen simply as having a destructor that does nothing. 10.4.2 Default Constructors [class.default] Similarly, most types can be considered to have a default constructor. A default constructor is a constructor that can be called without supplying an argument. Because of the default argument 1 5, 15 T ab le :T ab le si ze _t is a default constructor. If a user has declared a default constructor, that Ta bl e: Ta bl e(s iz e_ t) one will be used; otherwise, the compiler will try to generate one if needed and if the user hasn’t declared other constructors. A compiler-generated default constructor implicitly calls the default constructors for a class’ members of class type and bases (§12.2.2). For example: s tr uc t T ab le s { st ru ct Ta bl es i nt i in t i; i nt v i[1 0]; in t vi 10 T ab le t 1; Ta bl e t1 T ab le v t[1 0]; Ta bl e vt 10 };
    254. 244 Classes Chapter 10 T ab le s t t; Ta bl es tt Here, t t will be initialized using a generated default constructor that calls T ab le 15 for t t.t 1 and tt Ta bl e(1 5) tt t1 each element of t t.v t. On the other hand, t t.i and the elements of t t.v i are not initialized because tt vt tt i tt vi those objects are not of a class type. The reasons for the dissimilar treatment of classes and built-in types are C compatibility and fear of run-time overhead. Because c on st and references must be initialized (§5.5, §5.4), a class containing c on st or refer- co ns ts co ns t ence members cannot be default-constructed unless the programmer explicitly supplies a construc- tor (§10.4.6.1). For example: s tr uc t X { st ru ct c on st i nt a co ns t in t a; c on st i nt r co ns t in t& r; }; X x // error: no default constructor for X x; Default constructors can be invoked explicitly (§10.4.10). Built-in types also have default con- structors (§6.2.8). 10.4.3 Construction and Destruction [class.ctor.dtor] Consider the different ways an object can be created and how it gets destroyed afterwards. An object can be created as: §10.4.4 A named automatic object, which is created each time its declaration is encountered in the execution of the program and destroyed each time the program exits the block in which it occurs §10.4.5 A free-store object, which is created using the n ew operator and destroyed using the ne w d el et e operator de le te §10.4.6 A nonstatic member object, which is created as a member of another class object and created and destroyed when the object of which it is a member is created and destroyed §10.4.7 An array element, which is created and destroyed when the array of which it is an ele- ment is created and destroyed §10.4.8 A local static object, which is created the first time its declaration is encountered in the execution of the program and destroyed once at the termination of the program §10.4.9 A global, namespace, or class static object, which is created once ‘‘at the start of the program’’ and destroyed once at the termination of the program §10.4.10 A temporary object, which is created as part of the evaluation of an expression and destroyed at the end of the full expression in which it occurs §10.4.11 An object placed in memory obtained from a user-supplied function guided by argu- ments supplied in the allocation operation §10.4.12 A u ni on member, which may not have a constructor or a destructor un io n This list is roughly sorted in order of importance. The following subsections explain these various ways of creating objects and their uses.
    255. Section 10.4.4 Local Variables 245 10.4.4 Local Variables [class.local] The constructor for a local variable is executed each time the thread of control passes through the declaration of the local variable. The destructor for a local variable is executed each time the local variable’s block is exited. Destructors for local variables are executed in reverse order of their con- struction. For example: v oi d f in t i vo id f(i nt i) { T ab le a a; Ta bl e aa T ab le b b; Ta bl e bb i f (i 0) { if i>0 T ab le c c; Ta bl e cc // ... } T ab le d d; Ta bl e dd // ... } Here, a a, b b, and d d are constructed (in that order) each time f aa bb dd f() is called, and d d, b b, and a a are dd bb aa destroyed (in that order) each time we return from f f(). If i 0 for a call, c c will be constructed after i>0 cc b b and destroyed before d d is constructed. bb dd 10.4.4.1 Copying Objects [class.copy] If t 1 and t 2 are objects of a class T ab le t 2=t 1 by default means a memberwise copy of t 1 into t 2 t1 t2 Ta bl e, t2 t1 t1 t2 (§10.2.5). Having assignment interpreted this way can cause a surprising (and usually undesired) effect when used on objects of a class with pointer members. Memberwise copy is usually the wrong semantics for copying objects containing resources managed by a constructor/destructor pair. For example: v oi d h vo id h() { T ab le t 1; Ta bl e t1 T ab le t 2 = t 1; // copy initialization: trouble Ta bl e t2 t1 T ab le t 3; Ta bl e t3 t 3 = t 2; t3 t2 // copy assignment: trouble } Here, the T ab le default constructor is called twice: once each for t 1 and t 3. It is not called for t 2 Ta bl e t1 t3 t2 because that variable was initialized by copying. However, the T ab le destructor is called three Ta bl e times: once each for t 1, t 2, and t 3! The default interpretation of assignment is memberwise copy, so t1 t2 t3 t 1, t 2, and t 3 will, at the end of h t1 t2 t3 h(), each contain a pointer to the array of names allocated on the free store when t 1 was created. No pointer to the array of names allocated when t 3 was created t1 t3 remains because it was overwritten by the t 3=t 2 assignment. Thus, in the absence of automatic t3 t2 garbage collection (§10.4.5), its storage will be lost to the program forever. On the other hand, the array created for t 1 appears in t 1, t 2, and t 3, so it will be deleted thrice. The result of that is unde- t1 t1 t2 t3 fined and probably disastrous.
    256. 246 Classes Chapter 10 Such anomalies can be avoided by defining what it means to copy a T ab le Ta bl e: c la ss T ab le { cl as s Ta bl e // ... T ab le co ns t T ab le ; Ta bl e(c on st Ta bl e&) // copy constructor T ab le o pe ra to r=(c on st T ab le ; Ta bl e& op er at or co ns t Ta bl e&) // copy assignment }; The programmer can define any suitable meaning for these copy operations, but the traditional one for this kind of container is to copy the contained elements (or at least to give the user of the con- tainer the appearance that a copy has been done; see §11.12). For example: T ab le :T ab le co ns t T ab le t Ta bl e: Ta bl e(c on st Ta bl e& t) // copy constructor { p = n ew N am e[s z=t sz ; ne w Na me sz t.s z] f or (i nt i = 0 i sz i fo r in t 0; i<s z; i++) p i] = t p[i ; p[i t.p i] } T ab le T ab le :o pe ra to r=(c on st T ab le t Ta bl e& Ta bl e: op er at or co ns t Ta bl e& t) // assignment { i f (t hi s != &t { if th is t) // beware of self-assignment: t = t d el et e[] p de le te p; p = n ew N am e[s z=t sz ; ne w Na me sz t.s z] f or (i nt i = 0 i sz i fo r in t 0; i<s z; i++) p i] = t p[i ; p[i t.p i] } r et ur n *t hi s; re tu rn th is } As is almost always the case, the copy constructor and the copy assignment differ considerably. The fundamental reason is that a copy constructor initializes uninitialized memory, whereas the copy assignment operator must correctly deal with a well-constructed object. Assignment can be optimized in some cases, but the general strategy for an assignment operator is simple: protect against self-assignment, delete old elements, initialize, and copy in new elements. Usually every nonstatic member must be copied (§10.4.6.3). 10.4.5 Free Store [class.free] An object created on the free store has its constructor invoked by the n ew operator and exists until ne w the d el et e operator is applied to a pointer to it. Consider: de le te i nt m ai n() in t ma in { T ab le p = n ew T ab le Ta bl e* ne w Ta bl e; T ab le q = n ew T ab le Ta bl e* ne w Ta bl e; d el et e p de le te p; d el et e p // probably causes run-time error de le te p; } The constructor T ab le :T ab le Ta bl e: Ta bl e() is called twice. So is the destructor T ab le :~T ab le Ta bl e: Ta bl e(). Unfor- tunately, the n ew and the d el et es in this example don’t match, so the object pointed to by p is ne ws de le te
    257. Section 10.4.5 Free Store 247 deleted twice and the object pointed to by q not at all. Not deleting an object is typically not an error as far as the language is concerned; it is only a waste of space. However, in a program that is meant to run for a long time, such a memory leak is a serious and hard-to-find error. There are tools available for detecting such leaks. Deleting p twice is a serious error; the behavior is unde- fined and most likely disastrous. Some C++ implementations automatically recycle the storage occupied by unreachable objects (garbage collecting implementations), but their behavior is not standardized. Even when a garbage collector is running, d el et e will invoke a destructor if one is defined, so it is still a serious error to de le te delete an object twice. In many cases, that is only a minor inconvenience. In particular, where a garbage collector is known to exist, destructors that do memory management only can be elimi- nated. This simplification comes at the cost of portability and for some programs, a possible increase in run time and a loss of predictability of run-time behavior (§C.9.1). After d el et e has been applied to an object, it is an error to access that object in any way. Unfor- de le te tunately, implementations cannot reliably detect such errors. The user can specify how n ew does allocation and how d el et e does deallocation (see §6.2.6.2 ne w de le te and §15.6). It is also possible to specify the way an allocation, initialization (construction), and exceptions interact (see §14.4.5 and §19.4.5). Arrays on the free store are discussed in §10.4.7. 10.4.6 Class Objects as Members [class.m] Consider a class that might be used to hold information for a small organization: c la ss C lu b { cl as s Cl ub s tr in g n am e; st ri ng na me T ab le m em be rs Ta bl e me mb er s; T ab le o ff ic er s; Ta bl e of fi ce rs D at e f ou nd ed Da te fo un de d; // ... C lu b(c on st s tr in g& n D at e f d); Cl ub co ns t st ri ng n, Da te fd }; The C lu b’s constructor takes the name of the club and its founding date as arguments. Arguments Cl ub for a member’s constructor are specified in a member initializer list in the definition of the con- structor of the containing class. For example: C lu b::C lu b(c on st s tr in g& n D at e f d) Cl ub Cl ub co ns t st ri ng n, Da te fd : n am e(n , m em be rs , o ff ic er s(), f ou nd ed fd na me n) me mb er s() of fi ce rs fo un de d(f d) { // ... } The member initializers are preceded by a colon and the individual member initializers are sepa- rated by commas. The members’ constructors are called before the body of the containing class’ own constructor is executed. The constructors are called in the order in which they are declared in the class rather than the order in which they appear in the initializer list. To avoid confusion, it is best to specify the initializers in declaration order. The member destructors are called in the reverse order of con- struction.
    258. 248 Classes Chapter 10 If a member constructor needs no arguments, the member need not be mentioned in the member initializer list, so C lu b::C lu b(c on st s tr in g& n D at e f d) Cl ub Cl ub co ns t st ri ng n, Da te fd : n am e(n , f ou nd ed fd na me n) fo un de d(f d) { // ... } is equivalent to the previous version. In each case, C lu b::o ff ic er s is constructed by T ab le :T ab le Cl ub of fi ce rs Ta bl e: Ta bl e with the default argument 1 5. 15 When a class object containing class objects is destroyed, the body of that object’s own destructor (if one is specified) is executed first and then the members’ destructors are executed in reverse order of declaration. A constructor assembles the execution environment for the member functions for a class from the bottom up (members first). The destructor disassembles it from the top down (members last). 10.4.6.1 Necessary Member Initialization [class.ref.init] Member initializers are essential for types for which initialization differs from assignment – that is, for member objects of classes without default constructors, for c on st members, and for reference co ns t members. For example: c la ss X { cl as s c on st i nt i co ns t in t i; C lu b c Cl ub c; C lu b& p c; Cl ub pc // ... X in t i i, c on st s tr in g& n D at e d C lu b& c : i ii , c n,d , p c(c { } X(i nt ii co ns t st ri ng n, Da te d, Cl ub c) i(i i) c(n d) pc c) }; There isn’t any other way to initialize such members, and it is an error not to initialize objects of those types. For most types, however, the programmer has a choice between using an initializer and using an assignment. In that case, I usually prefer to use the member initializer syntax, thus making explicit the fact that initialization is being done. Often, there also is an efficiency advan- tage to using the initializer syntax. For example: c la ss P er so n { cl as s Pe rs on s tr in g n am e; st ri ng na me s tr in g a dd re ss st ri ng ad dr es s; // ... P er so n(c on st P er so n&); Pe rs on co ns t Pe rs on P er so n(c on st s tr in g& n c on st s tr in g& a ; Pe rs on co ns t st ri ng n, co ns t st ri ng a) }; P er so n::P er so n(c on st s tr in g& n c on st s tr in g& a Pe rs on Pe rs on co ns t st ri ng n, co ns t st ri ng a) : n am e(n na me n) { a dd re ss = a ad dr es s a; }
    259. Section 10.4.6.1 Necessary Member Initialization 249 Here n am e is initialized with a copy of n On the other hand, a dd re ss is first initialized to the na me n. ad dr es s empty string and then a copy of a is assigned. 10.4.6.2 Member Constants [class.memconst] It is also possible to initialize a static integral constant member by adding a constant-expression ini- tializer to its member declaration. For example: c la ss C ur io us { cl as s Cu ri ou s p ub li c: pu bl ic s ta ti c c on st i nt c 1 = 7 st at ic co ns t in t c1 7; // ok, but remember definition s ta ti c i nt c 2 = 1 1; st at ic in t c2 11 // error: not const c on st i nt c 3 = 1 3; co ns t in t c3 13 // error: not static s ta ti c c on st i nt c 4 = f 17 ; st at ic co ns t in t c4 f(1 7) // error: in-class initializer not constant s ta ti c c on st f lo at c 5 = 7 0; st at ic co ns t fl oa t c5 7.0 // error: in-class not integral // ... }; If (and only if) you use an initialized member in a way that requires it to be stored as an object in memory, the member must be (uniquely) defined somewhere. The initializer may not be repeated: c on st i nt C ur io us :c 1; co ns t in t Cu ri ou s: c1 // necessary, but don’t repeat initializer here c on st i nt p = &C ur io us :c 1; co ns t in t* Cu ri ou s: c1 // ok: Curious::c1 has been defined Alternatively, you can use an enumerator (§4.8, §14.4.6, §15.3) as a symbolic constant within a class declaration. For example: c la ss X { cl as s e nu m { c 1 = 7 c 2 = 1 1, c 3 = 1 3, c 4 = 1 7 }; en um c1 7, c2 11 c3 13 c4 17 // ... }; In that way, you are not tempted to initialize variables, floating-point numbers, etc. within a class. 10.4.6.3 Copying Members [class.mem.copy] A default copy constructor or default copy assignment (§10.4.4.1) copies all elements of a class. If this copy cannot be done, it is an error to try to copy an object of such a class. For example: cl as s Un iq ue _h an dl e c la ss U ni qu e_ ha nd le { p ri va te pr iv at e: // copy operations are private to prevent copying (§11.2.2) Un iq ue _h an dl e(c on st Un iq ue _h an dl e&) U ni qu e_ ha nd le co ns t U ni qu e_ ha nd le ; Un iq ue _h an dl e& op er at or co ns t Un iq ue _h an dl e&) U ni qu e_ ha nd le o pe ra to r=(c on st U ni qu e_ ha nd le ; p ub li c: pu bl ic // ... }; s tr uc t Y { st ru ct // ... Un iq ue _h an dl e a; U ni qu e_ ha nd le a // requires explicit initialization };
    260. 250 Classes Chapter 10 Y y 1; y1 Y y 2 = y 1; y2 y1 // error: cannot copy Y::a In addition, a default assignment cannot be generated if a nonstatic member is a reference, a c on st co ns t, or a user-defined type without a copy assignment. Note that the default copy constructor leaves a reference member referring to the same object in both the original and the copied object. This can be a problem if the object referred to is supposed to be deleted. When writing a copy constructor, we must take care to copy every element that needs to be copied. By default, elements are default-initialized, but that is often not what is desired in a copy constructor. For example: P er so n::P er so n(c on st P er so n& a : n am e(a na me { } Pe rs on Pe rs on co ns t Pe rs on a) na me a.n am e) // beware! Here, I forgot to copy the a dd re ss so a dd re ss is initialized to the empty string by default. When ad dr es s, ad dr es s adding a new member to a class, always check if there are user-defined constructors that need to be updated in order to initialize and copy the new member. 10.4.7 Arrays [class.array] If an object of a class can be constructed without supplying an explicit initializer, then arrays of that class can be defined. For example: T ab le t bl 10 ; Ta bl e tb l[1 0] This will create an array of 1 0 T ab le and initialize each T ab le by a call of T ab le :T ab le 10 Ta bl es Ta bl e Ta bl e: Ta bl e() with the default argument 1 5. 15 There is no way to specify explicit arguments for a constructor in an array declaration. If you absolutely must initialize members of an array with different values, you can write a default con- structor that directly or indirectly reads and writes nonlocal data. For example: c la ss I bu ff er { cl as s Ib uf fe r s tr in g b uf st ri ng bu f; p ub li c: pu bl ic I bu ff er Ib uf fe r() { c in bu f; } ci n>>b uf // ... }; v oi d f vo id f() { I bu ff er w or ds 10 0]; // each word initialized from cin Ib uf fe r wo rd s[1 00 // ... } It is usually best to avoid such subtleties. The destructor for each constructed element of an array is invoked when that array is destroyed. This is done implicitly for arrays that are not allocated using n ew Like C, C++ doesn’t distinguish ne w. between a pointer to an individual object and a pointer to the initial element of an array (§5.3). Consequently, the programmer must state whether an array or an individual object is being deleted. For example:
    261. Section 10.4.7 Arrays 251 v oi d f in t s z) vo id f(i nt sz { T ab le t 1 = n ew Ta bl e* t1 ne w T ab le Ta bl e; T ab le t 2 = n ew Ta bl e* t2 ne w T ab le sz ; Ta bl e[s z] T ab le t 3 = n ew Ta bl e* t3 ne w T ab le Ta bl e; T ab le t 4 = n ew Ta bl e* t4 ne w T ab le sz ; Ta bl e[s z] d el et e t 1; de le te t1 // right d el et e[] t 2; de le te t2 // right d el et e[] t 3; de le te t3 // wrong: trouble d el et e t 4; de le te t4 // wrong: trouble } Exactly how arrays and individual objects are allocated is implementation-dependent. Therefore, different implementations will react differently to incorrect uses of the d el et e and d el et e[] opera- de le te de le te tors. In simple and uninteresting cases like the previous one, a compiler can detect the problem, but generally something nasty will happen at run time. The special destruction operator for arrays, d el et e[], isn’t logically necessary. However, sup- de le te pose the implementation of the free store had been required to hold sufficient information for every object to tell if it was an individual or an array. The user could have been relieved of a burden, but that obligation would have imposed significant time and space overheads on some C++ implemen- tations. As always, if you find C-style arrays too cumbersome, use a class such as v ec to r (§3.7.1, §16.3) ve ct or instead. For example: v oi d g vo id g() { v ec to r<T ab le ve ct or Ta bl e>* p 1 = n ew v ec to r<T ab le 10 ; p1 ne w ve ct or Ta bl e>(1 0) T ab le p 2 = n ew T ab le Ta bl e* p2 ne w Ta bl e; d el et e p 1; de le te p1 d el et e p 2; de le te p2 } 10.4.8 Local Static Store [class.obj.static] The constructor for a local static object (§7.1.2) is called the first time the thread of control passes through the object’s definition. Consider this: v oi d f in t i vo id f(i nt i) { s ta ti c T ab le t bl st at ic Ta bl e tb l; // ... i f (i { if i) s ta ti c T ab le t bl 2; st at ic Ta bl e tb l2 // ... } }
    262. 252 Classes Chapter 10 i nt m ai n() in t ma in { f 0); f(0 f 1); f(1 f 2); f(2 // ... } Here, the constructor is called for t bl once the first time f tb l f() is called. Because t bl is declared tb l s ta ti c, it does not get destroyed on return from f st at ic f() and it does not get constructed a second time when f is called again. Because the block containing the declaration of t bl 2 doesn’t get executed f() tb l2 for the call f 0), t bl 2 doesn’t get constructed until the call f 1). It does not get constructed again f(0 tb l2 f(1 when its block is entered a second time. The destructors for local static objects are invoked in the reverse order of their construction when the program terminates (§9.4.1.1). Exactly when is unspecified. 10.4.9 Nonlocal Store [class.global] A variable defined outside any function (that is, global, namespace, and class s ta ti c variables) is st at ic initialized (constructed) before m ai n() is invoked, and any such variable that has been constructed ma in will have its destructor invoked after exit from m ai n(). Dynamic linking complicates this picture ma in slightly by delaying the initialization until the code is linked into the running program. Constructors for nonlocal objects in a translation unit are executed in the order their definitions occur. Consider: c la ss X { cl as s // ... s ta ti c T ab le m em tb l; st at ic Ta bl e me mt bl }; T ab le t bl Ta bl e tb l; T ab le X :m em tb l; Ta bl e X: me mt bl n am es pa ce Z { na me sp ac e T ab le t bl 2; Ta bl e tb l2 } The order of construction is t bl then X :m em tb l, and then Z :t bl 2. Note that a declaration (as tb l, X: me mt bl Z: tb l2 opposed to a definition), such as the declaration of m em tb l in X doesn’t affect the order of con- me mt bl X, struction. The destructors are called in the reverse order of construction: Z :t bl 2, then Z: tb l2 X :m em tb l, and then t bl X: me mt bl tb l. No implementation-independent guarantees are made about the order of construction of nonlo- cal objects in different compilation units. For example: // file1.c: T ab le t bl 1; Ta bl e tb l1 // file2.c: T ab le t bl 2; Ta bl e tb l2
    263. Section 10.4.9 Nonlocal Store 253 Whether t bl 1 is constructed before t bl 2 or vice versa is implementation-dependent. The order isn’t tb l1 tb l2 even guaranteed to be fixed in every particular implementation. Dynamic linking, or even a small change in the compilation process, can alter the sequence. The order of destruction is similarly implementation-dependent. Sometimes when you design a library, it is necessary, or simply convenient, to invent a type with a constructor and a destructor with the sole purpose of initialization and cleanup. Such a type would be used once only: to allocate a static object so that the constructor and the destructor are called. For example: cl as s Zl ib _i ni t c la ss Z li b_ in it { Zl ib _i ni t() Z li b_ in it ; // get Zlib ready for use Zl ib _i ni t() ~Z li b_ in it ; // clean up after Zlib }; c la ss Z li b { cl as s Zl ib st at ic Zl ib _i ni t x; s ta ti c Z li b_ in it x // ... }; Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units. A particular C++ implemen- tation may provide such a guarantee, but most don’t. A programmer may ensure proper initial- ization by implementing the strategy that the implementations usually employ for local static objects: a first-time switch. For example: c la ss Z li b { cl as s Zl ib s ta ti c b oo l i ni ti al iz ed st at ic bo ol in it ia li ze d; s ta ti c v oi d i ni ti al iz e() { /* initialize */ i ni ti al iz ed = t ru e; } st at ic vo id in it ia li ze in it ia li ze d tr ue p ub li c: pu bl ic // no constructor v oi d f vo id f() { i f (i ni ti al iz ed == f al se i ni ti al iz e(); if in it ia li ze d fa ls e) in it ia li ze // ... } // ... }; If there are many functions that need to test the first-time switch, this can be tedious, but it is often manageable. This technique relies on the fact that statically allocated objects without constructors are initialized to 0 The really difficult case is the one in which the first operation may be time- 0. critical so that the overhead of testing and possible initialization can be serious. In that case, further trickery is required (§21.5.2). An alternative approach for a simple object is to present it as a function (§9.4.1): i nt o bj in t& ob j() { s ta ti c i nt x = 0 r et ur n x } // initialized upon first use st at ic in t 0; re tu rn x; First-time switches do not handle every conceivable situation. For example, it is possible to create objects that refer to each other during construction. Such examples are best avoided. If such
    264. 254 Classes Chapter 10 objects are necessary, they must be constructed carefully in stages. Also, there is no similarly sim- ple last-time switch construct. Instead, see §9.4.1.1 and §21.5.2. 10.4.10 Temporary Objects [class.temp] Temporary objects most often are the result of arithmetic expressions. For example, at some point in the evaluation of x y+z the partial result x y must exist somewhere. Except when performance x*y z x*y is the issue (§11.6), temporary objects rarely become the concern of the programmer. However, it happens (§11.6, §22.4.7). Unless bound to a reference or used to initialize a named object, a temporary object is destroyed at the end of the full expression in which it was created. A full expression is an expression that is not a subexpression of some other expression. The standard s tr in g class has a member function c _s tr that returns a C-style, zero-terminated st ri ng c_ st r() array of characters (§3.5.1, §20.4.1). Also, the operator + is defined to mean string concatenation. These are very useful facilities for s tr in gs However, in combination they can cause obscure prob- st ri ng s. lems. For example: v oi d f st ri ng s 1, s tr in g& s 2, s tr in g& s 3) vo id f(s tr in g& s1 st ri ng s2 st ri ng s3 { c on st c ha r* c s = (s 1+s 2).c _s tr ; co ns t ch ar cs s1 s2 c_ st r() c ou t << c s; co ut cs i f (s tr le n(c s=(s 2+s 3).c _s tr if st rl en cs s2 s3 c_ st r())<8 && c s[0 8 cs 0]==´a a´) { // cs used here } } Probably, your first reaction is ‘‘but don’t do that,’’ and I agree. However, such code does get writ- ten, so it is worth knowing how it is interpreted. A temporary object of class s tr in g is created to hold s 1+s 2. Next, a pointer to a C-style string st ri ng s1 s2 is extracted from that object. Then – at the end of the expression – the temporary object is deleted. Now, where was the C-style string allocated? Probably as part of the temporary object holding s 1+s 2, and that storage is not guaranteed to exist after that temporary is destroyed. Consequently, s1 s2 c s points to deallocated storage. The output operation c ou t<<c s might work as expected, but that cs co ut cs would be sheer luck. A compiler can detect and warn against many variants of this problem. The example with the if-statement is a bit more subtle. The condition will work as expected because the full expression in which the temporary holding s 2+s 3 is created is the condition itself. s2 s3 However, that temporary is destroyed before the controlled statement is entered, so any use of c s cs there is not guaranteed to work. Please note that in this case, as in many others, the problems with temporaries arose from using a high-level data type in a low-level way. A cleaner programming style would have not only yielded a more understandable program fragment, but also avoided the problems with temporaries completely. For example:
    265. Section 10.4.10 Temporary Objects 255 v oi d f st ri ng s 1, s tr in g& s 2, s tr in g& s 3) vo id f(s tr in g& s1 st ri ng s2 st ri ng s3 { c ou t << s 1+s 2; co ut s1 s2 s tr in g s = s 2+s 3; st ri ng s2 s3 i f (s le ng th if s.l en gt h()<8 && s 0]==´a 8 s[0 a´) { // use s here } } A temporary can be used as an initializer for a c on st reference or a named object. For example: co ns t v oi d g co ns t s tr in g&, c on st s tr in g&); vo id g(c on st st ri ng co ns t st ri ng v oi d h st ri ng s 1, s tr in g& s 2) vo id h(s tr in g& s1 st ri ng s2 { c on st s tr in g& s = s 1+s 2; co ns t st ri ng s1 s2 s tr in g s s = s 1+s 2; st ri ng ss s1 s2 g s,s s); // we can use s and ss here g(s ss } This is fine. The temporary is destroyed when ‘‘its’’ reference or named object go out of scope. Remember that returning a reference to a local variable is an error (§7.3) and that a temporary object cannot be bound to a non-c on st reference (§5.5). co ns t A temporary object can also be created by explicitly invoking a constructor. For example: v oi d f Sh ap e& s i nt x i nt y vo id f(S ha pe s, in t x, in t y) { s mo ve Po in t(x y)); s.m ov e(P oi nt x,y // construct Point to pass to Shape::move() // ... } Such temporaries are destroyed in exactly the same way as the implicitly generated temporaries. 10.4.11 Placement of Objects [class.placement] Operator n ew creates its object on the free store by default. What if we wanted the object allocated ne w elsewhere? Consider a simple class: c la ss X { cl as s p ub li c: pu bl ic X in t); X(i nt // ... }; We can place objects anywhere by providing an allocator function with extra arguments and then supplying such extra arguments when using n ew ne w: v oi d* o pe ra to r n ew si ze _t v oi d* p { r et ur n p } vo id op er at or ne w(s iz e_ t, vo id p) re tu rn p; // explicit placement operator v oi d* b uf = r ei nt er pr et _c as t<v oi d*>(0 xF 00 F); // significant address vo id bu f re in te rp re t_ ca st vo id 0x F0 0F X p 2 = n ew bu f)X // construct an X at ‘buf;’ invokes: operator new(sizeof(X),buf) X* p2 ne w(b uf X;
    266. 256 Classes Chapter 10 Because of this usage, the n ew bu f)X syntax for supplying extra arguments to o pe ra to r n ew ne w(b uf X op er at or ne w() is known as the placement syntax. Note that every o pe ra to r n ew op er at or ne w() takes a size as its first argument and that the size of the object allocated is implicitly supplied (§15.6). The o pe ra to r n ew op er at or ne w() used by the n ew operator is chosen by the usual argument matching rules (§7.4); every o pe ra to r n ew ne w op er at or ne w() si ze _t has a s iz e_ t as its first argument. The ‘‘placement’’ o pe ra to r n ew op er at or ne w() is the simplest such allocator. It is defined in the standard header <n ew ne w>. The r ei nt er pr et _c as t is the crudest and potentially nastiest of the type conversion operators re in te rp re t_ ca st (§6.2.7). In most cases, it simply yields a value with the same bit pattern as its argument with the type required. Thus, it can be used for the inherently implementation-dependent, dangerous, and occasionally absolutely necessary activity of converting integer values to pointers and vice versa. The placement n ew construct can also be used to allocate memory from a specific arena: ne w c la ss A re na { cl as s Ar en a p ub li c: pu bl ic v ir tu al v oi d* a ll oc si ze _t =0 vi rt ua l vo id al lo c(s iz e_ t) 0; v ir tu al v oi d f re e(v oi d*) =0 vi rt ua l vo id fr ee vo id 0; // ... }; v oi d* o pe ra to r n ew si ze _t s z, A re na a vo id op er at or ne w(s iz e_ t sz Ar en a* a) { r et ur n a al lo c(s z); re tu rn a->a ll oc sz } Now objects of arbitrary types can be allocated from different A re na as needed. For example: Ar en as e xt er n A re na P er si st en t; ex te rn Ar en a* Pe rs is te nt e xt er n A re na S ha re d; ex te rn Ar en a* Sh ar ed v oi d g in t i vo id g(i nt i) { X p = n ew Pe rs is te nt X i); X* ne w(P er si st en t) X(i // X in persistent storage X q = n ew Sh ar ed X i); X* ne w(S ha re d) X(i // X in shared memory // ... } Placing an object in an area that is not (directly) controlled by the standard free-store manager implies that some care is required when destroying the object. The basic mechanism for that is an explicit call of a destructor: v oi d d es tr oy X* p A re na a vo id de st ro y(X p, Ar en a* a) { p p->~X ; X() // call destructor a fr ee p); a->f re e(p // free memory } Note that explicit calls of destructors, like the use of special-purpose global allocators, should be avoided wherever possible. Occasionally, they are essential. For example, it would be hard to implement an efficient general container along the lines of the standard library v ec to r (§3.7.1, ve ct or §16.3.8) without using explicit destructor class. However, a novice should think thrice before
    267. Section 10.4.11 Placement of Objects 257 calling a destructor explicitly and also should ask a more experienced colleague before doing so. See §14.4.7 for an explanation of how placement new interacts with exception handling. There is no special syntax for placement of arrays. Nor need there be, since arbitrary types can be allocated by placement new. However, a special o pe ra to r d el et e() can be defined for arrays op er at or de le te (§19.4.5). 10.4.12 Unions [class.union] A named union is defined as a s tr uc t, where every member has the same address (see §C.8.2). A st ru ct union can have member functions but not static members. In general, a compiler cannot know what member of a union is used; that is, the type of the object stored in a union is unknown. Consequently, a union may not have members with construc- tors or destructors. It wouldn’t be possible to protect that object against corruption or to guarantee that the right destructor is called when the union goes out of scope. Unions are best used in low-level code, or as part of the implementation of classes that keep track of what is stored in the union (see §10.6[20]). 10.5 Advice [class.advice] [1] Represent concepts as classes; §10.1. [2] Use public data (s tr uc ts) only when it really is just data and no invariant is meaningful for the st ru ct data members; §10.2.8. [3] A concrete type is the simplest kind of class. Where applicable, prefer a concrete type over more complicated classes and over plain data structures; §10.3. [4] Make a function a member only if it needs direct access to the representation of a class; §10.3.2. [5] Use a namespace to make the association between a class and its helper functions explicit; §10.3.2. [6] Make a member function that doesn’t modify the value of its object a c on st member function; co ns t §10.2.6. [7] Make a function that needs access to the representation of a class but needn’t be called for a specific object a s ta ti c member function; §10.2.4. st at ic [8] Use a constructor to establish an invariant for a class; §10.3.1. [9] If a constructor acquires a resource, its class needs a destructor to release the resource; §10.4.1. [10] If a class has a pointer member, it needs copy operations (copy constructor and copy assign- ment); §10.4.4.1. [11] If a class has a reference member, it probably needs copy operations (copy constructor and copy assignment); §10.4.6.3. [12] If a class needs a copy operation or a destructor, it probably needs a constructor, a destructor, a copy assignment, and a copy constructor; §10.4.4.1. [13] Check for self-assignment in copy assignments; §10.4.4.1. [14] When writing a copy constructor, be careful to copy every element that needs to be copied (beware of default initializers); §10.4.4.1.
    268. 258 Classes Chapter 10 [15] When adding a new member to a class, always check to see if there are user-defined construc- tors that need to be updated to initialize the member; §10.4.6.3. [16] Use enumerators when you need to define integer constants in class declarations; §10.4.6.1. [17] Avoid order dependencies when constructing global and namespace objects; §10.4.9. [18] Use first-time switches to minimize order dependencies; §10.4.9. [19] Remember that temporary objects are destroyed at the end of the full expression in which they are created; §10.4.10. 10.6 Exercises [class.exercises] 1. (∗1) Find the error in D at e::a dd _y ea r() in §10.2.2. Then find two additional errors in the Da te ad d_ ye ar version in §10.2.7. 2. (∗2.5) Complete and test D at e. Reimplement it with ‘‘number of days after 1/1/1970’’ repre- Da te sentation. 3. (∗2) Find a D at e class that is in commercial use. Critique the facilities it offers. If possible, Da te then discuss that D at e with a real user. Da te 4. (∗1) How do you access s et _d ef au lt from class D at e from namespace C hr on o (§10.3.2)? Give se t_ de fa ul t Da te Ch ro no at least three different ways. 5. (∗2) Define a class H is to gr am that keeps count of numbers in some intervals specified as argu- Hi st og ra m ments to H is to gr am constructor. Provide functions to print out the histogram. Handle out- Hi st og ra m’s of-range values. 6. (∗2) Define some classes for providing random numbers of certain distributions (for example, uniform and exponential). Each class has a constructor specifying parameters for the distribu- tion and a function d ra w that returns the next value. dr aw 7. (∗2.5) Complete class T ab le to hold (name,value) pairs. Then modify the desk calculator pro- Ta bl e gram from §6.1 to use class T ab le instead of m ap Compare and contrast the two versions. Ta bl e ma p. 8. (∗2) Rewrite T no de from §7.10[7] as a class with constructors, destructors, etc. Define a tree of Tn od e T no de as a class with constructors, destructors, etc. Tn od es 9. (∗3) Define, implement, and test a set of integers, class I nt se t. Provide union, intersection, and In ts et symmetric difference operations. 10. (∗1.5) Modify class I nt se t into a set of nodes, where N od e is a structure you define. In ts et No de 11. (∗3) Define a class for analyzing, storing, evaluating, and printing simple arithmetic expressions consisting of integer constants and the operators +, -, *, and /. The public interface should look like this: c la ss E xp r { cl as s Ex pr // ... p ub li c: pu bl ic E xp r(c ha r*); Ex pr ch ar i nt e va l(); in t ev al v oi d p ri nt ; vo id pr in t() }; The string argument for the constructor E xp r::E xp r() is the expression. The function Ex pr Ex pr E xp r::e va l() returns the value of the expression, and E xp r::p ri nt Ex pr ev al Ex pr pr in t() prints a representation
    269. Section 10.6 Exercises 259 of the expression on c ou t. A program might look like this: co ut E xp r x 12 3/4 12 3*4 3\"); Ex pr x(\"1 23 4+1 23 4-3 c ou t << \"x = \" << x ev al co ut x x.e va l() << \"\\ n\"; \\n x pr in t(); x.p ri nt Define class E xp r twice: once using a linked list of nodes as the representation and once using a Ex pr character string as the representation. Experiment with different ways of printing the expres- sion: fully parenthesized, postfix notation, assembly code, etc. Ch ar _q ue ue 12. (∗2) Define a class C ha r_ qu eu e so that the public interface does not depend on the representa- Ch ar _q ue ue tion. Implement C ha r_ qu eu e (a) as a linked list and (b) as a vector. Do not worry about con- currency. 13. (∗3) Design a symbol table class and a symbol table entry class for some language. Have a look at a compiler for that language to see what the symbol table really looks like. 14. (∗2) Modify the expression class from §10.6[11] to handle variables and the assignment opera- tor =. Use the symbol table class from §10.6[13]. 15. (∗1) Given this program: #i nc lu de <i os tr ea m> in cl ud e io st re am i nt m ai n() in t ma in { s td :c ou t << \"H el lo w or ld \\n st d: co ut He ll o, wo rl d!\\ n\"; } modify it to produce this output: I ni ti al iz e In it ia li ze H el lo w or ld He ll o, wo rl d! C le an u p Cl ea n up Do not change m ai n() in any way. ma in 16. (∗2) Define a C al cu la to r class for which the calculator functions from §6.1 provide most of the Ca lc ul at or implementation. Create C al cu la to rs and invoke them for input from c in from command-line Ca lc ul at or ci n, arguments, and for strings in the program. Allow output to be delivered to a variety of targets similar to the way input can be obtained from a variety of sources. 17. (∗2) Define two classes, each with a s ta ti c member, so that the construction of each s ta ti c st at ic st at ic member involves a reference to the other. Where might such constructs appear in real code? How can these classes be modified to eliminate the order dependence in the constructors? 18. (∗2.5) Compare class D at e (§10.3) with your solution to §5.9[13] and §7.10[19]. Discuss errors Da te found and likely differences in maintenance of the two solutions. 19. (∗3) Write a function that, given an i st re am and a v ec to r<s tr in g>, produces a is tr ea m ve ct or st ri ng m ap st ri ng ve ct or in t>> holding each string and the numbers of the lines on which the string ma p<s tr in g,v ec to r<i nt appears. Run the program on a text-file with no fewer than 1,000 lines looking for no fewer than 10 words. 20. (∗2) Take class E nt ry from §C.8.2 and modify it so that each union member is always used En tr y according to its type. .
    270. 260 Classes Chapter 10
    271. ________________________________________ ________________________________________________________________________________________________________________________________________________________________ 11 ________________________________________ ________________________________________________________________________________________________________________________________________________________________ Operator Overloading When I use a word it means just what I choose it to mean – neither more nor less. – Humpty Dumpty Notation — operator functions — binary and unary operators — predefined meanings for operators — user-defined meanings for operators — operators and namespaces — a complex type — member and nonmember operators — mixed-mode arithmetic — initialization — copying — conversions — literals — helper functions — conversion operators — ambiguity resolution — friends — members and friends — large objects — assignment and initialization — subscripting — function call — dereferencing — incre- ment and decrement — a string class — advice — exercises. 11.1 Introduction [over.intro] Every technical field – and most nontechnical fields – have developed conventional shorthand notation to make convenient the presentation and discussion involving frequently-used concepts. For example, because of long acquaintance x y*z x+y z is clearer to us than m ul ti pl y y b y z a nd a dd t he r es ul t t o x mu lt ip ly by an d ad d th e re su lt to It is hard to overestimate the importance of concise notation for common operations. Like most languages, C++ supports a set of operators for its built-in types. However, most con- cepts for which operators are conventionally used are not built-in types in C++, so they must be rep- resented as user-defined types. For example, if you need complex arithmetic, matrix algebra, logic signals, or character strings in C++, you use classes to represent these notions. Defining operators
    272. 262 Operator Overloading Chapter 11 for such classes sometimes allows a programmer to provide a more conventional and convenient notation for manipulating objects than could be achieved using only the basic functional notation. For example, c la ss c om pl ex { cl as s co mp le x // very simplified complex d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex do ub le r d ou bl e i : r e(r , i m(i { } co mp le x(d ou bl e r, do ub le i) re r) im i) c om pl ex o pe ra to r+(c om pl ex ; co mp le x op er at or co mp le x) c om pl ex o pe ra to r*(c om pl ex ; co mp le x op er at or co mp le x) }; defines a simple implementation of the concept of complex numbers. A c om pl ex is represented by co mp le x a pair of double-precision floating-point numbers manipulated by the operators + and *. The pro- grammer defines c om pl ex :o pe ra to r+() and c om pl ex :o pe ra to r*() to provide meanings for + co mp le x: op er at or co mp le x: op er at or and *, respectively. For example, if b and c are of type c om pl ex b c means b op er at or c). co mp le x, b+c b.o pe ra to r+(c We can now approximate the conventional interpretation of c om pl ex expressions: co mp le x v oi d f vo id f() { c om pl ex a = c om pl ex 1, 3 1); co mp le x co mp le x(1 3.1 c om pl ex b = c om pl ex 1.2 2 ; co mp le x co mp le x(1 2, 2) c om pl ex c = b co mp le x b; a = b c; b+c b = b c*a b+c a; c = a b+c om pl ex 1,2 ; a*b co mp le x(1 2) } The usual precedence rules hold, so the second statement means b b+(c a), not b b+c a. b=b c*a b=(b c)*a Many of the most obvious uses of operator overloading are for concrete types (§10.3). How- ever, the usefulness of user-defined operators is not restricted to concrete types. For example, the design of general and abstract interfaces often leads to the use of operators such as ->, [], and (). 11.2 Operator Functions [over.oper] Functions defining meanings for the following operators (§6.2) can be declared: + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , n ew ne w n ew ne w[] d el et e de le te d el et e[] de le te -> [] () The following operators cannot be defined by a user: :: (scope resolution; §4.9.4, §10.2.4), . (member selection; §5.7), and .* (member selection through pointer to function; §15.5).
    273. Section 11.2 Operator Functions 263 They take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties [Stroustrup,1994]. It is not possible to define new operator tokens, but you can use the function-call notation when this set of operators is not adequate. For example, use p owpo w(), not **. These restrictions may seem Draconian, but more flexible rules can easily lead to ambiguities. For example, defining an operator ** to mean exponentiation may seem an obvious and easy task at first glance, but think again. Should ** bind to the left (as in Fortran) or to the right (as in Algol)? Should the expres- sion a p be interpreted as a a**p a*(*p or as (a p) a)**(p p)? The name of an operator function is the keyword o pe ra to r followed by the operator itself; for op er at or example, o pe ra to r<<. An operator function is declared and can be called like any other function. op er at or A use of the operator is only a shorthand for an explicit call of the operator function. For example: v oi d f co mp le x a c om pl ex b vo id f(c om pl ex a, co mp le x b) { c om pl ex c = a + b co mp le x b; // shorthand c om pl ex d = a op er at or b); co mp le x a.o pe ra to r+(b // explicit call } Given the previous definition of c om pl ex the two initializers are synonymous. co mp le x, 11.2.1 Binary and Unary Operators [over.binary] A binary operator can be defined by either a nonstatic member function taking one argument or a nonmember function taking two arguments. For any binary operator @, a a@b b can be interpreted as aa bb either a a.o pe ra to r@(b b) or o pe ra to r@(a a,b b). If both are defined, overload resolution (§7.4) aa op er at or bb op er at or aa bb determines which, if any, interpretation is used. For example: c la ss X { cl as s p ub li c: pu bl ic v oi d o pe ra to r+(i nt ; vo id op er at or in t) X in t); X(i nt }; v oi d o pe ra to r+(X X); vo id op er at or X,X v oi d o pe ra to r+(X do ub le ; vo id op er at or X,d ou bl e) v oi d f X a vo id f(X a) { a 1; a+1 // a.operator+(1) 1 a; 1+a // ::operator+(X(1),a) a 1.0 a+1 0; // ::operator+(a,1.0) } A unary operator, whether prefix or postfix, can be defined by either a nonstatic member function taking no arguments or a nonmember function taking one argument. For any prefix unary operator @, @a a can be interpreted as either a a.o pe ra to r@() or o pe ra to r@(a a). If both are defined, over- aa aa op er at or op er at or aa load resolution (§7.4) determines which, if any, interpretation is used. For any postfix unary opera- tor @, a a@ can be interpreted as either a a.o pe ra to r@(i nt or o pe ra to r@(a a,i nt aa aa op er at or in t) op er at or aa in t). This is explained further in §11.11. If both are defined, overload resolution (§7.4) determines which, if
    274. 264 Operator Overloading Chapter 11 any, interpretation is used. An operator can be declared only for the syntax defined for it in the grammar (§A.5). For example, a user cannot define a unary % or a ternary +. Consider: c la ss X { cl as s // members (with implicit ‘this’ pointer): X o pe ra to r&(); X* op er at or // prefix unary & (address of) X o pe ra to r&(X ; op er at or X) // binary & (and) X o pe ra to r++(i nt ; op er at or in t) // postfix increment (see §11.11) X o pe ra to r&(X X); op er at or X,X // error: ternary X o pe ra to r/(); op er at or // error: unary / }; // nonmember functions : X o pe ra to r-(X ; op er at or X) // prefix unary minus X o pe ra to r-(X X); op er at or X,X // binary minus X o pe ra to r--(X in t); op er at or X&,i nt // postfix decrement X o pe ra to r-(); op er at or // error: no operand X o pe ra to r-(X X,X ; op er at or X,X X) // error: ternary X o pe ra to r%(X ; op er at or X) // error: unary % Operator [] is described in §11.8, operator () in §11.9, operator -> in §11.10, operators ++ and -- in §11.11, and the allocation and deallocation operators in §6.2.6.2, §10.4.11, and §15.6. 11.2.2 Predefined Meanings for Operators [over.predefined] Only a few assumptions are made about the meaning of a user-defined operator. In particular, o pe ra to r=, o pe ra to r[], o pe ra to r(), and o pe ra to r-> must be nonstatic member functions; this op er at or op er at or op er at or op er at or ensures that their first operands will be lvalues (§4.9.6). The meanings of some built-in operators are defined to be equivalent to some combination of other operators on the same arguments. For example, if a is an int, ++a means a 1, which in turn a a+=1 means a a+1 Such relations do not hold for user-defined operators unless the user happens to a=a 1. define them that way. For example, a compiler will not generate a definition of Z :o pe ra to r+=() Z: op er at or from the definitions of Z :o pe ra to r+() and Z :o pe ra to r=(). Z: op er at or Z: op er at or Because of historical accident, the operators = (assignment), & (address-of), and , (sequencing; §6.2.2) have predefined meanings when applied to class objects. These predefined meanings can be made inaccessible to general users by making them private: c la ss X { cl as s p ri va te pr iv at e: v oi d o pe ra to r=(c on st X ; vo id op er at or co ns t X&) v oi d o pe ra to r&(); vo id op er at or v oi d o pe ra to r,(c on st X ; vo id op er at or co ns t X&) // ... };
    275. Section 11.2.2 Predefined Meanings for Operators 265 v oi d f X a X vo id f(X a, b b) { a=b b; // error: operator= private a; // error: operator& private &a a b; a,b // error: operator, private } Alternatively, they can be given new meanings by suitable definitions. 11.2.3 Operators and User-Defined Types [over.user] An operator function must either be a member or take at least one argument of a user-defined type (functions redefining the n ew and d el et e operators need not). This rule ensures that a user cannot ne w de le te change the meaning of an expression unless the expression contains an object of a user-defined type. In particular, it is not possible to define an operator function that operates exclusively on pointers. This ensures that C++ is extensible but not mutable (with the exception of operators =, &, and , for class objects). An operator function intended to accept a basic type as its first operand cannot be a member function. For example, consider adding a complex variable a a to the integer 2 a a+2 can, with a aa 2: aa 2 suitably declared member function, be interpreted as a a.o pe ra to r+(2 but 2 aa cannot because aa op er at or 2), 2+a a there is no class i nt for which to define + to mean 2 op er at or aa Even if there were, two dif- in t 2.o pe ra to r+(a a). ferent member functions would be needed to cope with 2 aa and a a+2 Because the compiler does 2+a a aa 2. not know the meaning of a user-defined +, it cannot assume that it is commutative and so interpret 2 aa as a a+2 This example is trivially handled using nonmember functions (§11.3.2, §11.5). 2+a a aa 2. Enumerations are user-defined types so that we can define operators for them. For example: e nu m D ay { s un m on t ue w ed t hu f ri s at }; en um Da y su n, mo n, tu e, we d, th u, fr i, sa t D ay o pe ra to r++(D ay d Da y& op er at or Da y& d) { r et ur n d = (s at d) ? s un : D ay d+1 ; re tu rn sa t==d su n Da y(d 1) } Every expression is checked for ambiguities. Where a user-defined operator provides a possible interpretation, the expression is checked according to the rules in §7.4. 11.2.4 Operators in Namespaces [over.namespace] An operator is either a member of a class or defined in some namespace (possibly the global name- space). Consider this simplified version of string I/O from the standard library: n am es pa ce s td { na me sp ac e st d // simplified std c la ss o st re am { cl as s os tr ea m // ... o st re am o pe ra to r<<(c on st c ha r*); os tr ea m& op er at or co ns t ch ar }; e xt er n o st re am c ou t; ex te rn os tr ea m co ut
    276. 266 Operator Overloading Chapter 11 c la ss s tr in g { cl as s st ri ng // ... }; o st re am o pe ra to r<<(o st re am os tr ea m& op er at or os tr ea m&, c on st s tr in g&); co ns t st ri ng } i nt m ai n() in t ma in { c ha r* p = \"H el lo ch ar He ll o\"; s td :s tr in g s = \"w or ld st d: st ri ng wo rl d\"; s td :c ou t << p << \", \" << s << \"!\\ n\"; st d: co ut \\n } Naturally, this writes out H el lo w or ld But why? Note that I didn’t make everything from s td He ll o, wo rl d! st d accessible by writing: u si ng n am es pa ce s td us in g na me sp ac e st d; Instead, I used the s td : prefix for s tr in g and c ou t. In other words, I was at my best behavior and st d: st ri ng co ut didn’t pollute the global namespace or in other ways introduce unnecessary dependencies. The output operator for C-style strings (c ha r*) is a member of s td :o st re am so by definition ch ar st d: os tr ea m, s td :c ou t << p st d: co ut means s td :c ou t.o pe ra to r<<(p st d: co ut op er at or p) However, s td :o st re am doesn’t have a member function to output a s td :s tr in g, so st d: os tr ea m st d: st ri ng s td :c ou t << s st d: co ut means o pe ra to r<<(s td :c ou t,s op er at or st d: co ut s) Operators defined in namespaces can be found based on their operand types just like functions can be found based on their argument types (§8.2.6). In particular, c ou t is in namespace s td so s td is co ut st d, st d considered when looking for a suitable definition of <<. In that way, the compiler finds and uses: s td :o pe ra to r<<(s td :o st re am st d: op er at or st d: os tr ea m&, c on st s td :s tr in g&) co ns t st d: st ri ng For a binary operator @, x y where x is of type X and y is of type Y is resolved like this: x@y [1] If X is a class, determine whether class X or a base of X defines o pe ra to r@ as a member; if op er at or so, that is the @ to try to use. [2] Otherwise, – look for declarations of @ in the context surrounding x y; and x@y – if X is defined in namespace N look for declarations of @ in N and N, N; – if Y is defined in namespace M look for declarations of @ in M M, M. If declarations of o pe ra to r@ are found in the surrounding context, in N or in M we try to use op er at or N, M, those operators. In either case, declarations for several o pe ra to r@s may be found and overload resolution rules op er at or
    277. Section 11.2.4 Operators in Namespaces 267 (§7.4) are used to find the best match, if any. This lookup mechanism is applied only if the opera- tor has at least one operand of a user-defined type. Therefore, user-defined conversions (§11.3.2, §11.4) will be considered. Note that a t yp ed ef name is just a synonym and not a user-defined type ty pe de f (§4.9.7). 11.3 A Complex Number Type [over.complex] The implementation of complex numbers presented in the introduction is too restrictive to please anyone. For example, from looking at a math textbook we would expect this to work: v oi d f vo id f() { c om pl ex a = c om pl ex 1,2 ; co mp le x co mp le x(1 2) c om pl ex b = 3 co mp le x 3; c om pl ex c = a 2.3 co mp le x a+2 3; c om pl ex d = 2 b; co mp le x 2+b c om pl ex e = -b c; co mp le x b-c b = c 2*c c*2 c; } In addition, we would expect to be provided with a few additional operators, such as == for com- parison and << for output, and a suitable set of mathematical functions, such as s in and s qr t(). si n() sq rt Class c om pl ex is a concrete type, so its design follows the guidelines from §10.3. In addition, co mp le x users of complex arithmetic rely so heavily on operators that the definition of c om pl ex brings into co mp le x play most of the basic rules for operator overloading. 11.3.1 Member and Nonmember Operators [over.member] I prefer to minimize the number of functions that directly manipulate the representation of an object. This can be achieved by defining only operators that inherently modify the value of their first argument, such as +=, in the class itself. Operators that simply produce a new value based on the values of its arguments, such as +, are then defined outside the class and use the essential opera- tors in their implementation: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex o pe ra to r+=(c om pl ex a ; co mp le x& op er at or co mp le x a) // needs access to representation // ... }; c om pl ex o pe ra to r+(c om pl ex a c om pl ex b co mp le x op er at or co mp le x a, co mp le x b) { c om pl ex r = a co mp le x a; r et ur n r += b // access representation through += re tu rn b; } Given these declarations, we can write:
    278. 268 Operator Overloading Chapter 11 v oi d f co mp le x x c om pl ex y c om pl ex z vo id f(c om pl ex x, co mp le x y, co mp le x z) { c om pl ex r 1 = x y+z // r1 = operator+(x,operator+(y,z)) co mp le x r1 x+y z; c om pl ex r 2 = x co mp le x r2 x; // r2 = x r 2 += y r2 y; // r2.operator+=(y) r 2 += z r2 z; // r2.operator+=(z) } Except for possible efficiency differences, the computations of r 1 and r 2 are equivalent. r1 r2 Composite assignment operators such as += and *= tend to be simpler to define than their ‘‘simple’’ counterparts + and *. This surprises most people at first, but it follows from the fact that three objects are involved in a + operation (the two operands and the result), whereas only two objects are involved in a += operation. In the latter case, run-time efficiency is improved by elimi- nating the need for temporary variables. For example: i nl in e c om pl ex c om pl ex :o pe ra to r+=(c om pl ex a in li ne co mp le x& co mp le x: op er at or co mp le x a) { r e += a re re a.r e; i m += a im im a.i m; r et ur n *t hi s; re tu rn th is } does not require a temporary variable to hold the result of the addition and is simple for a compiler to inline perfectly. A good optimizer will generate close to optimal code for uses of the plain + operator also. However, we don’t always have a good optimizer and not all types are as simple as c om pl ex so co mp le x, §11.5 discusses ways of defining operators with direct access to the representation of classes. 11.3.2 Mixed-Mode Arithmetic [over.mixed] To cope with c om pl ex d = 2 b; co mp le x 2+b we need to define operator + to accept operands of different types. In Fortran terminology, we need mixed-mode arithmetic. We can achieve that simply by adding appropriate versions of the operators: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex o pe ra to r+=(c om pl ex a { co mp le x& op er at or co mp le x a) r e += a re re a.r e; i m += a im im a.i m; r et ur n *t hi s; re tu rn th is }
    279. Section 11.3.2 Mixed-Mode Arithmetic 269 c om pl ex o pe ra to r+=(d ou bl e a { co mp le x& op er at or do ub le a) r e += a re a; r et ur n *t hi s; re tu rn th is } // ... }; c om pl ex o pe ra to r+(c om pl ex a c om pl ex b co mp le x op er at or co mp le x a, co mp le x b) { c om pl ex r = a co mp le x a; r et ur n r += b // calls complex::operator+=(complex) re tu rn b; } c om pl ex o pe ra to r+(c om pl ex a d ou bl e b co mp le x op er at or co mp le x a, do ub le b) { c om pl ex r = a co mp le x a; r et ur n r += b // calls complex::operator+=(double) re tu rn b; } c om pl ex o pe ra to r+(d ou bl e a c om pl ex b co mp le x op er at or do ub le a, co mp le x b) { c om pl ex r = b co mp le x b; r et ur n r += a // calls complex::operator+=(double) re tu rn a; } Adding a d ou bl e to a complex number is a simpler operation than adding a c om pl ex This is do ub le co mp le x. reflected in these definitions. The operations taking d ou bl e operands do not touch the imaginary do ub le part of a complex number and thus will be more efficient. Given these declarations, we can write: v oi d f co mp le x x c om pl ex y vo id f(c om pl ex x, co mp le x y) { c om pl ex r 1 = x y; // calls operator+(complex,complex) co mp le x r1 x+y c om pl ex r 2 = x 2; // calls operator+(complex,double) co mp le x r2 x+2 c om pl ex r 3 = 2 x; // calls operator+(double,complex) co mp le x r3 2+x } 11.3.3 Initialization [over.ctor] To cope with assignments and initialization of c om pl ex variables with scalars, we need a conver- co mp le x sion of a scalar (integer or floating-point number) to a c om pl ex For example: co mp le x. c om pl ex b = 3 // should mean b.re=3, b.im=0 co mp le x 3; A constructor taking a single argument specifies a conversion from its argument type to the constructor’s type. For example:
    280. 270 Operator Overloading Chapter 11 c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex do ub le r :r e(r , i m(0 { } co mp le x(d ou bl e r) re r) im 0) // ... }; The constructor specifies the traditional embedding of the real line in the complex plane. A constructor is a prescription for creating a value of a given type. The constructor is used when a value of a type is expected and when such a value can be created by a constructor from the value supplied as an initializer or assigned value. Thus, a constructor requiring a single argument need not be called explicitly. For example, c om pl ex b = 3 co mp le x 3; means c om pl ex b = c om pl ex 3); co mp le x co mp le x(3 A user-defined conversion is implicitly applied only if it is unique (§7.4). See §11.7.1 for a way of specifying constructors that can only be explicitly invoked. Naturally, we still need the constructor that takes two doubles, and a default constructor initial- izing a c om pl ex to (0 0) is also useful: co mp le x 0,0 c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex co mp le x() : r e(0 , i m(0 { } re 0) im 0) c om pl ex do ub le r : r e(r , i m(0 { } co mp le x(d ou bl e r) re r) im 0) c om pl ex do ub le r d ou bl e i : r e(r , i m(i { } co mp le x(d ou bl e r, do ub le i) re r) im i) // ... }; Using default arguments, we can abbreviate: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex do ub le r =0 d ou bl e i =0 : r e(r , i m(i { } co mp le x(d ou bl e 0, do ub le 0) re r) im i) // ... }; When a constructor is explicitly declared for a type, it is not possible to use an initializer list (§5.7, §4.9.5) as the initializer. For example: c om pl ex z 1 = { 3 }; co mp le x z1 // error: complex has a constructor c om pl ex z 2 = { 3 4 }; co mp le x z2 3, // error: complex has a constructor
    281. Section 11.3.4 Copying 271 11.3.4 Copying [over.copy] In addition to the explicitly declared constructors, c om pl ex by default gets a copy constructor co mp le x defined (§10.2.5). A default copy constructor simply copies all members. To be explicit, we could equivalently have written: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex co ns t c om pl ex c : r e(c re , i m(c im { } co mp le x(c on st co mp le x& c) re c.r e) im c.i m) // ... }; However, for types where the default copy constructor has the right semantics, I prefer to rely on that default. It is less verbose than anything I can write, and people should understand the default. Also, compilers know about the default and its possible optimization opportunities. Furthermore, writing out the memberwise copy by hand is tedious and error-prone for classes with many data members (§10.4.6.3). I use a reference argument for the copy constructor because I must. The copy constructor defines what copying means – including what copying an argument means – so writing c om pl ex :c om pl ex co mp le x c : r e(c re , i m(c im { } // error co mp le x: co mp le x(c om pl ex c) re c.r e) im c.i m) is an error because any call would have involved an infinite recursion. For other functions taking c om pl ex arguments, I use value arguments rather than reference co mp le x arguments. Here, the designer has a choice. From a user’s point of view, there is little difference between a function that takes a c om pl ex argument and one that takes a c on st c om pl ex argument. co mp le x co ns t co mp le x& This issue is discussed further in §11.6. In principle, copy constructors are used in simple initializations such as c om pl ex x = 2 co mp le x 2; // create complex(2); then initialize x with it c om pl ex y = c om pl ex 2,0 ; co mp le x co mp le x(2 0) // create complex(2,0); then initialize y with it However, the calls to the copy constructor are trivially optimized away. We could equivalently have written: c om pl ex x 2); co mp le x x(2 // initialize x by 2 c om pl ex y 2,0 ; co mp le x y(2 0) // initialize x by (2,0) For arithmetic types, such as c om pl ex I like the look of the version using = better. It is possible to co mp le x, restrict the set of values accepted by the = style of initialization compared to the ()style by making the copy constructor private (§11.2.2) or by declaring a constructor e xp li ci t (§11.7.1). ex pl ic it Similar to initialization, assignment of two objects of the same class is by default defined as memberwise assignment (§10.2.5). We could explicitly define c om pl ex :o pe ra to r= to do that. co mp le x: op er at or However, for a simple type like c om pl ex there is no reason to do so. The default is just right. co mp le x The copy constructor – whether user-defined or compiler-generated – is used not only for the initialization of variables, but also for argument passing, value return, and exception handling (see §11.7). The semantics of these operations is defined to be the semantics of initialization (§7.1, §7.3, §14.2.1).
    282. 272 Operator Overloading Chapter 11 11.3.5 Constructors and Conversions [over.conv] We defined three versions of each of the four standard arithmetic operators: c om pl ex o pe ra to r+(c om pl ex co mp le x); co mp le x op er at or co mp le x,c om pl ex c om pl ex o pe ra to r+(c om pl ex do ub le ; co mp le x op er at or co mp le x,d ou bl e) c om pl ex o pe ra to r+(d ou bl e,c om pl ex ; co mp le x op er at or do ub le co mp le x) // ... This can get tedious, and what is tedious easily becomes error-prone. What if we had three alterna- tives for the type of each argument for each function? We would need three versions of each single-argument function, nine versions of each two-argument function, twenty-seven versions of each three-argument function, etc. Often these variants are very similar. In fact, almost all variants involve a simple conversion of arguments to a common type followed by a standard algorithm. The alternative to providing different versions of a function for each combination of arguments is to rely on conversions. For example, our c om pl ex class provides a constructor that converts a co mp le x d ou bl e to a c om pl ex Consequently, we could simply declare only one version of the equality do ub le co mp le x. operator for c om pl ex co mp le x: b oo l o pe ra to r==(c om pl ex co mp le x); bo ol op er at or co mp le x,c om pl ex v oi d f co mp le x vo id f(c om pl ex x c om pl ex y x, co mp le x y) { x y; x==y // means operator==(x,y) x 3; x==3 // means operator==(x,complex(3)) 3 y; 3==y // means operator==(complex(3),y) } There can be reasons for preferring to define separate functions. For example, in some cases the conversion can impose overheads, and in other cases, a simpler algorithm can be used for specific argument types. Where such issues are not significant, relying on conversions and providing only the most general variant of a function – plus possibly a few critical variants – contains the combi- natorial explosion of variants that can arise from mixed-mode arithmetic. Where several variants of a function or an operator exist, the compiler must pick ‘‘the right’’ variant based on the argument types and the available (standard and user-defined) conversions. Unless a best match exists, an expression is ambiguous and is an error (see §7.4). An object constructed by explicit or implicit use of a constructor is automatic and will be destroyed at the first opportunity (see §10.4.10). No implicit user-defined conversions are applied to the left-hand side of a . (or a ->). This is the case even when the . is implicit. For example: v oi d g co mp le x z vo id g(c om pl ex z) { 3 z; 3+z // ok: complex(3)+z 3 op er at or 3.o pe ra to r+=(z ; z) // error: 3 is not a class object 3 z; 3+=z // error: 3 is not a class object } Thus, you can express the notion that an operator requires an lvalue as their left-hand operand by making that operator a member.
    283. Section 11.3.6 Literals 273 11.3.6 Literals [over.literals] It is not possible to define literals of a class type in the sense that 1 2 and 1 2e 3 are literals of type 1.2 12 e3 d ou bl e. However, literals of the basic types can often be used instead if class member functions are do ub le used to provide an interpretation for them. Constructors taking a single argument provide a general mechanism for this. When constructors are simple and inline, it is quite reasonable to think of con- structor invocations with literal arguments as literals. For example, I think of c om pl ex 3) as a lit- co mp le x(3 eral of type c om pl ex even though technically it isn’t. co mp le x, 11.3.7 Additional Member Functions [over.additional] So far, we have provided class c om pl ex with constructors and arithmetic operators only. That is co mp le x not quite sufficient for real use. In particular, we often need to be able to examine the value of the real and imaginary parts: c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic d ou bl e r ea l() c on st { r et ur n r e; } do ub le re al co ns t re tu rn re d ou bl e i ma g() c on st { r et ur n i m; } do ub le im ag co ns t re tu rn im // ... }; Unlike the other members of c om pl ex r ea l() and i ma g() do not modify the value of a c om pl ex co mp le x, re al im ag co mp le x, so they can be declared c on st co ns t. Given r ea l() and i ma g(), we can define all kinds of useful operations without granting them re al im ag direct access to the representation of c om pl ex For example: co mp le x. i nl in e b oo l o pe ra to r==(c om pl ex a c om pl ex b in li ne bo ol op er at or co mp le x a, co mp le x b) { r et ur n a re al re tu rn a.r ea l()==b re al && a im ag b.r ea l() a.i ma g()==b im ag ; b.i ma g() } Note that we need only to be able to read the real and imaginary parts; writing them is less often needed. If we must do a ‘‘partial update,’’ we can: v oi d f co mp le x& z d ou bl e d vo id f(c om pl ex z, do ub le d) { // ... z = c om pl ex z.r ea l(),d ; // assign d to z.im co mp le x(z re al d) } A good optimizer generates a single assignment for that statement. 11.3.8 Helper Functions [over.helpers] If we put all the bits and pieces together, the c om pl ex class becomes: co mp le x
    284. 274 Operator Overloading Chapter 11 c la ss c om pl ex { cl as s co mp le x d ou bl e r e, i m; do ub le re im p ub li c: pu bl ic c om pl ex do ub le r =0 d ou bl e i =0 : r e(r , i m(i { } co mp le x(d ou bl e 0, do ub le 0) re r) im i) d ou bl e r ea l() c on st { r et ur n r e; } do ub le re al co ns t re tu rn re d ou bl e i ma g() c on st { r et ur n i m; } do ub le im ag co ns t re tu rn im c om pl ex o pe ra to r+=(c om pl ex ; co mp le x& op er at or co mp le x) c om pl ex o pe ra to r+=(d ou bl e); co mp le x& op er at or do ub le // – =, *=, and /= }; In addition, we must provide a number of helper functions: c om pl ex o pe ra to r+(c om pl ex co mp le x); co mp le x op er at or co mp le x,c om pl ex c om pl ex o pe ra to r+(c om pl ex do ub le ; co mp le x op er at or co mp le x,d ou bl e) c om pl ex o pe ra to r+(d ou bl e,c om pl ex ; co mp le x op er at or do ub le co mp le x) // – , *, and / c om pl ex o pe ra to r-(c om pl ex ; // unary minus co mp le x op er at or co mp le x) c om pl ex o pe ra to r+(c om pl ex ; // unary plus co mp le x op er at or co mp le x) b oo l o pe ra to r==(c om pl ex co mp le x); bo ol op er at or co mp le x,c om pl ex b oo l o pe ra to r!=(c om pl ex co mp le x); bo ol op er at or co mp le x,c om pl ex i st re am o pe ra to r>>(i st re am co mp le x&); // input is tr ea m& op er at or is tr ea m&,c om pl ex o st re am o pe ra to r<<(o st re am co mp le x); // output os tr ea m& op er at or os tr ea m&,c om pl ex Note that the members r ea l() and i ma g() are essential for defining the comparisons. The defini- re al im ag tion of most of the following helper functions similarly relies on r ea l() and i ma g(). re al im ag We might provide functions to allow users to think in terms of polar coordinates: c om pl ex p ol ar do ub le r ho d ou bl e t he ta ; co mp le x po la r(d ou bl e rh o, do ub le th et a) c om pl ex c on j(c om pl ex ; co mp le x co nj co mp le x) d ou bl e a bs co mp le x); do ub le ab s(c om pl ex d ou bl e a rg co mp le x); do ub le ar g(c om pl ex d ou bl e n or m(c om pl ex ; do ub le no rm co mp le x) d ou bl e r ea l(c om pl ex ; do ub le re al co mp le x) // for notational convenience d ou bl e i ma g(c om pl ex ; do ub le im ag co mp le x) // for notational convenience Finally, we must provide an appropriate set of standard mathematical functions: c om pl ex a co s(c om pl ex ; co mp le x ac os co mp le x) c om pl ex a si n(c om pl ex ; co mp le x as in co mp le x) c om pl ex a ta n(c om pl ex ; co mp le x at an co mp le x) // ... From a user’s point of view, the complex type presented here is almost identical to the c om pl ex do ub le found in <c om pl ex in the standard library (§22.5). co mp le x<d ou bl e> co mp le x>
    285. Section 11.4 Conversion Operators 275 11.4 Conversion Operators [over.conversion] Using a constructor to specify type conversion is convenient but has implications that can be unde- sirable. A constructor cannot specify [1] an implicit conversion from a user-defined type to a basic type (because the basic types are not classes), or [2] a conversion from a new class to a previously defined class (without modifying the decla- ration for the old class). These problems can be handled by defining a conversion operator for the source type. A member function X :o pe ra to r T X: op er at or T(), where T is a type name, defines a conversion from X to T For exam- T. ple, one could define a 6-bit non-negative integer, T in y, that can mix freely with integers in arith- Ti ny metic operations: c la ss T in y { cl as s Ti ny c ha r v ch ar v; v oi d a ss ig n(i nt i { i f (i 07 7) t hr ow B ad _r an ge ; v i; } vo id as si gn in t i) if i&~0 77 th ro w Ba d_ ra ng e() v=i p ub li c: pu bl ic c la ss B ad _r an ge { }; cl as s Ba d_ ra ng e T in y(i nt i { a ss ig n(i ; } Ti ny in t i) as si gn i) T in y& o pe ra to r=(i nt i { a ss ig n(i ; r et ur n *t hi s; } Ti ny op er at or in t i) as si gn i) re tu rn th is o pe ra to r i nt op er at or in t() c on st { r et ur n v } co ns t re tu rn v; // conversion to int function }; The range is checked whenever a T in y is initialized by an i nt and whenever an i nt is assigned to Ti ny in t in t one. No range check is needed when we copy a T in y, so the default copy constructor and assign- Ti ny ment are just right. To enable the usual integer operations on T in y variables, we define the implicit conversion from Ti ny T in y to i nt T in y::o pe ra to r i nt Ti ny in t, Ti ny op er at or in t(). Note that the type being converted to is part of the name of the operator and cannot be repeated as the return value of the conversion function: T in y::o pe ra to r i nt Ti ny op er at or in t() c on st { r et ur n v } co ns t re tu rn v; // right i nt T in y::o pe ra to r i nt in t Ti ny op er at or in t() c on st { r et ur n v } co ns t re tu rn v; // error In this respect also, a conversion operator resembles a constructor. Whenever a T in y appears where an i nt is needed, the appropriate i nt is used. For example: Ti ny in t in t i nt m ai n() in t ma in { T in y c 1 = 2 Ti ny c1 2; T in y c 2 = 6 2; Ti ny c2 62 T in y c 3 = c 2-c 1; Ti ny c3 c2 c1 // c3 = 60 T in y c 4 = c 3; Ti ny c4 c3 // no range check (not necessary) i nt i = c 1+c 2; in t c1 c2 // i = 64 c 1 = c 1+c 2; c1 c1 c2 // range error: c1 can’t be 64 i = c 3-6 4; c3 64 // i = – 4 c 2 = c 3-6 4; c2 c3 64 // range error: c2 can’t be – 4 c 3 = c 4; c3 c4 // no range check (not necessary) }
    286. 276 Operator Overloading Chapter 11 Conversion functions appear to be particularly useful for handling data structures when reading (implemented by a conversion operator) is trivial, while assignment and initialization are distinctly less trivial. The i st re am and o st re am types rely on a conversion function to enable statements such as is tr ea m os tr ea m w hi le (c in x) c ou t<<x wh il e ci n>>x co ut x; The input operation c in x returns an i st re am That value is implicitly converted to a value indi- ci n>>x is tr ea m&. cating the state of c in This value can then be tested by the w hi le (see §21.3.3). However, it is typ- ci n. wh il e ically not a good idea to define an implicit conversion from one type to another in such a way that information is lost in the conversion. In general, it is wise to be sparing in the introduction of conversion operators. When used in excess, they lead to ambiguities. Such ambiguities are caught by the compiler, but they can be a nuisance to resolve. Probably the best idea is initially to do conversions by named functions, such X: ma ke _i nt as X :m ak e_ in t(). If such a function becomes popular enough to make explicit use inelegant, it can be replaced by a conversion operator X :o pe ra to r i nt X: op er at or in t(). If both user-defined conversions and user-defined operators are defined, it is possible to get ambiguities between the user-defined operators and the built-in operators. For example: i nt o pe ra to r+(T in y,T in y); in t op er at or Ti ny Ti ny v oi d f Ti ny t i nt i vo id f(T in y t, in t i) { t i; // error, ambiguous: operator+(t,Tiny(i)) or int(t)+i ? t+i } It is therefore often best to rely on user-defined conversions or user-defined operators for a given type, but not both. 11.4.1 Ambiguities [over.ambig] An assignment of a value of type V to an object of class X is legal if there is an assignment operator X :o pe ra to r=(Z so that V is Z or there is a unique conversion of V to Z Initialization is treated X: op er at or Z) Z. equivalently. In some cases, a value of the desired type can be constructed by repeated use of constructors or conversion operators. This must be handled by explicit conversions; only one level of user-defined implicit conversion is legal. In some cases, a value of the desired type can be constructed in more than one way; such cases are illegal. For example: c la ss X { /* ... */ X in t); X ch ar ; }; cl as s X(i nt X(c ha r*) c la ss Y { /* ... */ Y in t); }; cl as s Y(i nt c la ss Z { /* ... */ Z X); }; cl as s Z(X X f X); f(X Y f Y); f(Y Z g Z); g(Z
    287. Section 11.4.1 Ambiguities 277 v oi d k 1() vo id k1 { f 1); f(1 // error: ambiguous f(X(1)) or f(Y(1))? f X(1 ; f(X 1)) // ok f Y(1 ; f(Y 1)) // ok g Ma ck ; g(\"M ac k\") // error: two user-defined conversions needed; g(Z(X(\"Mack\"))) not tried g X(\"D oc g(X Do c\")); // ok: g(Z(X(\"Doc\"))) g Z(\"S uz y\")); // ok: g(Z(X(\"Suzy\"))) g(Z Su zy } User-defined conversions are considered only if they are necessary to resolve a call. For example: c la ss X X { /* ... */ X X(i nt ; }; cl as s XX XX in t) v oi d h do ub le ; vo id h(d ou bl e) v oi d h XX ; vo id h(X X) v oi d k 2() vo id k2 { h 1); h(1 // h(double(1)) or h(XX(1))? h(double(1))! } The call h 1) means h do ub le 1)) because that alternative uses only a standard conversion h(1 h(d ou bl e(1 rather than a user-defined conversion (§7.4). The rules for conversion are neither the simplest to implement, the simplest to document, nor the most general that could be devised. They are, however, considerably safer, and the resulting resolutions are less surprising. It is far easier to manually resolve an ambiguity than to find an error caused by an unsuspected conversion. The insistence on strict bottom-up analysis implies that the return type is not used in overload- ing resolution. For example: c la ss Q ua d { cl as s Qu ad p ub li c: pu bl ic Q ua d(d ou bl e); Qu ad do ub le // ... }; Q ua d o pe ra to r+(Q ua d,Q ua d); Qu ad op er at or Qu ad Qu ad v oi d f do ub le a 1, d ou bl e a 2) vo id f(d ou bl e a1 do ub le a2 { Q ua d r 1 = a 1+a 2; Qu ad r1 a1 a2 // double-precision add Q ua d r 2 = Q ua d(a 1)+a 2; // force quad arithmetic Qu ad r2 Qu ad a1 a2 } The reason for this design choice is partly that strict bottom-up analysis is more comprehensible and partly that it is not considered the compiler’s job to decide which precision the programmer might want for the addition. Once the types of both sides of an initialization or assignment have been determined, both types are used to resolve the initialization or assignment. For example:
    288. 278 Operator Overloading Chapter 11 c la ss R ea l { cl as s Re al p ub li c: pu bl ic o pe ra to r d ou bl e(); op er at or do ub le o pe ra to r i nt ; op er at or in t() // ... }; v oi d g Re al a vo id g(R ea l a) { d ou bl e d = a do ub le a; // d = a.double(); i nt i = a in t a; // i = a.int(); d=a a; // d = a.double(); i=a a; // i = a.int(); } In these cases, the type analysis is still bottom-up, with only a single operator and its argument types considered at any one time. 11.5 Friends [over.friends] An ordinary member function declaration specifies three logically distinct things: [1] The function can access the private part of the class declaration, and [2] the function is in the scope of the class, and [3] the function must be invoked on an object (has a t hi s pointer). th is By declaring a member function s ta ti c (§10.2.4), we can give it the first two properties only. By st at ic declaring a function a f ri en d, we can give it the first property only. fr ie nd For example, we could define an operator that multiplies a M at ri x by a V ec to r. Naturally, Ma tr ix Ve ct or V ec to r and M at ri x each hide their representation and provide a complete set of operations for Ve ct or Ma tr ix manipulating objects of their type. However, our multiplication routine cannot be a member of both. Also, we don’t really want to provide low-level access functions to allow every user to both read and write the complete representation of both M at ri x and V ec to r. To avoid this, we declare Ma tr ix Ve ct or the o pe ra to r* a friend of both: op er at or c la ss M at ri x; cl as s Ma tr ix c la ss V ec to r { cl as s Ve ct or f lo at v 4]; fl oa t v[4 // ... f ri en d V ec to r o pe ra to r*(c on st M at ri x&, c on st V ec to r&); fr ie nd Ve ct or op er at or co ns t Ma tr ix co ns t Ve ct or }; c la ss M at ri x { cl as s Ma tr ix V ec to r v 4]; Ve ct or v[4 // ... f ri en d V ec to r o pe ra to r*(c on st M at ri x&, c on st V ec to r&); fr ie nd Ve ct or op er at or co ns t Ma tr ix co ns t Ve ct or };
    289. Section 11.5 Friends 279 V ec to r o pe ra to r*(c on st M at ri x& m c on st V ec to r& v Ve ct or op er at or co ns t Ma tr ix m, co ns t Ve ct or v) { V ec to r r Ve ct or r; f or (i nt i = 0 i 4; i fo r in t 0; i<4 i++) { // r[i] = m[i] * v; r v[i = 0 r.v i] 0; f or (i nt j = 0 j 4; j fo r in t 0; j<4 j++) r v[i += m v[i v[j * v v[j ; r.v i] m.v i].v j] v.v j] } r et ur n r re tu rn r; } A f ri en d declaration can be placed in either the private or the public part of a class declaration; it fr ie nd does not matter where. Like a member function, a friend function is explicitly declared in the declaration of the class of which it is a friend. It is therefore as much a part of that interface as is a member function. A member function of one class can be the friend of another. For example: cl as s Li st _i te ra to r c la ss L is t_ it er at or { // ... i nt n ex t(); in t* ne xt }; c la ss L is t { cl as s Li st fr ie nd in t* Li st _i te ra to r: ne xt f ri en d i nt L is t_ it er at or :n ex t(); // ... }; It is not unusual for all functions of one class to be friends of another. There is a shorthand for this: c la ss L is t { cl as s Li st fr ie nd cl as s Li st _i te ra to r; f ri en d c la ss L is t_ it er at or // ... }; Li st _i te ra to r’s This friend declaration makes all of L is t_ it er at or member functions friends of L is t. Li st Clearly, f ri en d classes should be used only to express closely connected concepts. Often, there fr ie nd is a choice between making a class a member (a nested class) or a friend (§24.4). 11.5.1 Finding Friends [over.lookup] Like a member declaration, a f ri en d declaration does not introduce a name into an enclosing scope. fr ie nd For example: c la ss M at ri x { cl as s Ma tr ix f ri en d c la ss X fo rm fr ie nd cl as s Xf or m; f ri en d M at ri x i nv er t(c on st M at ri x&); fr ie nd Ma tr ix in ve rt co ns t Ma tr ix // ... }; X fo rm x Xf or m x; // error: no Xform in scope M at ri x (*p co ns t M at ri x&) = &i nv er t; Ma tr ix p)(c on st Ma tr ix in ve rt // error: no invert() in scope For large programs and large classes, it is nice that a class doesn’t ‘‘quietly’’ add names to its
    290. 280 Operator Overloading Chapter 11 enclosing scope. For a template class that can be instantiated in many different contexts (Chapter 13), this is very important. A friend class must be previously declared in an enclosing scope or defined in the non-class scope immediately enclosing the class that is declaring it a friend. For example: c la ss X { /* ... */ }; cl as s // Y’s friend n am es pa ce N { na me sp ac e c la ss Y { cl as s f ri en d c la ss X fr ie nd cl as s X; f ri en d c la ss Z fr ie nd cl as s Z; f ri en d c la ss A E; fr ie nd cl as s AE }; c la ss Z { /* ... */ }; cl as s // Y’s friend } c la ss A E { /* ... */ }; cl as s AE // not a friend of Y A friend function can be explicitly declared just like friend classes, or it can be found through its argument types (§8.2.6) as if it was declared in the non-class scope immediately enclosing its class. For example: v oi d f Ma tr ix m vo id f(M at ri x& m) { i nv er t(m ; in ve rt m) // Matrix’s friend invert() } It follows that a friend function should either be explicitly declared in an enclosing scope or take an argument of its class. If not, the friend cannot be called. For example: // no f() here v oi d g ; vo id g() // X’s friend c la ss X { cl as s f ri en d v oi d f ; fr ie nd vo id f() // useless f ri en d v oi d g ; fr ie nd vo id g() f ri en d v oi d h co ns t X ; // can be found through its argument fr ie nd vo id h(c on st X&) }; v oi d f vo id f() { /* ... */ } // not a friend of X 11.5.2 Friends and Members [over.friends.members] When should we use a friend function, and when is a member function the better choice for specify- ing an operation? First, we try to minimize the number of functions that access the representation of a class and try to make the set of access functions as appropriate as possible. Therefore, the first question is not, ‘‘Should it be a member, a static member, or a friend?’’ but rather, ‘‘Does it really need access?’’ Typically, the set of functions that need access is smaller than we are willing to believe at first. Some operations must be members – for example, constructors, destructors, and virtual
    291. Section 11.5.2 Friends and Members 281 functions (§12.2.6) – but typically there is a choice. Because member names are local to the class, a function should be a member unless there is a specific reason for it to be a nonmember. Consider a class X presenting alternative ways of presenting an operation: c la ss X { cl as s // ... X in t); X(i nt i nt m 1(); in t m1 i nt m 2() c on st in t m2 co ns t; f ri en d i nt f 1(X ; fr ie nd in t f1 X&) f ri en d i nt f 2(c on st X ; fr ie nd in t f2 co ns t X&) f ri en d i nt f 3(X ; fr ie nd in t f3 X) }; Member functions can be invoked for objects of their class only; no user-defined conversions are applied. For example: v oi d g vo id g() { 9 9.m 1(); // error: X(99).m1() not tried 99 m1 9 9.m 2(); // error: X(99).m2() not tried 99 m2 } The conversion X in t) is not applied to make an X out of 9 9. X(i nt 99 The global function f 1() has a similar property because implicit conversions are not used for f1 non-c on st reference arguments (§5.5, §11.3.5). However, conversions may be applied to the argu- co ns t ments of f 2() and f 3(): f2 f3 v oi d h vo id h() { f 1(9 9); f1 99 // error: f1(X(99)) not tried f 2(9 9); f2 99 // ok: f2(X(99)); f 3(9 9); f3 99 // ok: f3(X(99)); } An operation modifying the state of a class object should therefore be a member or a global func- tion taking a non-c on st reference argument (or a non-c on st pointer argument). Operators that co ns t co ns t require lvalue operands for the fundamental types (=, *=, ++, etc.) are most naturally defined as members for user-defined types. Conversely, if implicit type conversion is desired for all operands of an operation, the function implementing it must be a nonmember function taking a c on st reference argument or a non- co ns t reference argument. This is often the case for the functions implementing operators that do not require lvalue operands when applied to fundamental types (+, -, ||, etc.). Such operators often need access to the representations of their operand class. Consequently, binary operators are the most common source of f ri en d functions. fr ie nd If no type conversions are defined, there appears to be no compelling reason to choose a mem- ber over a friend taking a reference argument, or vice versa. In some cases, the programmer may have a preference for one call syntax over another. For example, most people seem to prefer the
    292. 282 Operator Overloading Chapter 11 notation i nv m) for inverting a M at ri x m to the alternative m in v(). Naturally, if i nv in v(m Ma tr ix m.i nv in v() really does invert m itself, rather than return a new M at ri x that is the inverse of m it should be a member. Ma tr ix m, All other things considered equal, choose a member. It is not possible to know if someone someday will define a conversion operator. It is not always possible to predict if a future change may require changes to the state of the object involved. The member function call syntax makes it clear to the user that the object may be modified; a reference argument is far less obvious. Further- more, expressions in the body of a member can be noticeably shorter than the equivalent expres- sions in a global function; a nonmember function must use an explicit argument, whereas the mem- ber can use t hi s implicitly. Also, because member names are local to the class they tend to be th is shorter than the names of nonmember functions. 11.6 Large Objects [over.large] We defined the c om pl ex operators to take arguments of type c om pl ex This implies that for each co mp le x co mp le x. use of a c om pl ex operator, each operand is copied. The overhead of copying two d ou bl es can be co mp le x do ub le s noticeable but often less than what a pair of pointers impose. Unfortunately, not all classes have a conveniently small representation. To avoid excessive copying, one can declare functions to take reference arguments. For example: c la ss M at ri x { cl as s Ma tr ix d ou bl e m 4][4 ; do ub le m[4 4] p ub li c: pu bl ic M at ri x(); Ma tr ix f ri en d M at ri x o pe ra to r+(c on st M at ri x&, c on st M at ri x&); fr ie nd Ma tr ix op er at or co ns t Ma tr ix co ns t Ma tr ix f ri en d M at ri x o pe ra to r*(c on st M at ri x&, c on st M at ri x&); fr ie nd Ma tr ix op er at or co ns t Ma tr ix co ns t Ma tr ix }; References allow the use of expressions involving the usual arithmetic operators for large objects without excessive copying. Pointers cannot be used because it is not possible to redefine the mean- ing of an operator applied to a pointer. Addition could be defined like this: M at ri x o pe ra to r+(c on st M at ri x& a rg 1, c on st M at ri x& a rg 2) Ma tr ix op er at or co ns t Ma tr ix ar g1 co ns t Ma tr ix ar g2 { M at ri x s um Ma tr ix su m; f or (i nt i 0; i 4; i fo r in t i=0 i<4 i++) f or (i nt j 0; j 4; j fo r in t j=0 j<4 j++) s um m[i j] = a rg 1.m i][j + a rg 2.m i][j ; su m.m i][j ar g1 m[i j] ar g2 m[i j] r et ur n s um re tu rn su m; } This o pe ra to r+() accesses the operands of + through references but returns an object value. op er at or Returning a reference would appear to be more efficient: c la ss M at ri x { cl as s Ma tr ix // ... f ri en d M at ri x& o pe ra to r+(c on st M at ri x&, c on st M at ri x&); fr ie nd Ma tr ix op er at or co ns t Ma tr ix co ns t Ma tr ix f ri en d M at ri x& o pe ra to r*(c on st M at ri x&, c on st M at ri x&); fr ie nd Ma tr ix op er at or co ns t Ma tr ix co ns t Ma tr ix };
    293. Section 11.6 Large Objects 283 This is legal, but it causes a memory allocation problem. Because a reference to the result will be passed out of the function as a reference to the return value, the return value cannot be an automatic variable (§7.3). Since an operator is often used more than once in an expression, the result cannot be a s ta ti c local variable. The result would typically be allocated on the free store. Copying the st at ic return value is often cheaper (in execution time, code space, and data space) than allocating and (eventually) deallocating an object on the free store. It is also much simpler to program. There are techniques you can use to avoid copying the result. The simplest is to use a buffer of static objects. For example: c on st m ax _m at ri x_ te mp = 7 co ns t ma x_ ma tr ix _t em p 7; M at ri x& g et _m at ri x_ te mp Ma tr ix ge t_ ma tr ix _t em p() { s ta ti c i nt n bu f = 0 st at ic in t nb uf 0; s ta ti c M at ri x b uf ma x_ ma tr ix _t em p]; st at ic Ma tr ix bu f[m ax _m at ri x_ te mp i f (n bu f == m ax _m at ri x_ te mp n bu f = 0 ma x_ ma tr ix _t em p) nb uf 0; if nb uf r et ur n b uf nb uf re tu rn bu f[n bu f++]; } M at ri x& o pe ra to r+(c on st M at ri x& a rg 1, c on st M at ri x& a rg 2) Ma tr ix op er at or co ns t Ma tr ix ar g1 co ns t Ma tr ix ar g2 { M at ri x& r es = g et _m at ri x_ te mp ; Ma tr ix re s ge t_ ma tr ix _t em p() // ... r et ur n r es re tu rn re s; } Now a M at ri x is copied only when the result of an expression is assigned. However, heaven help Ma tr ix you if you write an expression that involves more than m ax _m at ri x_ te mp temporaries! ma x_ ma tr ix _t em p A less error-prone technique involves defining the matrix type as a handle (§25.7) to a represen- tation type that really holds the data. In that way, the matrix handles can manage the representation objects in such a way that allocation and copying are minimized (see §11.12 and §11.14[18]). However, that strategy relies on operators returning objects rather than references or pointers. Another technique is to define ternary operations and have them automatically invoked for expres- sions such as a b+c and a b*i (§21.4.6.3, §22.4.7). a=b c a+b i 11.7 Essential Operators [over.essential] In general, for a type X the copy constructor X co ns t X X, X(c on st X&) takes care of initialization by an object of the same type X It cannot be overemphasized that assignment and initialization are different X. operations (§10.4.4.1). This is especially important when a destructor is declared. If a class X has a destructor that performs a nontrivial task, such as free-store deallocation, the class is likely to need the full complement of functions that control construction, destruction, and copying:
    294. 284 Operator Overloading Chapter 11 c la ss X { cl as s // ... X So me ty pe ; X(S om et yp e) // constructor: create objects X co ns t X ; X(c on st X&) // copy constructor X o pe ra to r=(c on st X ; X& op er at or co ns t X&) // copy assignment: cleanup and copy X() // destructor: cleanup ~X ; }; There are three more cases in which an object is copied: as a function argument, as a function return value, and as an exception. When an argument is passed, a hitherto uninitialized variable – the formal parameter – is initialized. The semantics are identical to those of other initializations. The same is the case for function return values and exceptions, although that is less obvious. In such cases, the copy constructor will be applied. For example: s tr in g g st ri ng a rg st ri ng g(s tr in g ar g) { r et ur n a rg re tu rn ar g; } i nt m ai n () in t ma in { s tr in g s = \"N ew to n\"; st ri ng Ne wt on s = g s); g(s } Clearly, the value of s ought to be \" Ne wt on \" after the call of g \"N ew to n\" g(). Getting a copy of the value of s into the argument a rg is not difficult; a call of s tr in g’s copy constructor does that. Getting a copy ar g st ri ng of that value out of g g() takes another call of s tr in g(c on st s tr in g&); this time, the variable initial- st ri ng co ns t st ri ng ized is a temporary one, which is then assigned to s Often one, but not both, of these copy opera- s. tions can be optimized away. Such temporary variables are, of course, destroyed properly using s tr in g::~s tr in g() (see §10.4.10). st ri ng st ri ng For a class X for which the assignment operator X :o pe ra to r=(c on st X X: op er at or co ns t X&) and the copy con- structor X :X co ns t X X: X(c on st X&) are not explicitly declared by the programmer, the missing operation or operations will be generated by the compiler (§10.2.5). 11.7.1 Explicit Constructors [over.explicit] By default, a single argument constructor also defines an implicit conversion. For some types, that is ideal. For example: c om pl ex z = 2 // initialize z with complex(2) co mp le x 2; In other cases, the implicit conversion is undesirable and error-prone. For example: s tr in g s = ´a st ri ng a´; // make s a string with int(’a’) elements It is quite unlikely that this was what the person defining s meant. Implicit conversion can be suppressed by declaring a constructor e xp li ci t. That is, an e xp li ci t ex pl ic it ex pl ic it constructor will be invoked only explicitly. In particular, where a copy constructor is in principle needed (§11.3.4), an e xp li ci t constructor will not be implicitly invoked. For example: ex pl ic it
    295. Section 11.7.1 Explicit Constructors 285 c la ss S tr in g { cl as s St ri ng // ... e xp li ci t S tr in g(i nt n ; ex pl ic it St ri ng in t n) // preallocate n bytes S tr in g(c on st c ha r* p ; St ri ng co ns t ch ar p) // initial value is the C-style string p }; // error: no implicit char– >String conversion S tr in g St ri ng s 1 = ´a s1 a´; S tr in g St ri ng s 2(1 0); s2 10 // ok: String with space for 10 characters S tr in g St ri ng s 3 = S tr in g(1 0); s3 St ri ng 10 // ok: String with space for 10 characters S tr in g St ri ng s 4 = \"B ri an s4 Br ia n\"; // ok: s4 = String(\"Brian\") S tr in g St ri ng s 5(\"F aw lt y\"); s5 Fa wl ty v oi d f St ri ng ; vo id f(S tr in g) S tr in g g St ri ng g() { // error: no implicit int– >String conversion f 10 ; f(1 0) f St ri ng 10 ; f(S tr in g(1 0)) f Ar th ur ; f(\"A rt hu r\") // ok: f(String(\"Arthur\")) f s1 ; f(s 1) S tr in g* p 1 = n ew S tr in g(\"E ri c\"); St ri ng p1 ne w St ri ng Er ic S tr in g* p 2 = n ew S tr in g(1 0); St ri ng p2 ne w St ri ng 10 // error: no implicit int– >String conversion r et ur n 1 0; re tu rn 10 } The distinction between // error: no implicit char– >String conversion S tr in g s 1 = ´a St ri ng s1 a´; and S tr in g s 2(1 0); St ri ng s2 10 // ok: string with space for 10 characters may seem subtle, but it is less so in real code than in contrived examples. In D at e, we used a plain i nt to represent a year (§10.3). Had D at e been critical in our design, Da te in t Da te we might have introduced a Y ea r type to allow stronger compile-time checking. For example: Ye ar c la ss Y ea r { cl as s Ye ar i nt y in t y; p ub li c: pu bl ic e xp li ci t Y ea r(i nt i : y i) { } ex pl ic it Ye ar in t i) y(i // construct Year from int o pe ra to r i nt op er at or in t() c on st { r et ur n y } co ns t re tu rn y; // conversion: Year to int }; c la ss D at e { cl as s Da te p ub li c: pu bl ic D at e(i nt d M on th m Y ea r y ; Da te in t d, Mo nt h m, Ye ar y) // ... }; D at e d 3(1 97 8,f eb 21 ; Da te d3 19 78 fe b,2 1) // error: 21 is not a Year D at e d 4(2 1,f eb Ye ar 19 78 ; // ok Da te d4 21 fe b,Y ea r(1 97 8))
    296. 286 Operator Overloading Chapter 11 The Y ea r class is a simple ‘‘wrapper’’ around an i nt Thanks to the o pe ra to r i nt Ye ar in t. op er at or in t(), a Y ea r is Ye ar implicitly converted into an i nt wherever needed. By declaring the constructor e xp li ci t, we make in t ex pl ic it sure that the i nt to Y ea r happens only when we ask for it and that ‘‘accidental’’ assignments are in t Ye ar caught at compile time. Because Y ea r’s member functions are easily inlined, no run-time or space Ye ar costs are added. A similar technique can be used to define range types (§25.6.1). 11.8 Subscripting [over.subscript] An o pe ra to r[] function can be used to give subscripts a meaning for class objects. The second op er at or argument (the subscript) of an o pe ra to r[] function may be of any type. This makes it possible to op er at or define v ec to rs, associative arrays, etc. ve ct or As an example, let us recode the example from §5.5 in which an associative array is used to write a small program for counting the number of occurrences of words in a file. There, a function is used. Here, an associative array type is defined: c la ss A ss oc { cl as s As so c s tr uc t P ai r { st ru ct Pa ir s tr in g n am e; st ri ng na me d ou bl e v al do ub le va l; P ai r(s tr in g n =\"\", d ou bl e v =0 :n am e(n , v al v) { } Pa ir st ri ng do ub le 0) na me n) va l(v }; v ec to r<P ai r> v ec ve ct or Pa ir ve c; A ss oc co ns t A ss oc ; As so c(c on st As so c&) // private to prevent copying A ss oc o pe ra to r=(c on st A ss oc ; As so c& op er at or co ns t As so c&) // private to prevent copying p ub li c: pu bl ic A ss oc As so c() {} d ou bl e& o pe ra to r[](c on st s tr in g&); do ub le op er at or co ns t st ri ng v oi d p ri nt _a ll vo id pr in t_ al l() c on st co ns t; }; An A ss oc keeps a vector of P ai rs. The implementation uses the same trivial and inefficient search As so c Pa ir method as in §5.5: d ou bl e& A ss oc :o pe ra to r[](c on st s tr in g& s do ub le As so c: op er at or co ns t st ri ng s) // search for s; return its value if found; otherwise, make a new Pair and return the default value 0 { f or (v ec to r<P ai r>::c on st _i te ra to r p = v ec be gi n(); p ve c.e nd ; ++p fo r ve ct or Pa ir co ns t_ it er at or ve c.b eg in p!=v ec en d() p) i f (s == p na me r et ur n p va l; if s p->n am e) re tu rn p->v al v ec pu sh _b ac k(P ai r(s 0)); ve c.p us h_ ba ck Pa ir s,0 // initial value: 0 r et ur n v ec ba ck re tu rn ve c.b ac k().v al va l; // return last element (§16.3.3) } Because the representation of an A ss oc is hidden, we need a way of printing it: As so c
    297. Section 11.8 Subscripting 287 v oi d A ss oc :p ri nt _a ll vo id As so c: pr in t_ al l() c on st co ns t { f or (v ec to r<P ai r>::c on st _i te ra to r p = v ec be gi n(); p ve c.e nd ; ++p fo r ve ct or Pa ir co ns t_ it er at or ve c.b eg in p!=v ec en d() p) c ou t << p na me << \": \" << p va l << ´\\ n´; co ut p->n am e p->v al \\n } Finally, we can write the trivial main program: i nt m ai n() in t ma in // count the occurrences of each word on input { s tr in g b uf st ri ng bu f; A ss oc v ec As so c ve c; w hi le (c in bu f) v ec bu f]++; wh il e ci n>>b uf ve c[b uf ve c.p ri nt _a ll v ec pr in t_ al l(); } A further development of the idea of an associative array can be found in §17.4.1. An o pe ra to r[]() must be a member function. op er at or 11.9 Function Call [over.call] Function call, that is, the notation expression(expression-list), can be interpreted as a binary opera- tion with the expression as the left-hand operand and the expression-list as the right-hand operand. The call operator () can be overloaded in the same way as other operators can. An argument list for an o pe ra to r()() is evaluated and checked according to the usual argument-passing rules. op er at or Overloading function call seems to be useful primarily for defining types that have only a single operation and for types for which one operation is predominant. The most obvious, and probably also the most important, use of the () operator is to provide the usual function call syntax for objects that in some way behave like functions. An object that acts like a function is often called a function-like object or simply a function object (§18.4). Such function objects are important because they allow us to write code that takes nontrivial operations as parameters. For example, the standard library provides many algorithms that invoke a function for each element of a container. Consider: v oi d n eg at e(c om pl ex c { c = -c } vo id ne ga te co mp le x& c) c; v oi d f ve ct or co mp le x>& a a, l is t<c om pl ex l l) vo id f(v ec to r<c om pl ex aa li st co mp le x>& ll { f or _e ac h(a a.b eg in ,a a.e nd ,n eg at e); // negate all vector elements fo r_ ea ch aa be gi n() aa en d() ne ga te f or _e ac h(l l.b eg in ,l l.e nd ,n eg at e); // negate all list elements fo r_ ea ch ll be gi n() ll en d() ne ga te } This negates every element in the vector and the list. What if we wanted to add c om pl ex 2,3 to every element? That is easily done like this: co mp le x(2 3)
    298. 288 Operator Overloading Chapter 11 v oi d a dd 23 co mp le x& c vo id ad d2 3(c om pl ex c) { c += c om pl ex 2,3 ; co mp le x(2 3) } v oi d g ve ct or co mp le x>& a a, l is t<c om pl ex vo id g(v ec to r<c om pl ex aa li st co mp le x>& l l) ll { f or _e ac h(a a.b eg in ,a a.e nd ,a dd 23 ; fo r_ ea ch aa be gi n() aa en d() ad d2 3) f or _e ac h(l l.b eg in ,l l.e nd ,a dd 23 ; fo r_ ea ch ll be gi n() ll en d() ad d2 3) } How would we write a function to repeatedly add an arbitrary complex value? We need something to which we can pass that arbitrary value and which can then use that value each time it is called. That does not come naturally for functions. Typically, we end up ‘‘passing’’ the arbitrary value by leaving it in the function’s surrounding context. That’s messy. However, we can write a class that behaves in the desired way: c la ss A dd { cl as s Ad d c om pl ex v al co mp le x va l; p ub li c: pu bl ic A dd co mp le x c { v al = c } Ad d(c om pl ex c) va l c; // save value A dd do ub le r d ou bl e i { v al = c om pl ex r,i ; } Ad d(d ou bl e r, do ub le i) va l co mp le x(r i) v oi d o pe ra to r()(c om pl ex c c on st { c += v al } vo id op er at or co mp le x& c) co ns t va l; // add value to argument }; An object of class A dd is initialized with a complex number, and when invoked using (), it adds Ad d that number to its argument. For example: v oi d h ve ct or co mp le x>& a a, l is t<c om pl ex vo id h(v ec to r<c om pl ex aa li st co mp le x>& l l, c om pl ex z ll co mp le x z) { f or _e ac h(a a.b eg in ,a a.e nd ,A dd 2,3 ; fo r_ ea ch aa be gi n() aa en d() Ad d(2 3)) f or _e ac h(l l.b eg in ,l l.e nd ,A dd z)); fo r_ ea ch ll be gi n() ll en d() Ad d(z } This will add c om pl ex 2,3 to every element of the array and z to every element on the list. Note co mp le x(2 3) that A dd z) constructs an object that is used repeatedly by f or _e ac h(). It is not simply a function Ad d(z fo r_ ea ch that is called once or even called repeatedly. The function that is called repeatedly is A dd z)’s Ad d(z o pe ra to r()(). op er at or This all works because f or _e ac h is a template that applies () to its third argument without car- fo r_ ea ch ing exactly what that third argument really is: t em pl at e<c la ss I te r, c la ss F ct I te r f or _e ac h(I te r b I te r e F ct f te mp la te cl as s It er cl as s Fc t> It er fo r_ ea ch It er b, It er e, Fc t f) { w hi le (b != e f b++); wh il e b e) f(*b r et ur n b re tu rn b; } At first glance, this technique may look esoteric, but it is simple, efficient, and extremely useful (see §3.8.5, §18.4).
    299. Section 11.9 Function Call 289 Other popular uses of o pe ra to r()() are as a substring operator and as a subscripting operator op er at or for multidimensional arrays (§22.4.5). An o pe ra to r()() must be a member function. op er at or 11.10 Dereferencing [over.deref] The dereferencing operator -> can be defined as a unary postfix operator. That is, given a class c la ss P tr { cl as s Pt r // ... X o pe ra to r->(); X* op er at or }; objects of class P tr can be used to access members of class X in a very similar manner to the way Pt r pointers are used. For example: v oi d f Pt r p vo id f(P tr p) { // (p.operator– >())– >m = 7 p m=7 p->m 7; } The transformation of the object p into the pointer p op er at or p.o pe ra to r->() does not depend on the mem- ber m pointed to. That is the sense in which o pe ra to r->() is a unary postfix operator. However, op er at or there is no new syntax introduced, so a member name is still required after the ->. For example: v oi d g Pt r p vo id g(P tr p) { X q1 = p X* q1 p->; // syntax error X q 2 = p op er at or X* q2 p.o pe ra to r->(); // ok } Overloading -> is primarily useful for creating ‘‘smart pointers,’’ that is, objects that act like point- ers and in addition perform some action whenever an object is accessed through them. For exam- ple, one could define a class R ec _p tr for accessing objects of class R ec stored on disk. R ec _p tr Re c_ pt r Re c Re c_ pt r’s constructor takes a name that can be used to find the object on disk, R ec _p tr :o pe ra to r->() Re c_ pt r: op er at or brings the object into main memory when accessed through its R ec _p tr and R ec _p tr destructor Re c_ pt r, Re c_ pt r’s eventually writes the updated object back out to disk: c la ss R ec _p tr { cl as s Re c_ pt r R ec i n_ co re _a dd re ss Re c* in _c or e_ ad dr es s; c on st c ha r* i de nt if ie r; co ns t ch ar id en ti fi er // ... p ub li c: pu bl ic R ec _p tr co ns t c ha r* p : i de nt if ie r(p , i n_ co re _a dd re ss 0) { } Re c_ pt r(c on st ch ar p) id en ti fi er p) in _c or e_ ad dr es s(0 ~R ec _p tr Re c_ pt r() { w ri te _t o_ di sk in _c or e_ ad dr es s,i de nt if ie r); } wr it e_ to _d is k(i n_ co re _a dd re ss id en ti fi er R ec o pe ra to r->(); Re c* op er at or };
    300. 290 Operator Overloading Chapter 11 R ec R ec _p tr :o pe ra to r->() Re c* Re c_ pt r: op er at or { i f (i n_ co re _a dd re ss == 0 i n_ co re _a dd re ss = r ea d_ fr om _d is k(i de nt if ie r); if in _c or e_ ad dr es s 0) in _c or e_ ad dr es s re ad _f ro m_ di sk id en ti fi er r et ur n i n_ co re _a dd re ss re tu rn in _c or e_ ad dr es s; } R ec _p tr might be used like this: Re c_ pt r s tr uc t R ec { st ru ct Re c // the Rec that a Rec_ptr points to s tr in g n am e; st ri ng na me // ... }; v oi d u pd at e(c on st c ha r* s vo id up da te co ns t ch ar s) { R ec _p tr p s); Re c_ pt r p(s // get Rec_ptr for s p na me = \"R os co e\"; p->n am e Ro sc oe // update s; if necessary, first retrieve from disk // ... } Naturally, a real R ec _p tr would be a template so that the R ec type is a parameter. Also, a realistic Re c_ pt r Re c program would contain error-handling code and use a less naive way of interacting with the disk. For ordinary pointers, use of -> is synonymous with some uses of unary * and []. Given Yp Y* p; it holds that p m == (*p m == p 0].m p->m p).m p[0 m As usual, no such guarantee is provided for user-defined operators. The equivalence can be pro- vided where desired: c la ss P tr _t o_ Y { cl as s Pt r_ to _Y Yp Y* p; p ub li c: pu bl ic Y o pe ra to r->() { r et ur n p } Y* op er at or re tu rn p; Y o pe ra to r*() { r et ur n *p } Y& op er at or re tu rn p; Y o pe ra to r[](i nt i { r et ur n p i]; } Y& op er at or in t i) re tu rn p[i }; If you provide more than one of these operators, it might be wise to provide the equivalence, just as it is wise to ensure that ++x and x 1 have the same effect as x x+1 for a simple variable x of x x+=1 x=x 1 some class if ++, +=, =, and + are provided. The overloading of -> is important to a class of interesting programs and not just a minor curiosity. The reason is that indirection is a key concept and that overloading -> provides a clean, direct, and efficient way of representing indirection in a program. Iterators (Chapter 19) provide an important example of this. Another way of looking at operator -> is to consider it as a way of pro- viding C++ with a limited, but useful, form of delegation (§24.2.4). Operator -> must be a member function. If used, its return type must be a pointer or an object of a class to which you can apply ->. When declared for a template class, o pe ra to r->() is op er at or
    301. Section 11.10 Dereferencing 291 frequently unused, so it makes sense to postpone checking the constraint on the return type until actual use. 11.11 Increment and Decrement [over.incr] Once people invent ‘‘smart pointers,’’ they often decide to provide the increment operator ++ and the decrement operator -- to mirror these operators’ use for built-in types. This is especially obvi- ous and necessary where the aim is to replace an ordinary pointer type with a ‘‘smart pointer’’ type that has the same semantics, except that it adds a bit of run-time error checking. For example, con- sider a troublesome traditional program: v oi d f 1(T a vo id f1 T a) // traditional use { T v 20 0]; v[2 00 T p = &v 0]; T* v[0 p p--; *p = a p a; // Oops: ‘p’ out of range, uncaught p; ++p *p = a p a; // ok } We might want to replace the pointer p with an object of a class P tr _t o_ T that can be dereferenced Pt r_ to _T only provided it actually points to an object. We would also like to ensure that p can be incre- mented and decremented, only provided it points to an object within an array and the increment and decrement operations yield an object within the array. That is we would like something like this: c la ss P tr _t o_ T { cl as s Pt r_ to _T // ... }; v oi d f 2(T a vo id f2 T a) // checked { T v 20 0]; v[2 00 P tr _t o_ T p v[0 ,v 20 0); Pt r_ to _T p(&v 0] v,2 00 p p--; *p = a p a; // run-time error: ‘p’ out of range p; ++p *p = a p a; // ok } The increment and decrement operators are unique among C++ operators in that they can be used as both prefix and postfix operators. Consequently, we must define prefix and postfix increment and decrement P tr _t o_ T. For example: Pt r_ to _T
    302. 292 Operator Overloading Chapter 11 c la ss P tr _t o_ T { cl as s Pt r_ to _T Tp T* p; T a rr ay T* ar ra y; i nt s iz e; in t si ze p ub li c: pu bl ic P tr _t o_ T(T p T v i nt s ; Pt r_ to _T T* p, T* v, in t s) // bind to array v of size s, initial value p P tr _t o_ T(T p ; Pt r_ to _T T* p) // bind to single object, initial value p P tr _t o_ T& o pe ra to r++(); Pt r_ to _T op er at or // prefix P tr _t o_ T o pe ra to r++(i nt ; Pt r_ to _T op er at or in t) // postfix P tr _t o_ T& o pe ra to r--(); Pt r_ to _T op er at or // prefix P tr _t o_ T o pe ra to r--(i nt ; Pt r_ to _T op er at or in t) // postfix T o pe ra to r*(); T& op er at or // prefix }; The i nt argument is used to indicate that the function is to be invoked for postfix application of ++. in t This i nt is never used; the argument is simply a dummy used to distinguish between prefix and in t postfix application. The way to remember which version of an o pe ra to r++ is prefix is to note that op er at or the version without the dummy argument is prefix, exactly like all the other unary arithmetic and logical operators. The dummy argument is used only for the ‘‘odd’’ postfix ++ and --. Using P tr _t o_ T, the example is equivalent to: Pt r_ to _T v oi d f 3(T a vo id f3 T a) // checked { T v 20 0]; v[2 00 P tr _t o_ T p v[0 ,v 20 0); Pt r_ to _T p(&v 0] v,2 00 p op er at or p.o pe ra to r--(0 ; 0) p op er at or p.o pe ra to r*() = a // run-time error: ‘p’ out of range a; p op er at or p.o pe ra to r++(); p op er at or p.o pe ra to r*() = a // ok a; } Completing class P tr _t o_ T is left as an exercise (§11.14[19]). Its elaboration into a template using Pt r_ to _T exceptions to report the run-time errors is another exercise (§14.12[2]). An example of operators ++ and -- for iteration can be found in §19.3. A pointer template that behaves correctly with respect to inheritance is presented in (§13.6.3). 11.12 A String Class [over.string] Here is a more realistic version of class S tr in g. I designed it as the minimal string that served my St ri ng needs. This string provides value semantics, character read and write operations, checked and unchecked access, stream I/O, literal strings as literals, and equality and concatenation operators. It represents strings as C-style, zero-terminated arrays of characters and uses reference counts to mini- mize copying. Writing a better string class and/or one that provides more facilities is a good exer- cise (§11.14[7-12]). That done, we can throw away our exercises and use the standard library string (Chapter 20).
    303. Section 11.12 A String Class 293 My almost-real S tr in g employs three auxiliary classes: S re p, to allow an actual representation St ri ng Sr ep to be shared between several S tr in gs with the same value; R an ge to be thrown in case of range St ri ng Ra ng e, errors, and C re f, to help implement a subscript operator that distinguishes between reading and Cr ef writing: c la ss S tr in g { cl as s St ri ng s tr uc t S re p; st ru ct Sr ep // representation S re p *r ep Sr ep re p; p ub li c: pu bl ic c la ss C re f; cl as s Cr ef // reference to char c la ss R an ge { }; cl as s Ra ng e // for exceptions // ... }; Like other members, a member class (often called a nested class) can be declared in the class itself and defined later: s tr uc t S tr in g::S re p { st ru ct St ri ng Sr ep c ha r* s ch ar s; // pointer to elements i nt s z; in t sz // number of characters i nt n in t n; // reference count S re p(i nt n sz c on st c ha r* p Sr ep in t ns z, co ns t ch ar p) { n=1 1; s z = n sz sz ns z; s = n ew c ha r[s z+1 ; // add space for terminator ne w ch ar sz 1] s tr cp y(s p); st rc py s,p } ~S re p() { d el et e[] s } Sr ep de le te s; S re p* g et _o wn _c op y() Sr ep ge t_ ow n_ co py // clone if necessary { i f (n 1) r et ur n t hi s; if n==1 re tu rn th is n n--; r et ur n n ew S re p(s z,s ; re tu rn ne w Sr ep sz s) } v oi d a ss ig n(i nt n sz c on st c ha r* p vo id as si gn in t ns z, co ns t ch ar p) { i f (s z != n sz { if sz ns z) d el et e[] s de le te s; s z = n sz sz ns z; s = n ew c ha r[s z+1 ; ne w ch ar sz 1] } s tr cp y(s p); st rc py s,p }
    304. 294 Operator Overloading Chapter 11 p ri va te pr iv at e: // prevent copying: S re p(c on st S re p&); Sr ep co ns t Sr ep S re p& o pe ra to r=(c on st S re p&); Sr ep op er at or co ns t Sr ep }; Class S tr in g provides the usual set of constructors, destructor, and assignment operations: St ri ng c la ss S tr in g { cl as s St ri ng // ... S tr in g(); St ri ng // x = \"\" S tr in g(c on st c ha r*); St ri ng co ns t ch ar // x = \"abc\" S tr in g(c on st S tr in g&); St ri ng co ns t St ri ng // x = other_string S tr in g& o pe ra to r=(c on st c ha r *); St ri ng op er at or co ns t ch ar S tr in g& o pe ra to r=(c on st S tr in g&); St ri ng op er at or co ns t St ri ng ~S tr in g(); St ri ng // ... }; This S tr in g has value semantics. That is, after an assignment s 1=s 2, the two strings s 1 and s 2 are St ri ng s1 s2 s1 s2 fully distinct and subsequent changes to the one have no effect on the other. The alternative would be to give S tr in g pointer semantics. That would be to let changes to s 2 after s 1=s 2 also affect the St ri ng s2 s1 s2 value of s 1. For types with conventional arithmetic operations, such as complex, vector, matrix, s1 and string, I prefer value semantics. However, for the value semantics to be affordable, a S tr in g is St ri ng implemented as a handle to its representation and the representation is copied only when necessary: S tr in g::S tr in g() St ri ng St ri ng // the empty string is the default value { r ep = n ew S re p(0 re p ne w Sr ep 0,\"\"); } S tr in g::S tr in g(c on st S tr in g& x // copy constructor St ri ng St ri ng co ns t St ri ng x) { x re p->n x.r ep n++; r ep = x re p; re p x.r ep // share representation } S tr in g::~S tr in g() St ri ng St ri ng { i f (--r ep n == 0 d el et e r ep if re p->n 0) de le te re p; } S tr in g& S tr in g::o pe ra to r=(c on st S tr in g& x St ri ng St ri ng op er at or co ns t St ri ng x) // copy assignment { x re p->n x.r ep n++; // protects against ‘‘st = st’’ i f (--r ep n == 0 d el et e r ep if re p->n 0) de le te re p; r ep = x re p; re p x.r ep // share representation r et ur n *t hi s; re tu rn th is } Pseudo-copy operations taking c on st c ha r* arguments are provided to allow string literals: co ns t ch ar
    305. Section 11.12 A String Class 295 S tr in g::S tr in g(c on st c ha r* s St ri ng St ri ng co ns t ch ar s) { r ep = n ew S re p(s tr le n(s ,s ; re p ne w Sr ep st rl en s) s) } S tr in g& S tr in g::o pe ra to r=(c on st c ha r* s St ri ng St ri ng op er at or co ns t ch ar s) { i f (r ep n == 1 if re p->n 1) // recycle Srep r ep as si gn st rl en s),s ; re p->a ss ig n(s tr le n(s s) e ls e { el se // use new Srep r ep n--; re p->n r ep = n ew S re p(s tr le n(s ,s ; re p ne w Sr ep st rl en s) s) } r et ur n *t hi s; re tu rn th is } The design of access operators for a string is a difficult topic because ideally access is by conven- tional notation (that is, using []), maximally efficient, and range checked. Unfortunately, you can- not have all of these properties simultaneously. My choice here has been to provide efficient unchecked operations with a slightly inconvenient notation plus slightly less efficient checked oper- ators with the conventional notation: c la ss S tr in g { cl as s St ri ng // ... v oi d c he ck in t i c on st { i f (i 0 || r ep sz i) t hr ow R an ge ; } vo id ch ec k(i nt i) co ns t if i<0 re p->s z<=i th ro w Ra ng e() c ha r r ea d(i nt i c on st { r et ur n r ep s[i ; } ch ar re ad in t i) co ns t re tu rn re p->s i] v oi d w ri te in t i c ha r c { r ep re p->g et _o wn _c op y(); r ep s[i c; } vo id wr it e(i nt i, ch ar c) re p=r ep ge t_ ow n_ co py re p->s i]=c C re f o pe ra to r[](i nt i { c he ck i); r et ur n C re f(*t hi s,i ; } Cr ef op er at or in t i) ch ec k(i re tu rn Cr ef th is i) c ha r o pe ra to r[](i nt i c on st { c he ck i); r et ur n r ep s[i ; } ch ar op er at or in t i) co ns t ch ec k(i re tu rn re p->s i] i nt s iz e() c on st { r et ur n r ep sz } in t si ze co ns t re tu rn re p->s z; // ... }; The idea is to use [] to get checked access for ordinary use, but to allow the user to optimize by checking the range once for a set of accesses. For example: i nt h as h(c on st S tr in g& s in t ha sh co ns t St ri ng s) { i nt h = s re ad 0); in t s.r ea d(0 c on st i nt m ax = s si ze ; co ns t in t ma x s.s iz e() f or (i nt i = 1 i ma x; i fo r in t 1; i<m ax i++) h ^= s re ad i)>>1 // unchecked access to s s.r ea d(i 1; r et ur n h re tu rn h; } Defining an operator, such as [], to be used for both reading and writing is difficult where it is not acceptable simply to return a reference and let the user decide what to do with it. Here, that is not a reasonable alternative because I have defined S tr in g so that the representation is shared between St ri ng S tr in gs that have been assigned, passed as value arguments, etc., until someone actually writes to a St ri ng
    306. 296 Operator Overloading Chapter 11 S tr in g. Then, and only then, is the representation copied. This technique is usually called copy- St ri ng on-write. The actual copy is done by S tr in g::S re p::g et _o wn _c op y(). St ri ng Sr ep ge t_ ow n_ co py To get these access functions inlined, their definitions must be placed so that the definition of S re p is in scope. This implies that either S re p is defined within S tr in g or the access functions are Sr ep Sr ep St ri ng defined i nl in e outside S tr in g and after S tr in g::S re p (§11.14[2]). in li ne St ri ng St ri ng Sr ep To distinguish between a read and a write, S tr in g::o pe ra to r[]() returns a C re f when called St ri ng op er at or Cr ef for a non-c on st object. A C re f behaves like a c ha r&, except that it calls co ns t Cr ef ch ar S tr in g::S re p::g et _o wn _c op y() when written to: St ri ng Sr ep ge t_ ow n_ co py c la ss S tr in g::C re f { cl as s St ri ng Cr ef // reference to s[i] f ri en d c la ss S tr in g; fr ie nd cl as s St ri ng S tr in g& s St ri ng s; i nt i in t i; C re f(S tr in g& s s, i nt i i) : s ss , i ii { } Cr ef St ri ng ss in t ii s(s s) i(i i) p ub li c: pu bl ic o pe ra to r c ha r() { r et ur n s re ad i); } op er at or ch ar re tu rn s.r ea d(i // yield value v oi d o pe ra to r=(c ha r c { s wr it e(i c); } vo id op er at or ch ar c) s.w ri te i,c // change value }; For example: v oi d f St ri ng s c on st S tr in g& r vo id f(S tr in g s, co ns t St ri ng r) { i nt c 1 = s 1]; // c1 = s.operator[](1).operator char() in t c1 s[1 s 1] = ´c s[1 c´; // s.operator[](1).operator=(’c’) i nt c 2 = r 1]; // c2 = r.operator[](1) in t c2 r[1 r 1] = ´d r[1 d´; // error: assignment to char, r.operator[](1) = ’d’ } Note that for a non-c on st object s op er at or co ns t s.o pe ra to r[](1 is C re f(s 1). 1) Cr ef s,1 To complete class S tr in g, I provide a set of useful functions: St ri ng c la ss S tr in g { cl as s St ri ng // ... S tr in g& o pe ra to r+=(c on st S tr in g&); St ri ng op er at or co ns t St ri ng S tr in g& o pe ra to r+=(c on st c ha r*); St ri ng op er at or co ns t ch ar f ri en d o st re am o pe ra to r<<(o st re am fr ie nd os tr ea m& op er at or os tr ea m&, c on st S tr in g&); co ns t St ri ng f ri en d i st re am o pe ra to r>>(i st re am fr ie nd is tr ea m& op er at or is tr ea m&, S tr in g&); St ri ng f ri en d b oo l o pe ra to r==(c on st S tr in g& x c on st c ha r* s fr ie nd bo ol op er at or co ns t St ri ng x, co ns t ch ar s) { r et ur n s tr cm p(x re p->s s == 0 } re tu rn st rc mp x.r ep s, s) 0; f ri en d b oo l o pe ra to r==(c on st S tr in g& x c on st S tr in g& y fr ie nd bo ol op er at or co ns t St ri ng x, co ns t St ri ng y) { r et ur n s tr cm p(x re p->s y re p->s == 0 } re tu rn st rc mp x.r ep s, y.r ep s) 0; f ri en d b oo l o pe ra to r!=(c on st S tr in g& x c on st c ha r* s fr ie nd bo ol op er at or co ns t St ri ng x, co ns t ch ar s) { r et ur n s tr cm p(x re p->s s != 0 } re tu rn st rc mp x.r ep s, s) 0;
    307. Section 11.12 A String Class 297 f ri en d b oo l o pe ra to r!=(c on st S tr in g& x c on st S tr in g& y fr ie nd bo ol op er at or co ns t St ri ng x, co ns t St ri ng y) { r et ur n s tr cm p(x re p->s y re p->s != 0 } re tu rn st rc mp x.r ep s, y.r ep s) 0; }; S tr in g o pe ra to r+(c on st S tr in g&, c on st S tr in g&); St ri ng op er at or co ns t St ri ng co ns t St ri ng S tr in g o pe ra to r+(c on st S tr in g&, c on st c ha r*); St ri ng op er at or co ns t St ri ng co ns t ch ar To save space, I have left the I/O and concatenation operations as exercises. The main program simply exercises the S tr in g operators a bit: St ri ng S tr in g f St ri ng a S tr in g b St ri ng f(S tr in g a, St ri ng b) { a 2] = ´x a[2 x´; c ha r c = b 3]; ch ar b[3 c ou t << \"i n f \" << a << ´ ´ << b << ´ ´ << c << ´\\ n´; co ut in f: \\n r et ur n b re tu rn b; } i nt m ai n() in t ma in { S tr in g x y St ri ng x, y; c ou t << \"P le as e e nt er t wo s tr in gs \\n co ut Pl ea se en te r tw o st ri ng s\\ n\"; c in >> x >> y ci n y; c ou t << \"i np ut \" << x << ´ ´ << y << ´\\ n´; co ut in pu t: \\n S tr in g z = x St ri ng x; y = f x,y ; f(x y) i f (x != z c ou t << \"x c or ru pt ed \\n if x z) co ut x co rr up te d!\\ n\"; x 0] = ´!´; x[0 i f (x == z c ou t << \"w ri te f ai le d!\\ n\"; if x z) co ut wr it e fa il ed \\n c ou t << \"e xi t: \" << x << ´ ´ << y << ´ ´ << z << ´\\ n´; co ut ex it \\n } This S tr in g lacks many features that you might consider important or even essential. For example, St ri ng it offers no operation of producing a C-string representation of its value (§11.14[10], Chapter 20). 11.13 Advice [class.advice] [1] Define operators primarily to mimic conventional usage; §11.1. [2] For large operands, use c on st reference argument types; §11.6. co ns t [3] For large results, consider optimizing the return; §11.6. [4] Prefer the default copy operations if appropriate for a class; §11.3.4. [5] Redefine or prohibit copying if the default is not appropriate for a type; §11.2.2. [6] Prefer member functions over nonmembers for operations that need access to the representa- tion; §11.5.2. [7] Prefer nonmember functions over members for operations that do not need access to the repre- sentation; §11.5.2. [8] Use namespaces to associate helper functions with ‘‘their’’ class; §11.2.4. [9] Use nonmember functions for symmetric operators; §11.3.2. [10] Use () for subscripting multidimensional arrays; §11.9.
    308. 298 Operator Overloading Chapter 11 [11] Make constructors that take a single ‘‘size argument’’ e xp li ci t; §11.7.1. ex pl ic it [12] For non-specialized uses, prefer the standard s tr in g (Chapter 20) to the result of your own st ri ng exercises; §11.12. [13] Be cautious about introducing implicit conversions; §11.4. [14] Use member functions to express operators that require an lvalue as its left-hand operand; §11.3.5. 11.14 Exercises [over.exercises] 1. (∗2) In the following program, which conversions are used in each expression? s tr uc t X { st ru ct i nt i in t i; X in t); X(i nt o pe ra to r+(i nt ; op er at or in t) }; s tr uc t Y { st ru ct i nt i in t i; Y X); Y(X o pe ra to r+(X ; op er at or X) o pe ra to r i nt ; op er at or in t() }; e xt er n X o pe ra to r*(X Y ; ex te rn op er at or X, Y) e xt er n i nt f X); ex te rn in t f(X X x=1 1; Y y=x x; i nt i = 2 in t 2; i nt m ai n() in t ma in { i + 1 0; 10 y + 1 0; 10 y + 10 * y 10 y; x+y+i i; x*x+i i; f 7); f(7 f y); f(y y+y y; 1 06 + y 10 6 y; } Modify the program so that it will run and print the values of each legal expression. 2. (∗2) Complete and test class S tr in g from §11.12. St ri ng 3. (∗2) Define a class I NT that behaves exactly like an i nt Hint: Define I NT :o pe ra to r i nt IN T in t. IN T: op er at or in t(). 4. (∗1) Define a class R IN T that behaves like an i nt except that the only operations allowed are + RI NT in t (unary and binary), - (unary and binary), *, /, and %. Hint: Do not define R IN T::o pe ra to r RI NT op er at or i nt in t(). 5. (∗3) Define a class L IN T that behaves like a R IN T, except that it has at least 64 bits of preci- LI NT RI NT sion. 6. (∗4) Define a class implementing arbitrary precision arithmetic. Test it by calculating the facto- rial of 1 00 0. Hint: You will need to manage storage in a way similar to what was done for class 10 00 S tr in g. St ri ng
    309. Section 11.14 Exercises 299 7. (∗2) Define an external iterator for class S tr in g: St ri ng cl as s St ri ng _i te r c la ss S tr in g_ it er { // refer to string and string element p ub li c: pu bl ic St ri ng _i te r(S tr in g& s) S tr in g_ it er St ri ng s ; // iterator for s c ha r& n ex t(); ch ar ne xt // reference to next element // more operations of your choice }; Compare this in utility, programming style, and efficiency to having an internal iterator for S tr in g (that is, a notion of a current element for the S tr in g and operations relating to that ele- St ri ng St ri ng ment). 8. (∗1.5) Provide a substring operator for a string class by overloading (). What other operations would you like to be able to do on a string? 9. (∗3) Design class S tr in g so that the substring operator can be used on the left-hand side of an St ri ng assignment. First, write a version in which a string can be assigned to a substring of the same length. Then, write a version in which the lengths may differ. 10. (∗2) Define an operation for S tr in g that produces a C-string representation of its value. Discuss St ri ng the pros and cons of having that operation as a conversion operator. Discuss alternatives for allocating the memory for that C-string representation. 11. (∗2.5) Define and implement a simple regular expression pat