C++ Books and Possible Insanity (RegEx’ing C++)…

By | January 18, 2017

C++ Books

So a number of weeks ago I asked a question on G+ about peoples preferences for buying technical books (hard copy, soft copy or both). The reasons I asked was that I wanted to buy a number of books to re-learn C++. I originally (tried) to learn C++ back around 2000 and I can't say I did a very god job. I think I understood most of the language features but didn't know the run-time library at all. I have released a few bits of C++ code recently and one observation that was made was that I programmed C++ Builder in a Delphi way not a C++ way. That was a valid observation and it was as I mentioned above, due to me not knowing the C++ run-time and Standard Template Library.

So below is a list of the books I ended up buying as I think they could be useful to others – some who may be interested in learning C++ and are from a Delphi background or others who may find them generally useful.

Programming: Principles and Practice Using C++ – Bjarne Stroustrup

As mentioned above I wanted to re-learn C++ from the beginning and after downloading a sample of this book on Kindle I ended up buying the full kindle version and then a number of weeks later a paper back version.

https://www.amazon.co.uk/gp/product/0321992784/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1

The book is essentially a college / university level course book for learning C++ from the very man who invented the language – that was one of the appealing items about the book. Secondly it teachs you C++ in a more modern C++11 way rather than the old C++98 way. One of the aspects of C++ I always struggled with was parameter passing and pointers. I've used Delphi since version 1 in 95 and basically forgotten all I knew about pointer from Turbo Pascal as I've rarely needed to use them.

I found this book pretty good to learn from. I didn't do all the exercises as I was trying to learn as must as I could as quickly as I could however one of the nice aspects of the book is that it starts early on by getting you to create a class which essentially mirrors the STL's vector class which is one of the most useful collections in the STL.

The book has the following chapters:

  • Part 1: The Basics
    • Hello World
    • Objects, Types and Values
    • Computation
    • Errors
    • Writing a Program
    • Completing a Program
    • Technicalities: Funtions, etc.
    • Technicalities: Classes, etc.
  • Part 2: Input and Output
    • Input and Output Streams
    • Customizing Input an Output
    • A Display Model
    • Graphics Classes
    • Graphics Class Design
    • Graphing Functions and Data
    • Graphical User Interfaces
  • Part 3: Data and Algorithms
    • Vector and Free Store
    • Vectors and Arrays
    • Vector, Templates and Exceptions
    • Containers and Iterators
    • Algorithms and Maps
  • Part 4: Broadening the view
    • Ideals and History
    • Text Mnaipulation
    • Numerics
    • Embbedded Systems Programming
    • Testing
    • The C Programming Language
  • Part V: Appendices
    • Language Summary
    • Standard Library Summary
    • Getting Started with Visual Studio
    • Installing FLTK
    • GUI Implementation

One thing it is not is a reference book for the STL, you need another book for that (see below). One criticism of the book I have is that for the code examples it does not use a fixed width font (same for the e-book and paper version). I didn't think this help me understand some of the code example however saying that I don't regret buying the book and will keep referring to it and possible retired my older C++ reference book.

The C++ Standard Library: A Tutorial and Reference – Nicolai M. Josuttis

I have to say from the start that this is a MUST BUY BOOK! It doesn't matter whether you're a beginner, intermediate or advanced C++ programmer I think this book should be on your shelf (virtual or otherwise) as its been in valuable to me learning the STL, C++ String and RegEx.

https://www.amazon.co.uk/gp/product/0321623215/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1

It's a very good reference book which complements the above learning book but it's not just a terse list of interface methods but has tutorials on how to use the various classes. It also has some early chapters that go though C++11 basics, one useful one for me was the use of std::unique_ptr<>.

The book has the following chapters:

  • Chapter 1: About this book
  • Chapter 2: Intoduction to C++ and the Standard Library
  • Chapter 3: New Language Features
  • Chapter 4: General Concepts
  • Chapter 5: Utilities
  • Chapter 6: The Standard Template Library
  • Chapter 7: STL Containers
  • Chapter 8: STL Container Members in Detail
  • Chapter 9: STL Iterators
  • Chapter 10: STL Function Objects and Using Lambdas
  • Chapter 11: STL Algorithms
  • Chapter 12: Special Containers
  • Chapter 13: Strings
  • Chapter 14: Regular Expresions
  • Chapter 15: Input/Output Using Stream Classes
  • Chapter 16: Internationalization
  • Chapter 17: Numerics
  • Chapter 18: Concurrency
  • Chapter 19: Allocators

