IT training practical c plus plus metaprogramming khotailieu

54 28 0
IT training practical c plus plus metaprogramming khotailieu

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Practical C++ Metaprogramming Modern Techniques for Accelerated Development Edouard Alligand and Joel Falcou Beijing Boston Farnham Sebastopol Tokyo Practical C++ Metaprogramming by Edouard Alligand and Joel Falcou Copyright © 2016 O’Reilly Media, Inc All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://safaribooksonline.com) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Nan Barber and Brian Foster Production Editor: Colleen Lobner Copyeditor: Octal Publishing, Inc Proofreader: Rachel Head September 2016: Interior Designer: David Futato Cover Designer: Randy Comer Illustrator: Rebecca Demarest First Edition Revision History for the First Edition 2016-09-13: First Release The O’Reilly logo is a registered trademark of O’Reilly Media, Inc Practical C++ Metaprogramming, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work Use of the information and instructions contained in this work is at your own risk If any code samples or other technology this work contains or describes is sub‐ ject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights 978-1-491-95504-8 [LSI] Table of Contents Preface vii Introduction A Misunderstood Technique What Is Metaprogramming? How to Get Started with Metaprogramming Summary C++ Metaprogramming in Practice A Typical Code Maintenance Assignment Creating a Straightforward Interface Generating Code Automatically Making Values and Pointers Work Together Putting It All Together Summary 10 13 13 25 26 C++ Metaprogramming and Application Design 27 Compile-Time Versus Runtime Paradigms Type Containers Compile-Time Operations Advanced Uses of Metaprogramming Helper Functions and Libraries Summary 27 30 31 40 42 43 v Preface Another arcane text about an overly complex language! C++ is already difficult enough to master; why people feel the need to make it even more difficult? C++’s power comes at a price, but with the latest revisions of the language, the bar has been drastically lowered The improvements in C++11 and C++14 have had a positive impact in many areas, from how you write a loop to how you can write templates We’ve had the idea of writing about template metaprogramming for a long time, because we wanted to demonstrate how much easier it has become We also wanted to prove its usefulness and efficiency By that we mean that it’s not only a valid solution, but sometimes the best solution Last but not least, even if you don’t use metaprogramming every day, understanding its concepts will make you a better programmer: you will learn to look at problems differently and increase your mastery and understanding of the language A Journey of a Thousand Miles Begins with a Single Step Really mastering C++ metaprogramming is difficult and takes a lot of time You need to understand how compilers work to get around their bugs and limitations The feedback you can receive when you have an error is more often than not arcane That is the bad news vii The good news is that you don’t need to master C++ metaprogram‐ ming, because you are standing on the shoulders of giants In this report, we will progressively expose you to the technique and its practical applications, and give you a list of tools that you can use to get right to it Then, depending on your tastes and your aspirations, you can decide how deep down the rabbit hole you want to go Understanding Metaprogramming Metaprogramming is a technique that can greatly increase your pro‐ ductivity when properly used Improperly used, though it can result in unmaintainable code and greatly increased development time Dismissing metaprogramming based on a preconceived notion or dogma is counterproductive Nevertheless, properly understanding if the technique suits your needs is paramount for fruitful and rewarding use An analogy we like to use is that you should see a metaprogram as a robot you program to a job for you After you’ve programmed the robot, it will be happy to the task for you a thousand times, without error Additionally, the robot is faster than you and more precise If you something wrong, though, it might not be immediately obvious where the problem is Is it a problem in how you program‐ med the robot? Is it a bug in the robot? Or is your program correct but the result unexpected? That’s what makes metaprogramming more difficult: the feedback isn’t immediate, and because you added an intermediary you’ve added more variables to the equation That’s also why before using this technique you must ensure that you know how to program the robot viii | Preface Conventions Used in This Report The following typographical conventions are used in this report: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, data‐ bases, data types, environment variables, statements, and key‐ words This element signifies a general note This element indicates a warning or caution Acknowledgments This report would probably not exist without the work of Aleksey Gurtovoy and David Abrahams, authors of the Boost.MPL library and the reference book C++ Template Metaprogramming (AddisonWesley Professional) More recently, Eric Niebler and Peter Dimov paved the way to what modern C++ template metaprogramming should look like They have been greatly influential in our work We would also like to thank all of the contributors to the Brigand library and Louis Dionne for his metaprogramming library bench‐ mark Finally, we would like to thank Jon Kalb and Michael Caisse for their reviews, as well as our families, friends, and coworkers, who have been incredibly supportive Preface | ix ... Practical C+ + Metaprogramming Modern Techniques for Accelerated Development Edouard Alligand and Joel Falcou Beijing Boston Farnham Sebastopol Tokyo Practical C+ + Metaprogramming. .. syntactically impractical functional language, which later would actually be pro‐ ven by Todd Veldhuizen to be Turing-complete If your computer science courses need to be refreshed, this basically... fraction of this computational power A classic ros‐ ter of applications of C+ + template metaprogramming includes the following: • Complex constant computations • Programmatic type constructions

Ngày đăng: 12/11/2019, 22:27

Từ khóa liên quan

Mục lục

  • Cover

  • Programming Newsletter

  • Copyright

  • Table of Contents

  • Preface

    • A Journey of a Thousand Miles Begins with a Single Step

    • Understanding Metaprogramming

    • Conventions Used in This Report

    • Acknowledgments

    • Chapter 1. Introduction

      • A Misunderstood Technique

      • What Is Metaprogramming?

        • The Early History of Metaprogramming

        • Enter C++ Templates

        • How to Get Started with Metaprogramming

          • Checking Integer Type

          • Checking the Memory Model

          • Summary

          • Chapter 2. C++ Metaprogramming in Practice

            • A Typical Code Maintenance Assignment

            • Creating a Straightforward Interface

            • Generating Code Automatically

            • Making Values and Pointers Work Together

              • Type Manipulation 101

              • A Generic Function Translator

              • Extracting the C Function’s Parameters

Tài liệu cùng người dùng

Tài liệu liên quan