Modern C++ Design 原版完整文件.docx

上传人:暗伤 文档编号:96870216 上传时间:2024-03-27 格式:DOCX 页数:285 大小:894.08KB
返回 下载 相关 举报
Modern C++ Design 原版完整文件.docx_第1页
第1页 / 共285页
Modern C++ Design 原版完整文件.docx_第2页
第2页 / 共285页
点击查看更多>>
资源描述

《Modern C++ Design 原版完整文件.docx》由会员分享,可在线阅读,更多相关《Modern C++ Design 原版完整文件.docx(285页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Table of ContentsModern C+ Design: Generic Programming and Design Patterns AppliedBy Andrei AlexandrescuPublisher : Addison Wesley Pub Date : February 01, 2001ISBN : 0-201-70431-5Pages : 352Modern C+ Design is an important book. Fundamentally, it demonstrates generic patterns or pattern templates as

2、 a powerful new way of creating extensible designs in C+-a new way to combine templates and patterns that you may never have dreamt was possible, but is. If your work involves C+ design and coding, you should read this book. Highly recommended.-Herb SutterWhats left to say about C+ that hasnt alread

3、y been said? Plenty, it turns out.-From the Foreword by John VlissidesIn Modern C+ Design, Andrei Alexandrescu opens new vistas for C+ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, gener

4、ic programming, and C+, enabling programmers to achieve expressive, flexible, and highly reusable code.This book introduces the concept of generic components-reusable design templates that produce boilerplate code for compiler consumption-all within C+. Generic components enable an easier and more s

5、eamless transition from design to application code, generate code that better expresses the original design intention, and support the reuse of design structures with minimal recoding.The author describes the specific C+ techniques and features that are used in building generic components and goes o

6、n to implement industrial strength generic components for real-world applications. Recurring issues that C+ developers face in their day-to-day activity are discussed in depth and implemented in a generic way. These include: Policy-based design for flexibility Partial template specialization Typelis

7、ts-powerful type manipulation structures Patterns such as Visitor, Singleton, Command, and Factories Multi-method enginesFor each generic component, the book presents the fundamental problems and design options, and finally implements a generic solution.In addition, an accompanying Web site, makes t

8、he code implementations available for the generic components in the book and provides a free, downloadable C+ library, called Loki, created by the author. Loki provides out-of-the-box functionality for virtually any C+ project.Team-FlyTable of ContentxTable of ContentiCopyrightviForewordviiForewordi

9、xPrefacexAudiencexiLokixiOrganizationxiiAcknowledgmentsxiiiPart I: Techniques1Chapter 1. Policy-Based Class Design21.1 The Multiplicity of Software Design21.2 The Failure of the Do-It-All Interface31.3 Multiple Inheritance to the Rescue?41.4 The Benefit of Templates51.5 Policies and Policy Classes61

10、.6 Enriched Policies91.7 Destructors of Policy Classes101.8 Optional Functionality Through Incomplete Instantiation111.9 Combining Policy Classes121.10 Customizing Structure with Policy Classes131.11 Compatible and Incompatible Policies141.12 Decomposing a Class into Policies161.13 Summary17Chapter

11、2. Techniques192.1 Compile-Time Assertions192.2 Partial Template Specialization222.3 Local Classes232.4 Mapping Integral Constants to Types242.5 Type-to-Type Mapping262.6 Type Selection282.7 Detecting Convertibility and Inheritance at Compile Time292.8 A Wrapper Around type_info322.9 NullType and Em

12、ptyType342.10 Type Traits342.11 Summary40Chapter 3. Typelists423.1 The Need for Typelists423.2 Defining Typelists433.3 Linearizing Typelist Creation453.4 Calculating Length453.5 Intermezzo463.6 Indexed Access473.7 Searching Typelists483.8 Appending to Typelists493.9 Erasing a Type from a Typelist503

13、.10 Erasing Duplicates513.11 Replacing an Element in a Typelist523.12 Partially Ordering Typelists533.13 Class Generation with Typelists563.14 Summary653.15 Typelist Quick Facts66Chapter 4. Small-Object Allocation684.1 The Default Free Store Allocator684.2 The Workings of a Memory Allocator694.3 A S

14、mall-Object Allocator704.4 Chunks714.5 The Fixed-Size Allocator744.6 The SmallObjAllocator Class774.7 A Hat Trick794.8 Simple, Complicated, Yet Simple in the End814.9 Administrivia824.10 Summary834.11 Small-Object Allocator Quick Facts83Part II: Components85Chapter 5. Generalized Functors865.1 The C

15、ommand Design Pattern865.2 Command in the Real World895.3 C+ Callable Entities895.4 The Functor Class Template Skeleton915.5 Implementing the Forwarding Functor:operator()955.6 Handling Functors965.7 Build One, Get One Free985.8 Argument and Return Type Conversions995.9 Handling Pointers to Member F

16、unctions1015.10 Binding1045.11 Chaining Requests1065.12 Real-World Issues I: The Cost of Forwarding Functions1075.13 Real-World Issues II: Heap Allocation1085.14 Implementing Undo and Redo with Functor1105.15 Summary1105.16 Functor Quick Facts111Chapter 6. Implementing Singletons1136.1 Static Data +

17、 Static Functions != Singleton1136.2 The Basic C+ Idioms Supporting Singletons1146.3 Enforcing the Singletons Uniqueness1166.4 Destroying the Singleton1166.5 The Dead Reference Problem1186.6 Addressing the Dead Reference Problem (I): The Phoenix Singleton1206.7 Addressing the Dead Reference Problem

18、(II): Singletons with Longevity1226.8 Implementing Singletons with Longevity1256.9 Living in a Multithreaded World1286.10 Putting It All Together1306.11 Working with SingletonHolder1346.12 Summary1366.13 SingletonHolder Class Template Quick Facts136Chapter 7. Smart Pointers1387.1 Smart Pointers 1011

19、387.2 The Deal1397.3 Storage of Smart Pointers1407.4 Smart Pointer Member Functions1427.5 Ownership-Handling Strategies1437.6 The Address-of Operator1507.7 Implicit Conversion to Raw Pointer Types1517.8 Equality and Inequality1537.9 Ordering Comparisons1577.10 Checking and Error Reporting1597.11 Sma

20、rt Pointers to const and const Smart Pointers1617.12 Arrays1617.13 Smart Pointers and Multithreading1627.14 Putting It All Together1657.15 Summary1717.16 SmartPtr Quick Facts171Chapter 8. Object Factories1738.1 The Need for Object Factories1748.2 Object Factories in C+: Classes and Objects1758.3 Imp

21、lementing an Object Factory1768.4 Type Identifiers1808.5 Generalization1818.6 Minutiae1848.7 Clone Factories1858.8 Using Object Factories with Other Generic Components1888.9 Summary1898.10 Factory Class Template Quick Facts1898.11 CloneFactory Class Template Quick Facts190Chapter 9. Abstract Factory

22、1919.1 The Architectural Role of Abstract Factory1919.2 A Generic Abstract Factory Interface1939.3 Implementing AbstractFactory1969.4 A Prototype-Based Abstract Factory Implementation1999.5 Summary2029.6 AbstractFactory and ConcreteFactory Quick Facts203Chapter 10. Visitor20510.1 Visitor Basics20510

23、.2 Overloading and the Catch-All Function21010.3 An Implementation Refinement: The Acyclic Visitor21110.4 A Generic Implementation of Visitor21510.5 Back to the Cyclic Visitor22110.6 Hooking Variations22310.7 Summary22610.8 Visitor Generic Components Quick Facts226Chapter 11. Multimethods22811.1 Wha

24、t Are Multimethods?22811.2 When Are Multimethods Needed?22911.3 Double Switch-on-Type: Brute Force23011.4 The Brute-Force Approach Automated23211.5 Symmetry with the Brute-Force Dispatcher23711.6 The Logarithmic Double Dispatcher24011.7 FnDispatcher and Symmetry24511.8 Double Dispatch to Functors246

25、11.9 Converting Arguments: static_cast or dynamic_cast?24811.10 Constant-Time Multimethods: Raw Speed25211.11 BasicDispatcher and BasicFastDispatcher as Policies25511.12 Looking Forward25711.13 Summary25811.14 Double Dispatcher Quick Facts259Appendix A. A Minimalist Multithreading Library262A.1 A Cr

26、itique of Multithreading262A.2 Lokis Approach263A.3 Atomic Operations on Integral Types264A.4 Mutexes265A.5 Locking Semantics in Object-Oriented Programming267A.6 Optional volatile Modifier269A.7 Semaphores, Events, and Other Good Things269A.8 Summary269Bibliography270CopyrightMany of the designatio

27、ns 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 with initial capital letters or in all capitals.The author and publisher h

28、ave 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 or programs cont

29、ained herein.The publisher offers discounts on this book when ordered in quantity for special sales. For more information, please contact:Pearson Education Corporate Sales Division One Lake StreetUpper Saddle River, NJ 07458 (800) 382-3419corpsalesVisit AWon the Web: Library of Congress Cataloging-i

30、n-Publication DataAlexandrescu, Andrei.Modern C+ design : generic programming and design patterns applied / Andrei Alexandrescu.p. cm. (C+ in depth series)Includes bibliographical references and index. ISBN 0-201-70431-51. C+ (Computer program language) 2. Generic programming (Computer science) I. T

31、itle. II. Series.QA76.73.C153 A42 2001005.133dc21 00-049596Copyright 2001 by Addison-WesleyAll 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, wi

32、thout the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.Text printed on recycled paper2 3 4 5 6 7 8 9 10DOC0504030201Second printing, June 2001Forewordby Scott MeyersIn 1991, I wrote the first edition of Effective C+. The book contained a

33、lmost no discussions of templates, because templates were such a recent addition to the language, I knew almost nothing about them. What little template code I included, I had verified by e-mailing it to other people, because none of the compilers to which I had access offered support for templates.

34、In 1995, I wrote More Effective C+. Again I wrote almost nothing about templates. What stopped me this time was neither a lack of knowledge of templates (my initial outline for the book included an entire chapter on the topic) nor shortcomings on the part of my compilers. Instead, it was a suspicion

35、 that the C+ communitys understanding of templates was about to undergo such dramatic change, anything I had to say about them would soon be considered trite, superficial, or just plain wrong.There were two reasons for that suspicion. The first was a column by John Barton and Lee Nackman in the Janu

36、ary 1995 C+ Report that described how templates could be used to perform typesafe dimensional analysis with zero runtime cost. This was a problem Id spent some time on myself, and I knew that many had searched for a solution, but none had succeeded. Barton and Nackmans revolutionary approach made me

37、 realize that templates were good for a lot more than just creating containers of T.As an example of their design, consider this code for multiplying two physical quantities of arbitrary dimensional type:template Physical operator*(Physical lhs,Physical rhs)return Physical:unit*lhs.value()*rhs.value

38、();Even without the context of the column to clarify this code, its clear that this function template takes six parameters, none of which represents a type! This use of templates was such a revelation to me, I was positively giddy.Shortly thereafter, I started reading about the STL. Alexander Stepan

39、ovs elegant li brary design, where containers know nothing about algorithms; algorithms know nothing about containers; iterators act like pointers (but may be objects instead); containers and algorithms accept function pointers and function objects with equal aplomb; and library clients may extend t

40、he library without having to inherit from any base classes or redefine any virtual functions, made me feelas I had when I read Barton and Nackmans worklike I knew almost nothing about templates.So I wrote almost nothing about them in More Effective C+. How could I? My understanding of templates was

41、still at the containers-of-T stage, while Barton, Nackman, Stepanov, and others were demonstrating that such uses barely scratched the surface of what templates could do.In 1998, Andrei Alexandrescu and I began an e-mail correspondence, and it was not long before I recognized that I was again about

42、to modify my thinking about templates. Where Barton, Nackman, andStepanov had stunned me with what templates could do, however, Andreis work initially made more of an impression on me for how it did what it did.One of the simplest things he helped popularize continues to be the example I use when in

43、troducing people to his work. Its the CTAssert template, analogous in use to the assert macro, but applied to conditions that can be evaluated during compilation. Here it is:template struct CTAssert; template struct CTAssert ;Thats it. Notice how the general template, CTAssert, is never defined. Not

44、ice how there is a specialization for true, but not for false. In this design, whats missing is at least as important as whats present. It makes you look at template code in a new way, because large portions of the source code are deliberately omitted. Thats a very different way of thinking from the

45、 one most of us are used to. (In this book, Andrei discusses the more sophisticated CompileTimeChecker template instead of CTAssert.)Eventually, Andrei turned his attention to the development of template-based implementations of popular language idioms and design patterns, especially the GoF* patter

46、ns. This led to a brief skirmish with the Patterns community, because one of their fundamental tenets is that patterns cannot be represented in code. Once it became clear that Andrei was automating the generation of pattern implementations rather than trying to encode patterns themselves, that objection was removed, and I was pleased to see Andrei and one of the GoF (John Vlissides) collaborate on two columns in the C+ Report focusing on Andreis work.* GoF s

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com