Index Operators are listed under operator. That is, operator<< is listed under operator<<, not under <<, etc. Example classes, structs, and class or struct templates are indexed under example classes/templates. Example function and function templates are indexed under example functions/templates. Before A alternatives to virtual functions 169–177 ambiguity .NET 7, 81, 135, 145, 194 multiple inheritance and 192 see also C# nested dependent names and types 205 =, in initialization vs. assignment 6 Arbiter, Petronius vii ptg7544714 1066 150 argument-dependent lookup 110 2nd edition of this book arithmetic, mixed-mode 103, 222–226 compared to 3rd edition xv–xvi, 277–279 array layout, vs. object layout 73 see also inside back cover array new 254–255 3rd edition of this book array, invalid index and 7 compared to 2nd edition xv–xvi, 277–279 see also inside back cover ASPECT_RATIO 13 assignment 80-20 rule 139, 168 see also operator= chaining assignments 52 A copy-and-swap and 56 generalized 220 Abrahams, David xvii, xviii, xix to self, operator= and 53–57 abstract classes 43 vs. initialization 6, 27–29, 114 accessibility assignment operator, copy 5 control over data members’ 95 auto_ptr, see std::auto_ptr name, multiple inheritance and 193 automatically generated functions 34–37 accessing names, in templatized copy constructor and copy assignment bases 207–212 operator 221 addresses disallowing 37–39 inline functions 136 avoiding code duplication 50, 60 objects 118 aggregation, see composition Alexandrescu, Andrei xix B aliasing 54 Bai, Yun xix alignment 249–250 Barry, Dave, allusion to 229 allocators, in the STL 240 Bartolucci, Guido xix
Effective C++ Index 281 base classes TR1 and 9–10, 268, 269 copying 59 typelist support 271 duplication of data in 193 web site 10, 269, 272 lookup in, this-> and 210 boost, as synonym for std::tr1 268 names hidden in derived classes 263 Bosch, Derek xviii polymorphic 44 breakpoints, and inlining 139 polymorphic, destructors and 40–44 Buffy the Vampire Slayer xx templatized 207–212 bugs, reporting xvi virtual 193 built-in types 26–27 basic guarantee, the 128 efficiency and passing 89 Battle of Hastings 150 incompatibilities with 80 Berck, Benjamin xix bidirectional iterators 227 bidirectional_iterator_tag 228 C binary upgradeability, inlining and 138 C standard library and C++ standard binding library 264 dynamic, see dynamic binding C# 43, 76, 97, 100, 116, 118, 190 static, see static binding see also .NET birds and penguins 151–153 C++ Programming Language, The xvii bitwise const member functions 21–22 C++ standard library 263–269 books <iosfwd> and 144 C++ Programming Language, The xvii array replacements and 75 C++ Templates xviii C standard library and 264 Design Patterns xvii C89 standard library and 264 Effective STL 273, 275–276 header organization of 101 Exceptional C++ xvii list template 186 Exceptional C++ Style xvii, xviii logic_error and 113 More Effective C++ 273, 273–274 set template 185 More Exceptional C++ xvii ptg7544714 vector template 75 Satyricon vii C++ Templates xviii Some Must Watch While Some Must C++, as language federation 11–13 Sleep 150 C++0x 264 Boost 10, 269–272 C++-style casts 117 containers 271 Conversion library 270 C, as sublanguage of C++ 12 correctness and testing support 272 C99 standard library, TR1 and 267 data structures 272 caching function objects and higher-order pro- const and 22 gramming utilities 271 mutable and 22 functionality not provided 272 Cai, Steve xix generic programming support 271 calling swap 110 Graph library 270 calls to base classes, casting and 119 inter-language support 272 Cargill, Tom xviii Lambda library 271 Carrara, Enrico xix math and numerics utilities 271 Carroll, Glenn xviii memory management utilities 272 MPL library 270, 271 casting 116–123 noncopyable base class 39 see also const_cast, static_cast, Pool library 250, 251 dynamic_cast, and reinterpret_cast scoped_array 65, 216, 272 base class calls and 119 shared_array 65 constness away 24–25 shared_ptr implementation, costs 83 encapsulation and 123 smart pointers 65, 272 grep and 117 web page xvii syntactic forms 116–117 type systems and 116 string and text utilities 271 template metaprogramming undefined behavior and 119 support 271 chaining assignments 52
282 Index Effective C++ Chang, Brandon xix compiler-generated functions 34–37 Clamage, Steve xviii disallowing 37–39 class definitions functions compilers may generate 221 artificial client dependencies, compilers eliminating 143 parsing nested dependent names 204 class declarations vs. 143 programs executing within, see tem- object sizes and 141 plate metaprogramming class design, see type design register usage and 89 reordering operations 76 class names, explicitly specifying 162 typename and 207 class, vs. typename 203 when errors are diagnosed 212 classes compile-time polymorphism 201 see also class definitions, interfaces composition 184–186 abstract 43, 162 base meanings of 184 replacing private inheritance with 189 see also base classes synonyms for 184 duplication of data in 193 vs. private inheritance 188 polymorphic 44 conceptual constness, see const, logical templatized 207–212 consistency with the built-in types 19, 86 virtual 193 const 13, 17–26 defining 4 bitwise 21–22 derived caching and 22 see also inheritance casting away 24–25 virtual base initialization of 194 function declarations and 18 Handle 144–145 logical 22–23 Interface 145–147 member functions 19–25 meaning of no virtual functions 41 duplication and 23–25 RAII, see RAII members, initialization of 29 specification, see interfaces overloading on 19–20 traits 226–232 ptg7544714 pass by reference and 86–90 client 7 passing std::auto_ptr and 220 clustering objects 251 pointers 17 code return value 18 bloat 24, 135, 230 uses 17 avoiding, in templates 212–217 vs. #define 13–14 copy assignment operator 60 const_cast 25, 117 duplication, see duplication see also casting exception-safe 127–134 const_iterator, vs. iterators 18 factoring out of templates 212–217 constants, see const incorrect, efficiency and 90 constraints on interfaces, from reuse 195 inheritance 85 sharing, see duplication, avoiding constructors 84 Cohen, Jake xix copy 5 Comeau, Greg xviii default 4 URL for his C/C++ FAQ xviii empty, illusion of 137 common features and inheritance 164 explicit 5, 85, 104 commonality and variability analysis 212 implicitly generated 34 compatibility, vptrs and 42 inlining and 137–138 compatible types, accepting 218–222 operator new and 137 possible implementation in derived compilation dependencies 140–148 classes 138 minimizing 140–148, 190 pointers, references, and objects relationship to new 73 static functions and 52 and 143 virtual 146, 147 compiler warnings 262–263 virtual functions and 48–52 calls to virtuals and 50 with vs. without arguments 114 inlining and 136 containers, in Boost 271 partial copies and 58
Effective C++ Index 283 containment, see composition deadly MI diamond 193 continue, delete and 62 debuggers control over data members’ #define and 13 accessibility 95 inline functions and 139 convenience functions 100 declarations 3 Conversion library, in Boost 270 inline functions 135 conversions, type, see type conversions replacing definitions 143 static const integral members 14 copies, partial 58 default constructors 4 copy assignment operator 5 construction with arguments vs. 114 code in copy constructor and 60 implicitly generated 34 derived classes and 60 copy constructors default implementations for virtual functions, danger of 163–167 default definition 35 of copy constructor 35 derived classes and 60 of operator= 35 generalized 219 default initialization, unintended 59 how used 5 implicitly generated 34 default parameters 180–183 pass-by-value and 6 impact if changed 183 static binding of 182 copy-and-swap 131 assignment and 56 #define exception-safe code and 132 debuggers and 13 copying disadvantages of 13, 16 base class parts 59 vs. const 13–14 behavior, resource management vs. inline functions 16–17 and 66–69 definitions 4 functions, the 57 classes 4 objects 57–60 deliberate omission of 38 correctness functions 4 designing interfaces for 78–83 implicitly generated functions 35 ptg7544714 testing and, Boost support 272 objects 4 corresponding forms of new and pure virtual functions 162, 166–167 replacing with declarations 143 delete 73–75 static class members 242 corrupt data structures, exception-safe static const integral members 14 code and 127 templates 4 cows, coming home 139 variable, postponing 113–116 crimes against English 39, 204 delete cross-DLL problem 82 see also operator delete CRTP 246 forms of 73–75 C-style casts 116 operator delete and 73 ctor 8 relationship to destructors 73 curiously recurring template pattern 246 usage problem scenarios 62 delete [], std::auto_ptr and tr1::shared_ptr and 65 D deleters std::auto_ptr and 68 dangling handles 126 tr1::shared_ptr and 68, 81–83 Dashtinezhad, Sasan xix Delphi 97 data members Dement, William 150 adding, copying functions and 58 control over accessibility 95 dependencies, compilation 140–148 protected 97 dependent names 204 static, initialization of 242 dereferencing a null pointer, undefined why private 94–98 behavior of 6 data structures derived classes exception-safe code and 127 copy assignment operators and 60 in Boost 272 copy constructors and 60 Davis, Tony xviii hiding names in base classes 263
284 Index Effective C++ implementing constructors in 138 contents of 275–276 virtual base initialization and 194 efficiency design assignment vs. construction and contradiction in 179 destruction 94 of interfaces 78–83 default parameter binding 182 of types 78–86 dynamic_cast 120 Design Patterns xvii Handle classes 147 design patterns incorrect code and 90, 94 curiously recurring template init. with vs. without args 114 (CRTP) 246 Interface classes 147 encapsulation and 173 macros vs. inline functions 16 generating from templates 237 member init. vs. assignment 28 Singleton 31 minimizing compilation Strategy 171–177 dependencies 147 Template Method 170 operator new/operator delete and 248 TMP and 237 pass-by-reference and 87 pass-by-value and 86–87 destructors 84 passing built-in types and 89 exceptions and 44–48 runtime vs. compile-time tests 230 inlining and 137–138 template metaprogramming and 233 pure virtual 43 template vs. function parameters 216 relationship to delete 73 unused objects 113 resource managing objects and 63 virtual functions 168 static functions and 52 virtual Eiffel 100 operator delete and 255 embedding, see composition polymorphic base classes and 40–44 empty base optimization (EBO) 190–191 virtual functions and 48–52 encapsulation 95, 99 Dewhurst, Steve xvii casts and 123 dimensional unit correctness, TMP design patterns and 173 ptg7544714 handles and 125 and 236 measuring 99 DLLs, delete and 82 protected members and 97 dtor 8 RAII classes and 72 Dulimov, Peter xix enum hack 15–16, 236 duplication errata list, for this book xvi avoiding 23–25, 29, 50, 60, 164, 183, 212– 217 errors detected during linking 39, 44 base class data and 193 runtime 152 init function and 60 evaluation order, of parameters 76 dynamic binding definition of 181 example classes/templates A 4 of virtual functions 179 ABEntry 27 dynamic type, definition of 181 AccessLevels 95 dynamic_cast 50, 117, 120–123 Address 184 see also casting Airplane 164, 165, 166 efficiency of 120 Airport 164 AtomicClock 40 E AWOV 43 B 4, 178, 262 early binding 180 Base 54, 118, 137, 157, 158, 159, 160, 254, easy to use correctly and hard to use 255, 259 incorrectly 78–83 BelowBottom 219 EBO, see empty base optimization bidirectional_iterator_tag 228 Effective C++, compared to More Effective Bird 151, 152, 153 Bitmap 54 C++ and Effective STL 273 BorrowableItem 192 Effective STL 273, 275–276 Bottom 218 compared to Effective C++ 273 BuyTransaction 49, 51
Effective C++ Index 285 C 5 MsgInfo 208 Circle 181 MsgSender 208 CompanyA 208 MsgSender<CompanyZ> 209 CompanyB 208 NamedObject 35, 36 CompanyZ 209 NewHandlerHolder 243 CostEstimate 15 NewHandlerSupport 245 CPerson 198 output_iterator_tag 228 CTextBlock 21, 22, 23 OutputFile 193, 194 Customer 57, 58 Penguin 151, 152, 153 D 178, 262 Person 86, 135, 140, 141, 142, 145, 146, DatabaseID 197 150, 184, 187 Date 58, 79 PersonInfo 195, 197 Day 79 PhoneNumber 27, 184 DBConn 45, 47 PMImpl 131 DBConnection 45 Point 26, 41, 123 deque 229 PrettyMenu 127, 130, 131 deque::iterator 229 PriorityCustomer 58 Derived 54, 118, 137, 157, 158, 159, 160, random_access_iterator_tag 228 206, 254, 260 Rational 90, 102, 103, 105, 222, 223, 224, Directory 31 225, 226 ElectronicGadget 192 RealPerson 147 Ellipse 161 Rectangle 124, 125, 154, 161, 181, 183 Empty 34, 190 RectData 124 EvilBadGuy 172, 174 SellTransaction 49 EyeCandyCharacter 175 Set 185 Factorial 235 Shape 161, 162, 163, 167, 180, 182, 183 Factorial<0> 235 SmartPtr 218, 219, 220 File 193, 194 SpecialString 42 FileSystem 30 SpecialWindow 119, 120, 121, 122 FlyingBird 152 SpeedDataCollection 96 ptg7544714 Font 71 Square 154 forward_iterator_tag 228 SquareMatrix 213, 214, 215, 216 GameCharacter 169, 170, 172, 173, 176 SquareMatrixBase 214, 215 GameLevel 174 StandardNewDeleteForms 260 GamePlayer 14, 15 Student 86, 150, 187 GraphNode 4 TextBlock 20, 23, 24 GUIObject 126 TimeKeeper 40, 41 HealthCalcFunc 176 Timer 188 HealthCalculator 174 Top 218 HoldsAnInt 190, 191 Transaction 48, 50, 51 HomeForSale 37, 38, 39 Uncopyable 39 input_iterator_tag 228 WaterClock 40 input_iterator_tag<Iter*> 230 WebBrowser 98, 100, 101 InputFile 193, 194 Widget 4, 5, 44, 52, 53, 54, 56, 107, 108, Investment 61, 70 109, 118, 189, 199, 201, 242, 245, 246, IOFile 193, 194 257, 258, 261 IPerson 195, 197 Widget::WidgetTimer 189 iterator_traits 229 WidgetImpl 106, 108 see also std::iterator_traits Window 88, 119, 121, 122 list 229 WindowWithScrollBars 88 list::iterator 229 WristWatch 40 Lock 66, 67, 68 X 242 LoggingMsgSender 208, 210, 211 Y 242 Middle 218 Year 79 ModelA 164, 165, 167 example functions/templates ModelB 164, 165, 167 ABEntry::ABEntry 27, 28 ModelC 164, 166, 167 AccessLevels::getReadOnly 95 Month 79, 80 AccessLevels::getReadWrite 95 MP3Player 192 AccessLevels::setReadOnly 95
286 Index Effective C++ AccessLevels::setWriteOnly 95 Font::operator FontHandle 71 advance 228, 230, 232, 233, 234 GameCharacter::doHealthValue 170 Airplane::defaultFly 165 GameCharacter::GameCharacter 172, 174, Airplane::fly 164, 165, 166, 167 176 askUserForDatabaseID 195 GameCharacter::healthValue 169, 170, AWOV::AWOV 43 172, 174, 176 B::mf 178 GameLevel::health 174 Base::operator delete 255 getFont 70 Base::operator new 254 hasAcceptableQuality 6 Bird::fly 151 HealthCalcFunc::calc 176 BorrowableItem::checkOut 192 HealthCalculator::operator() 174 boundingBox 126 lock 66 BuyTransaction::BuyTransaction 51 Lock::~Lock 66 BuyTransaction::createLogString 51 Lock::Lock 66, 68 calcHealth 174 logCall 57 callWithMax 16 LoggingMsgSender::sendClear 208, 210, changeFontSize 71 211 Circle::draw 181 loseHealthQuickly 172 clearAppointments 143, 144 loseHealthSlowly 172 clearBrowser 98 main 141, 142, 236, 241 CPerson::birthDate 198 makeBigger 154 CPerson::CPerson 198 makePerson 195 CPerson::name 198 max 135 CPerson::valueDelimClose 198 ModelA::fly 165, 167 CPerson::valueDelimOpen 198 ModelB::fly 165, 167 createInvestment 62, 70, 81, 82, 83 ModelC::fly 166, 167 CTextBlock::length 22, 23 Month::Dec 80 CTextBlock::operator[] 21 Month::Feb 80 Customer::Customer 58 Month::Jan 80 Customer::operator= 58 Month::Month 79, 80 ptg7544714 D::mf 178 MsgSender::sendClear 208 Date::Date 79 MsgSender::sendSecret 208 Day::Day 79 MsgSender<CompanyZ>::sendSecret 209 daysHeld 69 NewHandlerHolder::~NewHandlerHolder 243 DBConn::~DBConn 45, 46, 47 NewHandlerHolder::NewHandlerHolder 243 DBConn::close 47 NewHandlerSupport::operator new 245 defaultHealthCalc 172, 173 NewHandlerSupport::set_new_handler 245 Derived::Derived 138, 206 numDigits 4 Derived::mf1 160 operator delete 255 Derived::mf4 157 operator new 249, 252 Directory::Directory 31, 32 operator* 91, 92, 94, 105, 222, 224, 225, doAdvance 231 226 doMultiply 226 operator== 93 doProcessing 200, 202 outOfMem 240 doSomething 5, 44, 54, 110 Penguin::fly 152 doSomeWork 118 Person::age 135 eat 151, 187 Person::create 146, 147 ElectronicGadget::checkOut 192 Person::name 145 Empty::~Empty 34 Person::Person 145 Empty::Empty 34 PersonInfo::theName 196 Empty::operator= 34 PersonInfo::valueDelimClose 196 encryptPassword 114, 115 PersonInfo::valueDelimOpen 196 error 152 PrettyMenu::changeBackground 127, 128, EvilBadGuy::EvilBadGuy 172 130, 131 f 62, 63, 64 print 20 FlyingBird::fly 152 print2nd 204, 205 Font::~Font 71 printNameAndDisplay 88, 89 Font::Font 71 priority 75 Font::get 71 PriorityCustomer::operator= 59
Effective C++ Index 287 PriorityCustomer::PriorityCustomer 59 destructors and 44–48 processWidget 75 member swap and 112 RealPerson::~RealPerson 147 standard hierarchy for 264 RealPerson::RealPerson 147 swallowing 46 Rectangle::doDraw 183 unused objects and 114 Rectangle::draw 181, 183 exception-safe code 127–134 Rectangle::lowerRight 124, 125 copy-and-swap and 132 Rectangle::upperLeft 124, 125 legacy code and 133 releaseFont 70 pimpl idiom and 131 Set::insert 186 side effects and 132 Set::member 186 exception-safety guarantees 128–129 Set::remove 186 explicit calls to base class functions 211 Set::size 186 explicit constructors 5, 85, 104 Shape::doDraw 183 generalized copy construction and 219 Shape::draw 161, 162, 180, 182, 183 explicit inline request 135 Shape::error 161, 163 explicit specification, of class names 162 Shape::objectID 161, 167 SmartPtr::get 220 explicit type conversions vs. implicit 70– SmartPtr::SmartPtr 220 72 someFunc 132, 156 expression templates 237 SpecialWindow::blink 122 expressions, implicit interfaces and 201 SpecialWindow::onResize 119, 120 SquareMatrix::invert 214 SquareMatrix::setDataPtr 215 F SquareMatrix::SquareMatrix 215, 216 factoring code, out of templates 212–217 StandardNewDeleteForms::operator factory function 40, 62, 69, 81, 146, 195 delete 260, 261 StandardNewDeleteForms::operator Fallenstedt, Martin xix new 260, 261 federation, of languages, C++ as 11–13 std::swap 109 Fehér, Attila F. xix ptg7544714 std::swap<Widget> 107, 108 final classes, in Java 43 study 151, 187 final methods, in Java 190 swap 106, 109 fixed-size static buffers, problems of 196 tempDir 32 forms of new and delete 73–75 TextBlock::operator[] 20, 23, 24 FORTRAN 42 tfs 32 forward iterators 227 Timer::onTick 188 forward_iterator_tag 228 Transaction::init 50 Transaction::Transaction 49, 50, 51 forwarding functions 144, 160 Uncopyable::operator= 39 French, Donald xx Uncopyable::Uncopyable 39 friend functions 38, 85, 105, 135, 173, 223– unlock 66 225 validateStudent 87 vs. member functions 98–102 Widget::onTick 189 friendship Widget::operator new 244 in real life 105 Widget::operator+= 53 without needing special access Widget::operator= 53, 54, 55, 56, 107 rights 225 Widget::set_new_handler 243 Fruchterman, Thomas xix Widget::swap 108 FUDGE_FACTOR 15 Window::blink 122 Fuller, John xx Window::onResize 119 function declarations, const in 18 workWithIterator 206, 207 Year::Year 79 function objects definition of 6 exception specifications 85 higher-order programming utilities Exceptional C++ xvii and, in Boost 271 Exceptional C++ Style xvii, xviii functions exceptions 113 convenience 100 delete and 62 copying 57
288 Index Effective C++ defining 4 headers deliberately not defining 38 for declarations vs. for definitions 144 factory, see factory function inline functions and 135 forwarding 144, 160 namespaces and 100 implicitly generated 34–37, 221 of C++ standard library 101 disallowing 37–39 templates and 136 inline, declaring 135 usage, in this book 3 member hello world, template metaprogramming templatized 218–222 and 235 vs. non-member 104–105 Helm, Richard xvii non-member Henney, Kevlin xix templates and 222–226 Hicks, Cory xix type conversions and 102–105, 222– hiding names, see name hiding 226 higher-order programming and function non-member non-friend, vs object utilities, in Boost 271 member 98–102 highlighting, in this book 5 non-virtual, meaning 168 return values, modifying 21 signatures, explicit interfaces and 201 I static identity test 55 ctors and dtors and 52 if...else for types 230 virtual, see virtual functions #ifdef 17 function-style casts 116 #ifndef 17 implementation-dependent behavior, G warnings and 263 implementations Gamma, Erich xvii decoupling from interfaces 165 Geller, Alan xix default, danger of 163–167 ptg7544714 generalized assignment 220 inheritance of 161–169 generalized copy constructors 219 of derived class constructors and generative programming 237 destructors 137 generic programming support, in of Interface classes 147 Boost 271 references 89 get, smart pointers and 70 std::max 135 goddess, see Urbano, Nancy L. std::swap 106 implicit inline request 135 goto, delete and 62 implicit interfaces 199–203 Graph library, in Boost 270 implicit type conversions vs. explicit 70– grep, casts and 117 72 guarantees, exception safety 128–129 implicitly generated functions 34–37, 221 Gutnik, Gene xix disallowing 37–39 #include directives 17 H compilation dependencies and 140 incompatibilities, with built-in types 80 Handle classes 144–145 incorrect code and efficiency 90 handles 125 infinite loop, in operator new 253 dangling 126 encapsulation and 125 inheritance accidental 165–166 operator[] and 126 combining with templates 243–245 returning 123–126 common features and 164 has-a relationship 184 intuition and 151–155 hash tables, in TR1 266 mathematics and 155 Hastings, Battle of 150 mixin-style 244 Haugland, Solveig xx name hiding and 156–161 head scratching, avoiding 95 of implementation 161–169 header files, see headers of interface 161–169
Effective C++ Index 289 of interface vs. implementation 161–169 vs. macros, efficiency and 16 operator new and 253–254 inlining 134–139 penguins and birds and 151–153 constructors/destructors and 137–138 private 187–192 dynamic linking and 139 protected 151 Handle classes and 148 public 150–155 inheritance and 137–138 rectangles and squares and 153–155 Interface classes and 148 redefining non-virtual functions library design and 138 and 178–180 recompiling and 139 scopes and 156 relinking and 139 sharing features and 164 suggested strategy for 139 inheritance, multiple 192–198 templates and 136 ambiguity and 192 time of 135 combining public and private 197 virtual functions and 136 deadly diamond 193 input iterators 227 inheritance, private 214 input_iterator_tag 228 combining with public 197 input_iterator_tag<Iter*> 230 eliminating 189 insomnia 150 for redefining virtual functions 197 instructions, reordering by compilers 76 meaning 187 integral types 14 vs. composition 188 Interface classes 145–147 inheritance, public combining with private 197 interfaces decoupling from implementations 165 is-a relationship and 150–155 definition of 7 meaning of 150 design considerations 78–86 name hiding and 159 explicit, signatures and 201 virtual inheritance and 194 implicit 199–203 inheritance, virtual 194 expressions and 201 init function 60 inheritance of 161–169 ptg7544714 initialization 4, 26–27 new types and 79–80 assignment vs. 6 separating from implementations 140 built-in types 26–27 template parameters and 199–203 const members 29 undeclared 85 const static members 14 inter-language support, in Boost 272 default, unintended 59 in-class, of static const integral internationalization, library support for 264 members 14 local static objects 31 invalid array index, undefined behavior non-local static objects 30 and 7 objects 26–33 invariants reference members 29 NVI and 171 static members 242 over specialization 168 virtual base classes and 194 <iosfwd> 144 vs. assignment 27–29, 114 is-a relationship 150–155 with vs. without arguments 114 is-implemented-in-terms-of 184–186, 187 initialization order istream_iterators 227 class members 29 iterator categories 227–228 importance of 31 iterator_category 229 non-local statics 29–33 iterators as handles 125 inline functions see also inlining iterators, vs. const_iterators 18 address of 136 as request to compiler 135 J debuggers and 139 declaring 135 Jagdhar, Emily xix headers and 135 Janert, Philipp xix optimizing compilers and 134 Java 7, 43, 76, 81, 100, 116, 118, 142, 145, recursion and 136 190, 194 vs. #define 16–17
290 Index Effective C++ Johnson, Ralph xvii maintenance Johnson, Tim xviii, xix common base classes and 164 Josuttis, Nicolai M. xviii delete and 62 managing resources, see resource man- agement K Manis, Vincent xix Marin, Alex xix Kaelbling, Mike xviii math and numerics utilities, in Boost 271 Kakulapati, Gunavardhan xix mathematical functions, in TR1 267 Kalenkovich, Eugene xix mathematics, inheritance and 155 Kennedy, Glenn xix matrix operations, optimizing 237 Kernighan, Brian xviii, xix Matthews, Leon xix Kimura, Junichi xviii max, std, implementation of 135 Kirman, Jak xviii Meadowbrooke, Chrysta xix Kirmse, Andrew xix meaning Knox, Timothy xviii, xix of classes without virtual functions 41 Koenig lookup 110 of composition 184 Kourounis, Drosos xix of non-virtual functions 168 Kreuzer, Gerhard xix of pass-by-value 6 of private inheritance 187 L of public inheritance 150 of pure virtual functions 162 Laeuchli, Jesse xix of references 91 Lambda library, in Boost 271 of simple virtual functions 163 Langer, Angelika xix measuring encapsulation 99 languages, other, compatibility with 42 Meehan, Jim xix Lanzetta, Michael xix member data, see data members late binding 180 member function templates 218–222 ptg7544714 layering, see composition member functions bitwise const 21–22 layouts, objects vs. arrays 73 common design errors 168–169 Lea, Doug xviii const 19–25 leaks, exception-safe code and 127 duplication and 23–25 Leary-Coutu, Chanda xx encapsulation and 99 Lee, Sam xix implicitly generated 34–37, 221 legacy code, exception-safety and 133 disallowing 37–39 Lejter, Moises xviii, xx logically const 22–23 lemur, ring-tailed 196 private 38 Lewandowski, Scott xviii protected 166 lhs, as parameter name 8 vs. non-member functions 104–105 vs. non-member non-friends 98–102 Li, Greg xix member initialization link-time errors 39, 44 for const static integral members 14 link-time inlining 135 lists 28–29 list 186 vs. assignment 28–29 local static objects order 29 definition of 30 initialization of 31 memory allocation arrays and 254–255 locales 264 error handling for 240–246 locks, RAII and 66–68 memory leaks, new expressions and 256 logic_error class 113 memory management logically const member functions 22–23 functions, replacing 247–252 multithreading and 239, 253 M utilities, in Boost 272 metaprogramming, see template metapro- mailing list for Scott Meyers xvi gramming
Effective C++ Index 291 Meyers, Scott Nauroth, Chris xix mailing list for xvi nested dependent names 204 web site for xvi nested dependent type names, typename mf, as identifier 9 and 205 Michaels, Laura xviii new Mickelsen, Denise xx see also operator new minimizing compilation expressions, memory leaks and 256 dependencies 140–148, 190 forms of 73–75 Mittal, Nishant xix operator new and 73 mixed-mode arithmetic 103, 104, 222–226 relationship to constructors 73 smart pointers and 75–77 mixin-style inheritance 244 modeling is-implemented-in-terms- new types, interface design and 79–80 of 184–186 new-handler 240–247 definition of 240 modifying function return values 21 deinstalling 241 Monty Python, allusion to 91 identifying 253 Moore, Vanessa xx new-handling functions, behavior of 241 More Effective C++ 273, 273–274 new-style casts 117 compared to Effective C++ 273 noncopyable base class, in Boost 39 contents of 273–274 non-dependent names 204 More Exceptional C++ xvii non-local static objects, initialization Moroff, Hal xix of 30 MPL library, in Boost 270, 271 non-member functions multiparadigm programming language, member functions vs. 104–105 C++ as 11 templates and 222–226 multiple inheritance, see inheritance type conversions and 102–105, 222–226 multithreading non-member non-friend functions 98–102 memory management routines non-type parameters 213 and 239, 253 ptg7544714 non-const static objects and 32 non-virtual functions 178–180 treatment in this book 9 static binding of 178 mutable 22–23 interface idiom, see NVI mutexes, RAII and 66–68 nothrow guarantee, the 129 nothrow new 246 N null pointer deleting 255 Nagler, Eric xix dereferencing 6 Nahil, Julie xx set_new_handler and 241 name hiding NVI 170–171, 183 inheritance and 156–161 operators new/delete and 259–261 using declarations and 159 O name lookup this-> and 210 object-oriented C++, as sublanguage of C++ 12 using declarations and 211 object-oriented principles, encapsulation name shadowing, see name hiding and 99 names accessing in templatized bases 207–212 objects alignment of 249–250 available in both C and C++ 3 clustering 251 dependent 204 compilation dependencies and 143 hidden by derived classes 263 copying all parts 57–60 nested, dependent 204 defining 4 non-dependent 204 definitions, postponing 113–116 namespaces 110 handles to internals of 123–126 headers and 100 namespace pollution in a class 166 initialization, with vs. without arguments 114 Nancy, see Urbano, Nancy L. layout vs. array layout 73
292 Index Effective C++ multiple addresses for 118 during compilation 134 partial copies of 58 inline functions and 134 placing in shared memory 251 order resource management and 61–66 initialization of non-local statics 29–33 returning, vs. references 90–94 member initialization 29 size, pass-by-value and 89 ostream_iterators 227 sizes, determining 141 other languages, compatibility with 42 vs. variables 3 output iterators 227 Oldham, Jeffrey D. xix output_iterator_tag 228 old-style casts 117 overloading operations, reordering by compilers 76 as if...else for types 230 operator delete 84 on const 19–20 see also delete std::swap 109 behavior of 255 overrides of virtuals, preventing 189 efficiency of 248 ownership transfer 68 name hiding and 259–261 non-member, pseudocode for 255 placement 256–261 P replacing 247–252 standard forms of 260 Pal, Balog xix virtual destructors and 255 parameters operator delete[] 84, 255 see also pass-by-value, pass-by-refer- operator new 84 ence see also new default 180–183 arrays and 254–255 evaluation order 76 bad_alloc and 246, 252 non-type, for templates 213 behavior of 252–255 type conversions and, see type conver- efficiency of 248 sions infinite loop within 253 Pareto Principle, see 80-20 rule ptg7544714 inheritance and 253–254 parsing problems, nested dependent member, and “wrongly sized” names and 204 requests 254 partial copies 58 name hiding and 259–261 partial specialization new-handling functions and 241 function templates 109 non-member, pseudocode for 252 std::swap 108 out-of-memory conditions and 240–241, parts, of objects, copying all 57–60 252–253 pass-by-reference, efficiency and 87 placement 256–261 replacing 247–252 pass-by-reference-to-const, vs pass-by- value 86–90 returning 0 and 246 standard forms of 260 pass-by-value std::bad_alloc and 246, 252 copy constructor and 6 efficiency of 86–87 operator new[] 84, 254–255 meaning of 6 operator() (function call operator) 6 object size and 89 operator= vs. pass-by-reference-to-const 86–90 const members and 36–37 default implementation 35 patterns implicit generation 34 see design patterns Pedersen, Roger E. xix reference members and 36–37 return value of 52–53 penguins and birds 151–153 self-assignment and 53–57 performance, see efficiency when not implicitly generated 36–37 Persephone ix, xx, 36 operator[] 126 pessimization 93 overloading on const 19–20 physical constness, see const, bitwise return type of 21 pimpl idiom optimization definition of 106 by compilers 94 exception-safe code and 131
Effective C++ Index 293 placement delete, see operator delete random number generation, in TR1 267 placement new, see operator new random_access_iterator_tag 228 Plato 87 RCSP, see smart pointers pointer arithmetic and undefined reading uninitialized values 26 behavior 119 rectangles and squares 153–155 pointers recursive functions, inlining and 136 see also smart pointers redefining inherited non-virtual as handles 125 functions 178–180 bitwise const member functions and 21 Reed, Kathy xx compilation dependencies and 143 Reeves, Jack xix const 17 in headers 14 references as handles 125 null, dereferencing 6 compilation dependencies and 143 template parameters and 217 functions returning 31 to single vs. multiple objects, and implementation 89 delete 73 meaning 91 polymorphic base classes, destructors members, initialization of 29 and 40–44 returning 90–94 polymorphism 199–201 to static object, as function return compile-time 201 value 92–94 runtime 200 register usage, objects and 89 Pool library, in Boost 250, 251 regular expressions, in TR1 266 postponing variable definitions 113–116 reinterpret_cast 117, 249 Prasertsith, Chuti xx see also casting preconditions, NVI and 171 relationships pregnancy, exception-safe code and 133 has-a 184 private data members, why 94–98 is-a 150–155 private inheritance, see inheritance is-implemented-in-terms-of 184–186, ptg7544714 private member functions 38 187 private virtual functions 171 reordering operations, by compilers 76 properties 97 replacing definitions with protected declarations 143 data members 97 replacing new/delete 247–252 inheritance, see inheritance replication, see duplication member functions 166 reporting, bugs in this book xvi members, encapsulation of 97 Resource Acquisition Is Initialization, see public inheritance, see inheritance RAII pun, really bad 152 resource leaks, exception-safe code pure virtual destructors and 127 defining 43 resource management implementing 43 see also RAII pure virtual functions 43 copying behavior and 66–69 defining 162, 166–167 objects and 61–66 meaning 162 raw resource access and 69–73 resources, managing objects and 69–73 return by reference 90–94 R return types Rabbani, Danny xix const 18 objects vs. references 90–94 Rabinowitz, Marty xx of operator[] 21 RAII 63, 70, 243 return value of operator= 52–53 classes 72 copying behavior and 66–69 returning handles 123–126 encapsulation and 72 reuse, see code reuse mutexes and 66–68 revenge, compilers taking 58 random access iterators 227 rhs, as parameter name 8
294 Index Effective C++ Roze, Mike xix Smalltalk 142 rule of 80-20 139, 168 smart pointers 63, 64, 70, 81, 121, 146, 237 runtime see also std::auto_ptr and tr1::shared_ptr errors 152 get and 70 inlining 135 in Boost 65, 272 polymorphism 200 web page for xvii in TR1 265 newed objects and 75–77 S type conversions and 218–220 Socrates 87 Saks, Dan xviii Some Must Watch While Some Must Santos, Eugene, Jr. xviii Sleep 150 Satch 36 Somers, Jeff xix Satyricon vii specialization Scherpelz, Jeff xix invariants over 168 Schirripa, Steve xix partial, of std::swap 108 Schober, Hendrik xviii, xix total, of std::swap 107, 108 Schroeder, Sandra xx specification, see interfaces scoped_array 65, 216, 272 squares and rectangles 153–155 scopes, inheritance and 156 standard exception hierarchy 264 sealed classes, in C# 43 standard forms of operator new/delete 260 sealed methods, in C# 190 standard library, see C++ standard second edition, see 2nd edition library, C standard library self-assignment, operator= and 53–57 standard template library, see STL set 185 Stasko, John xviii set_new_handler statements using new, smart pointers class-specific, implementing 243–245 and 75–77 using 240–246 static set_unexpected function 129 binding ptg7544714 shadowing, names, see name shadowing of default parameters 182 Shakespeare, William 156 of non-virtual functions 178 shared memory, placing objects in 251 objects, returning references to 92–94 shared_array 65 type, definition of 180 shared_ptr implementation in Boost, static functions, ctors and dtors and 52 costs 83 static members sharing code, see duplication, avoiding const member functions and 21 sharing common features 164 definition 242 initialization 242 Shewchuk, John xviii static objects side effects, exception safety and 132 definition of 30 signatures multithreading and 32 definition of 3 static_cast 25, 82, 117, 119, 249 explicit interfaces and 201 see also casting simple virtual functions, meaning of 163 std namespace, specializing templates Singh, Siddhartha xix in 107 Singleton pattern 31 std::auto_ptr 63–65, 70 size_t 3 conversion to tr1::shared_ptr and 220 sizeof 253, 254 delete [] and 65 empty classes and 190 pass by const and 220 freestanding classes and 254 std::auto_ptr, deleter support and 68 sizes std::char_traits 232 of freestanding classes 254 std::iterator_traits, pointers and 230 of objects 141 std::list 186 sleeping pills 150 std::max, implementation of 135 slist 227 std::numeric_limits 232 Smallberg, David xviii, xix
Effective C++ Index 295 std::set 185 pointer type parameters and 217 std::size_t 3 shorthand for 224 std::swap specializations 229, 235 see also swap partial 109, 230 implementation of 106 total 107, 209 overloading 109 type conversions and 222–226 partial specialization of 108 type deduction for 223 total specialization of 107, 108 temporary objects, eliminated by std::tr1, see TR1 compilers 94 stepping through functions, inlining terminology, used in this book 3–8 and 139 testing and correctness, Boost support STL for 272 allocators 240 text and string utilities, in Boost 271 as sublanguage of C++ 12 third edition, see 3rd edition containers, swap and 108 this->, to force base class lookup 210 definition of 6 threading, see multithreading iterator categories in 227–228 Tilly, Barbara xviii Strategy pattern 171–177 TMP, see template metaprogramming string and text utilities, in Boost 271 Tondo, Clovis xviii strong guarantee, the 128 Topic, Michael xix Stroustrup, Bjarne xvii, xviii total class template specialization 209 Stroustrup, Nicholas xix total specialization of std::swap 107, 108 Sutter, Herb xvii, xviii, xix total template specializations 107 swallowing exceptions 46 TR1 9, 264–267 swap 106–112 array component 267 see also std::swap bind component 266 calling 110 Boost and 9–10, 268, 269 exceptions and 112 boost as synonym for std::tr1 268 ptg7544714 STL containers and 108 C99 compatibility component 267 when to write 111 function component 265 symbols, available in both C and C++ 3 hash tables component 266 math functions component 267 T mem_fn component 267 random numbers component 267 template C++, as sublanguage of C++ 12 reference_wrapper component 267 regular expression component 266 template metaprogramming 233–238 result_of component 267 efficiency and 233 smart pointers component 265 hello world in 235 support for TMP 267 pattern implementations and 237 tuples component 266 support in Boost 271 type traits component 267 support in TR1 267 URL for information on 268 Template Method pattern 170 tr1::array 267 templates tr1::bind 175, 266 code bloat, avoiding in 212–217 combining with inheritance 243–245 tr1::function 173–175, 265 defining 4 tr1::mem_fn 267 errors, when detected 212 tr1::reference_wrapper 267 expression 237 tr1::result_of 267 headers and 136 tr1::shared_ptr 53, 64–65, 70, 75–77 in std, specializing 107 construction from other smart pointers inlining and 136 and 220 instantiation of 222 cross-DLL problem and 82 member functions 218–222 delete [] and 65 names in base classes and 207–212 deleter support in 68, 81–83 non-type parameters 213 member template ctors in 220–221 parameters, omitting 224 tr1::tuple 266
296 Index Effective C++ tr1::unordered_map 43, 266 unexpected function 129 tr1::unordered_multimap 266 uninitialized tr1::unordered_multiset 266 data members, virtual functions and 49 tr1::unordered_set 266 values, reading 26 tr1::weak_ptr 265 unnecessary objects, avoiding 115 traits classes 226–232 unused objects cost of 113 transfer, ownership 68 exceptions and 114 translation unit, definition of 30 Urbano, Nancy L. vii, xviii, xx Trux, Antoine xviii see also goddess Tsao, Mike xix URLs tuples, in TR1 266 Boost 10, 269, 272 type conversions 85, 104 Boost smart pointers xvii explicit ctors and 5 Effective C++ errata list xvi implicit 104 Effective C++ TR1 Info. Page 268 implicit vs. explicit 70–72 Greg Comeau’s C/C++ FAQ xviii non-member functions and 102–105, Scott Meyers’ mailing list xvi 222–226 Scott Meyers’ web site xvi private inheritance and 187 this book’s errata list xvi smart pointers and 218–220 usage statistics, memory management templates and 222–226 and 248 type deduction, for templates 223 using declarations type design 78–86 name hiding and 159 type traits, in TR1 267 name lookup and 211 typedef, typename and 206–207 typedefs, new/delete and 75 typeid 50, 230, 234, 235 V typelists 271 valarray 264 typename 203–207 value, pass by, see pass-by-value ptg7544714 compiler variations and 207 Van Wyk, Chris xviii, xix typedef and 206–207 Vandevoorde, David xviii vs. class 203 variable, vs. object 3 types variables definitions, postponing 113–116 built-in, initialization 26–27 vector template 75 compatible, accepting all 218–222 if...else for 230 Viciana, Paco xix integral, definition of 14 virtual base classes 193 traits classes and 226–232 virtual constructors 146, 147 virtual destructors operator delete and 255 U polymorphic base classes and 40–44 undeclared interface 85 virtual functions alternatives to 169–177 undefined behavior ctors/dtors and 48–52 advance and 231 default implementations and 163–167 array deletion and 73 default parameters and 180–183 casting + pointer arithmetic and 119 dynamic binding of 179 definition of 6 efficiency and 168 destroyed objects and 91 explict base class qualification and 211 exceptions and 45 implementation 42 initialization order and 30 inlining and 136 invalid array index and 7 language interoperability and 42 multiple deletes and 63, 247 meaning of none in class 41 null pointers and 6 preventing overrides 189 object deletion and 41, 43, 74 private 171 uninitialized values and 26 pure, see pure virtual functions undefined values of members before con- simple, meaning of 163 struction and after destruction 50
Effective C++ Index 297 uninitialized data members and 49 virtual inheritance, see inheritance virtual table 42 virtual table pointer 42 Vlissides, John xvii vptr 42 vtbl 42 W Wait, John xx warnings, from compiler 262–263 calls to virtuals and 50 inlining and 136 partial copies and 58 web sites, see URLs Widget class, as used in this book 8 Wiegers, Karl xix Wilson, Matthew xix Wizard of Oz, allusion to 154 X XP, allusion to 225 XYZ Airlines 163 Z ptg7544714 Zabluda, Oleg xviii Zolman, Leor xviii, xix
This page intentionally left blank ptg7544714
eBook Versions of Best-Selling Now Available! Works by Scott Meyers Scott Meyers’ seminal C++ books — Effective C++, More Effective C++, and Effective STL — have been immensely helpful to hundreds of thousands of C++ programmers. All three are finally available in PDF eBook format. And not just as any PDFs — Scott has worked closely with Addison-Wesley’s production team to exploit the capabilities of electronic presentation and avoid the artificial limitations often imposed on readers of electronic books. THESE PDF VERSIONS ARE OFFERED WITHOUT DRM, THEREBY ALLOWING YOU TO: • Copy, paste, and print excerpts (for personal use) • Install copies on your other machines (for personal use) eBook ISBN 032151582X • Run without network connections, accessing eBooks from fast local storage • Add your own bookmarks and annotations (depending on your PDF viewer) • Index with desktop search tools ptg7544714 • Read with any PDF-viewing program on any operating system THE PDF FEATURES OF THESE EBOOKS INCLUDE: • Hyperlinked tables of contents, indices, and cross-references within each eBook • Hyperlinks to the Web from cited references and URLs • Powerful, convenient, full-text searching eBook ISBN 0321515811 (depending on your PDF viewer) • The production quality of the print editions THE PUBLISHER ALSO OFFERS: • Special discounts when purchasing all three Meyers eBooks together • Special prices when purchasing both the print and electronic version of a book For more info, and to purchase, go to informit.com eBook ISBN 0321515803
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321