I thought this book so good I brought it twice (hardback and soft copy).

Mastering Regular Expressions: Understand Your Data and Be More Productive – Jeffrey E. F. Friedl

Hold on I here you say, This isn't a C++ book!. Well yes you're right but I need to first to explain my current madness.

The current project I'm working on is a C++ Open Tools API project to provide C++ Builder with code browsing using the CTRL+SHIFT+UP/DOWN keyboard configuration and Code Completion. It sounds simple BUT is not. The OTA Project itself is IDE agnostic (I'll write about that when its finished) but the hard part is parsing the C++ language. I currently don't have a C++ parser in Browse and Doc It to use (but I do need one) however I thought that would be too much work to get up and running so I chose to try and do it with RegEx instead (see the later part of this article).

https://www.amazon.co.uk/gp/product/B007I8S1X0/ref=oh_aui_d_detailpage_o01_?ie=UTF8&psc=1

From all accounts, even though this book is a little old, it is still THE regular expression book to read. It doesn't use Delphi or C++ but more Perl with a few other languages interdispersed however the languages are immaterial as it does an excellent job of building your knowledge of regular expressions. The book has given me detail behind numerous regular expression elements which tutorials on the net or other net references have not provided. I think that if you want to learn regular expressions (and I strongly suggest everyone does as they may completely change the way you manipulate information) then you can't go wrong with this book.

The book has the following chapters:

  • 1. Introduction to Regular Expressions
  • 2. Extended Introduction Examples
  • 3. Overview of Regular Expression Features and Flavors
  • 4. The Mechanics of Expression Processing
  • 5. Practial RegEx Techniques
  • 6. Crafting an Efficient Expression
  • 7. Perl
  • 8. Java
  • 9. .NET
  • 10. PHP

This book was only available (at sensible prices) in Kindle format.

Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14 – Scott Meyers

This was actually the first book I brought (was only available in paper back) however I quickly realised that I didn't understand very much.

https://www.amazon.co.uk/gp/product/1491903996/ref=oh_aui_detailpage_o05_s00?ie=UTF8&psc=1

You need to be reasonable proficient in C++, especially the C++11 language for this book to be relevant. I've currently read very little of the book however it does go into a lot of detail on specific topics. I think it will be useful in the long term but for now I'll be using the other C++ books to get my project complete.

The book has the following chapters:

  • 1. Deducing Types
  • 2. auto
  • 3. Moving to Modern C++
  • 4. Smart Pointers
  • 5. Rvalue References, Move Semantics, and Perfect Forwarding
  • 6. Lambda Expressions
  • 7. The Concurrency API
  • 8. Tweaks

Possible Insanity (RegEx'ing C++)

So have I gone insane? Probably. As I mentioned above I've chosen to use regular expressions to parse the C++ code to help identifier the classes and functons. I've already done the work on the OTA framework for the code and created an initial collaboration object for holding the code structure (all in C++11) but I still needed to define the RegEx code.

Regular expressions as they are currently defined are very powerful however to create a regular expression of the length required to parse elements of code is somewhat impractical so I decided to modify the concept by providing a mechanism to insert macros into the regular expressions.

The screen shot below is an application (sorry in Delphi) I've created to help me generate, test and debug the regular expressions. the file format I've chosen is a little like an INI file. Lines can be commented out with commas at the start. All regular expressions must be defined as a key=value pair and a macro must be defined before it can be used in a regular expression below. The form of the macro should be familiar $(MacroName). The application searches multiple files for an expression and display a treeview of the match results so that I can examine the matches.

What this allows me to do is essentially define the grammar elements of the C++ code as macros and build more complex macros (say for a class header definition) from simpler grammatical elements.

Eventually on Sunday morning I got the full expression for a class header definition to work which means I can proceed with implementing this technique. To help me do this I also spent time creating a SynEdit highlighter for regular expressions (with my modifications) so that I could see into the expression (the full expanded expression can be shown in the editor at the bottom right of the application).

So all I need to do now is write the function expressions, code this all up in C++ (along with unit testing of the macros to ensure the C++ regex is the same as the TRegEx class in Delphi. Then its reading the information, placing it into the structure and providing mechanisms for finding functions.

I shall now go back to my nice padded cell with crayons 🙂