Book Image

The Modern C++ Challenge

Book Image

The Modern C++ Challenge

Overview of this book

C++ is one of the most widely-used programming languages and has applications in a variety of fields, such as gaming, GUI programming, and operating systems, to name a few. Through the years, C++ has evolved into (and remains) one of the top choices for software developers worldwide. This book will show you some notable C++ features and how to implement them to meet your application needs. Each problem is unique and doesn't just test your knowledge of the language; it tests your ability to think out of the box and come up with the best solutions. With varying levels of difficulty, you'll be faced with a wide variety of challenges. And in case you're stumped, you don't have to worry: we've got the best solutions to the problems in the book. So are you up for the challenge?
Table of Contents (15 chapters)

To get the most out of this book

As previously mentioned, you need a basic familiarity with the C++ language and the standard library in order to be able to utilize this book, or you can learn that along the way. In any case, this book will teach you how to solve problems, but it will not teach you about the language and features utilized in the solutions. You will need a compiler with C++17 support; a complete list of required libraries as well as possible compilers you can use can be found in the Software Hardware List available in the code bundle. In the following sections, you will find detailed instructions for downloading and building the code from this book.

Download the example code files

You can download the code files with the solutions to the problems in this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/The-Modern-Cpp-Challenge. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Building the code

Although a large number of 3rd party libraries are used throughout the book, all these libraries, as well as all the solutions provided in the book are cross-platform and run on all platforms. However, the code has been developed and tested with Visual Studio 2017 v15.6/7 on Windows 10 and Xcode 9.3 on Mac OS 10.13.x.

If you are using Xcode on a Mac, there are two features used in the book that are not available with the LLVM toolset included in Xcode; these are the filesystem library and std::optional. However, these have been designed based on the Boost.Filesystem and Boost.Optional libraries and the use of the mentioned standard libraries in the proposed solutions is easily interchangeable with the Boost libraries. In fact, the accompanying code is written so that it works with either of the two; controlling which one to use is done with several macros. Instructions for building either with one or another are provided below, although the same information is also available in the source archive.

In order to support most of the development environments and build systems you could use on various platforms, the code is provided with CMake scripts. These are used to generate projects or build scripts for your preferred toolset. If you do not have CMake installed on your machine, you can get it from https://cmake.org/. Below, you can find instructions for using CMake to generate Visual Studio and Xcode scripts. For other tools, please refer to the CMake documentation, if necessary.

How to generate projects for Visual Studio 2017

Do the following in order to generate Visual Studio 2017 projects to target the x86 platform:

  1. Open a command prompt and go to the build directory in the source code root folder.
  2. Execute the following CMake command:

    cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF
  1. After completion, the Visual Studio solution can be found at build/cppchallenger.sln.

If you want to target the x64 platform instead, use the generator called "Visual Studio 15 2017 Win64". Visual Studio 2017 15.4 supports both filesystem (as an experimental library) and std::optional. If you use a previous version, or just want to use the Boost libraries instead, you can generate the projects using the following command, after you properly install Boost:

cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF -DBOOST_FILESYSTEM=ON -DBOOST_OPTIONAL=ON -DBOOST_INCLUDE_DIR=<path_to_headers> -DBOOST_LIB_DIR=<path_to_libs>

Make sure that the paths to the headers and static library files do not include trailing backslashes (i.e. \).

How to generate projects for Xcode

Several solutions in the last chapter utilize the libcurl library. For SSL support, this library needs to be linked with the OpenSSL library. Do the following to install OpenSSL:

  1. Download the library from https://www.openssl.org/.
  2. Unzip the archive and, in a terminal, go to its root directory.
  3. Build and install the library with the following commands (executed in this order):

    ./Configure darwin64-x86_64-cc shared enable-ec_nistp_64_gcc_128 no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl/macos-x86_64

    make depend

    sudo make install

Until std::optional and the filesystem library will be available with Xcode's Clang, you need to use Boost. Do the following to install and build the Boost libraries:

  1. Install Homebrew from https://brew.sh/.
  2. Run the following command to download and install Boost automatically.

    brew install boost
  3. After installation, the Boost library will be available at /usr/local/Cellar/boost/1.65.0.

In order to generate projects for Xcode from the sources you have to:

  1. Open a terminal and go to the build directory in the source code root directory.
  2. Execute the following CMake command:

    cmake -G Xcode .. -DOPENSSL_ROOT_DIR=/usr/local/bin -DOPENSSL_INCLUDE_DIR=/usr/local/include/ -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF -DBOOST_FILESYSTEM=ON -DBOOST_OPTIONAL=ON -DBOOST_INCLUDE_DIR=/usr/local/Cellar/boost/1.65.0 -DBOOST_LIB_DIR=/usr/local/Cellar/boost/1.65.0/lib
  3. After completion, the Xcode project can be found at build/cppchallenger.xcodeproj.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

int main()
{
std::cout << "Hello, World!\n";
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

template<typename C, typename... Args>
void push_back(C& c, Args&&... args)
{
(c.push_back(args), ...);
}

Any command-line input or output is written as follows:

$ mkdir build
$ cd build

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.