Advanced C++ Programming Cookbook

3 (1 reviews total)
By Dr. Rian Quinn
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Using Exceptions for Error Handling

About this book

If you think you've mastered C++ and know everything it takes to write robust applications, you'll be in for a surprise. With this book, you'll gain comprehensive insights into C++, covering exclusive tips and interesting techniques to enhance your app development process.

You'll kick off with the basic principles of library design and development, which will help you understand how to write reusable and maintainable code. You'll then discover the importance of exception safety, and how you can avoid unexpected errors or bugs in your code. The book will take you through the modern elements of C++, such as move semantics, type deductions, and coroutines. As you advance, you'll delve into template programming - the standard tool for most library developers looking to achieve high code reusability. You'll explore the STL and learn how to avoid common pitfalls while implementing templates. Later, you'll learn about the problems of multithreaded programming such as data races, deadlocks, and thread starvation. You'll also learn high-performance programming by using benchmarking tools and libraries. Finally, you'll discover advanced techniques for debugging and testing to ensure code reliability.

By the end of this book, you'll have become an expert at C++ programming and will have gained the skills to solve complex development problems with ease.

Publication date:
January 2020
Publisher
Packt
Pages
454
ISBN
9781838559915

 

Getting Started with Library Development

In this chapter, we will cover some useful recipes for creating our own libraries, including an explanation of the principle of least surprise, which encourages us to implement libraries using semantics that our users are already familiar with. We will also look at how to namespace everything to ensure our custom libraries don't conflict with others. In addition, we will look at how to create header-only libraries, as well as some best practices associated with library development. Finally, we will conclude this chapter with a demonstration of the boost libraries to show you what a large library looks like and how it can be used by users in their own projects.

In this chapter, we will cover the following recipes:

  • Understanding the principle of least surprise
  • How to namespace everything
  • Header-only libraries
  • Learning library development...
 

Technical requirements

To compile and run the examples in this chapter, you must have administrative access to a computer running Ubuntu 18.04 with a functional internet connection. Prior to running these examples, you must install the following packages using the following command:

> sudo apt-get install build-essential git cmake

If this is installed on any operating system other than Ubuntu 18.04, then GCC 7.4 or higher and CMake 3.6 or higher will be required.

 

Understanding the principle of least surprise

When either using existing C++ libraries or creating your own, understanding the principle of least surprise (also called the principle of least astonishment) is critical to developing source code efficiently and effectively. This principle simply states that any feature that a C++ library provides should be intuitive and should operate as the developer expects. Another way of saying this is that a library's APIs should be self-documenting. Although this principle is critically important when designing libraries, it can and should be applied to all forms of software development. In this recipe, we will explore this principle in depth.

Getting ready

As with all of the recipes...

 

How to namespace everything

When creating a library, it is important to namespace everything. Doing so ensures that of the APIs provided by the library cause name collisions with the user's code or with facilities provided by other libraries. In this recipe, we will demonstrate how to do this in our own libraries.

Getting ready

As with all of the recipes in this chapter, ensure that all of the technical requirements have been met, including installing Ubuntu 18.04 or higher and running the following in a Terminal window:

> sudo apt-get install build-essential git cmake

This will ensure your operating system has the proper tools to compile and execute the examples in this recipe. Once you have done this, open a new...

 

Header-only libraries

Header-only libraries are exactly as they sound; an entire library is implemented using header files (usually a single header file). The benefit of header-only libraries is that they are easy to include in your project as you simply include the header and you are done (there is no need to compile the library as there are no source files to compile). In this recipe, we will learn about some issues that arise when attempting to create a header-only library and how to overcome them. This recipe is important because, if you plan to create your own library, a header-only library is a great place to start and will likely increase your adoption rates as downstream users will have less trouble integrating your library into their code base.

Getting ready

...
 

Learning library development best practices

When writing your own library, there are certain best practices that all library authors should adhere to. In this recipe, we will explore some higher-priority best practices and conclude with some information about a project dedicated to defining these best practices, including a registration system that provides your library with a grade as to how well it compiles. This recipe is important as it will teach you how to make the highest-quality library, ensuring a strong and vibrant user base.

Getting ready

As with all of the recipes in this chapter, ensure that all of the technical requirements have been met, including installing Ubuntu 18.04 or higher and running the following in...

 

Learning how to use the boost APIs

The boost libraries are a set of libraries designed to work in conjunction with the standard C++ libraries. In fact, a lot of the libraries that are currently being provided by C++ originated from the boost libraries. The boost libraries provide everything from containers, clocks, and timers to more complicated mathematical APIs such as graphs and CRC calculations. In this recipe, we will learn how to use the boost libraries, specifically to demonstrate what a large library looks like and how such a library would be included in a user's project. This recipe is important as it will demonstrate just how complicated a library can get, teaching you how to write your own libraries accordingly.

Getting ready

...

About the Author

  • Dr. Rian Quinn

    Dr. Rian Quinn is the Chief Technology Officer (CTO) in the Advanced Technologies Business Unit at Assured Information Security, Inc., having focused on trusted computing, hypervisor-related technologies, machine learning/artificial intelligence, and cybersecurity for more than 10 years, and has 9 years of technical management and business development experience. He holds a Ph.D. in computer engineering, with specializations in information assurance and computer architectures, from Binghamton University. He is the cofounder and lead developer of the Bareflank hypervisor, and is an active member of several open source projects, including Microsoft's Guideline Support Library (GSL) and OpenXT. Rian previously wrote Hands-On System Programming with C++.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Examples are very trivial

Recommended For You

Advanced C++ Programming Cookbook
Unlock this book and the full library FREE for 7 days
Start now