https://learnmoderncpp.com/ Skip to content Learn Modern C++ Discover a language matched to today's computing needs Menu * Home An Introductory Tutorial for Modern C++ Featured cpptutor4 Comments This site contains an original, self-contained guide to learning the fundamentals of Modern C++, intended to be the basis of a self-study course. Some Chapters depend upon material presented in earlier ones, so studying them linearly is recommended. There are also regular posts (accessible from the front page) which cover topics or areas of C++ not covered in the course. If you have any queries or suggestions, please leave a comment. The course materials are in a completed state and are made freely available here: 0. About this Tutorial 1. String and Character Literals 2. Variables, Scopes and Namespaces 3. Conditions and Operators 4. Functions 5. Arrays, Pointers and Loops 6. Enums and Structs 7. Strings, Containers and Views 8. Files and Formatting 9. Classes, Friends and Polymorphism 10. Templates, Exceptions, Lambdas, Smart Pointers * Coding Assignments for each Chapter Download the code examples (as a zipfile) or browse on GitHub. View details of the course topics. Last update: 2023/05/04 Modern C++ 101: Refactoring Legacy Code December 5, 2023December 6, 2023 cpptutorLeave a comment So here's a program written in a hurry, possibly in the style of a college CS student in about week six of learning C++ as a taught course. It's simple, it works, and it can (naturally) be improved upon: Continue reading "Modern C++ 101: Refactoring Legacy Code" - Selecting Functions at Runtime November 22, 2023November 24, 2023 cpptutorLeave a comment Not all choices can be made at compile-time, sometimes including which function (of several possibilities) to invoke. This article aims to cover all of the methods available to Modern C++ when selecting which function to call, with the decision made at runtime (based upon user input, for example). Terms covered in this article include function pointers, function objects, virtual functions and lambdas. Continue reading "Selecting Functions at Runtime" - Signaling error conditions without exceptions October 28, 2023November 7, 2023 cpptutorLeave a comment In this article we're going to look at a feature new to C++23 which supports a way to return an error condition from a function where a result value was anticipated. This is enabled in a new header which (as far as I am aware) does not require specific compiler support. (MSVC 19.37 with /std:c++latest was used to test the program code in this article.) Continue reading "Signaling error conditions without exceptions" - Selecting Functions at Compile Time September 26, 2023September 26, 2023 cpptutorLeave a comment When writing C++, we like to do as much of the work as possible at compile time; with C++ being a statically-typed language, we know the type(s) involved when compilation is taking place. In this article we'll look at how to pass and receive, variables and results, of differing types depending on the nature of the function call itself. The four methods we will examine are: function overloading, template specialization, tag dispatch, and SFINAE. Continue reading "Selecting Functions at Compile Time" - Concepts 101 September 3, 2023September 3, 2023 cpptutorLeave a comment Concepts finally appeared in the language with the completion of C++20--I say finally as work to specify them had been going on continuously since before the release of C++11. This article attempts to explain their rationale and usage, with examples that should compile on any up-to-date C++ compiler when specifying -std=c++20 (or /std:c++20 for MSVC). So why do we need concepts when template syntax has been available since C++98? Put simply, templates have been (successfully) used in ways that were never envisaged; however the reporting of invalid instantiations and similar errors has not kept pace. Also, the use of auto in function parameter lists gives us template syntax "invisibly", which the novice/intermediate coder may not expect. Continue reading "Concepts 101" - Designing Traits and Policy Classes August 20, 2023August 23, 2023 cpptutorLeave a comment Traits and policy classes are often used with C++ generics, but their role and purpose is often something of an enigma even to experienced C++ programmers. The definitions of these two terms overlap to some extent, and could be summarized as follows: * Traits represent natural additional properties of a template parameter. * Policies represent configurable behavior for generic functions and types. In this article the example code will show use of both of these two, with type information obtained through a (specialized) traits class and functionality obtained from a (generic) policy class. Continue reading "Designing Traits and Policy Classes" - Applying Functional Programming Techniques to Modern C++ August 3, 2023August 23, 2023 cpptutorLeave a comment Functional programming (FP) is a discipline and practice of computer science where Mathematical concepts come to the fore. There are a number of popular FP languages out there, but we can stay with C++ and explore some of the themes of FP by using a subset of familiar syntax. In this article we'll look at a number of FP concepts, such as purity, immutability and use of higher-order functions, and how they can be concisely and usefully represented in Modern C++. 1. Purity A function is considered pure if its operation depends only upon its formal input parameters, that is not accessing any external (global) state. Global state in FP is a little like the historical Goto statement, shunned but sometimes necessary, and so is by design abstracted away as much as possible. A C++ lambda is pure if it has an empty capture clause, for example: Continue reading "Applying Functional Programming Techniques to Modern C++" - Designing Classes for Serialization (4) July 23, 2023July 23, 2023 cpptutorLeave a comment In the previous articles of this mini-series we've covered the use of custom serialization and deserialization functions to generate and interpret XML. In this article we'll look at how to add to existing classes to give them the ability to utilize the (de-)serialization logic already written. The process is similar to before: create an in-memory model and then serialize it, or start with blank containers and then read in the data, checking for errors. Continue reading "Designing Classes for Serialization (4)" - Designing Classes for Serialization (3) July 9, 2023November 4, 2023 cpptutorLeave a comment In the previous articles we've looked at creating an in-memory XML hierarchy (or DOM), and serializing from and deserializing to this memory model. In this article we're going to look at querying the DOM in a similar fashion to JavaScript methods such as document.getElementById() available in a browser. The scheme chosen is to overload operator [] (twice) and provide other convenience methods such as name() and numberOfChildren(). Continue reading "Designing Classes for Serialization (3)" - Designing Classes for Serialization (2) June 27, 2023July 9, 2023 cpptutorLeave a comment The complement of serialization is deserialization, and this is typically more difficult as it requires character- or pattern-matching of the input, plus checking for erroneous input. To match a stream of XML the possibility of using std::getline() was considered, but this is too inflexible as regards whitespace. Stream input to a std::string was also considered, but again this is unsuitable as breaking all input at whitespace is not what is needed. Continue reading "Designing Classes for Serialization (2)" - Partially Mocking an ES Module with Jest and Node.js June 26, 2023 cpptutorLeave a comment Disclaimer: This article has nothing to do with C++. Also, I do not have a huge amount of experience with the Node.js ecosystem. This information is provided here because of the issues I had reading outdated, and often incorrect, "solutions" on forums and blogs. I hope someone finds it useful. Continue reading "Partially Mocking an ES Module with Jest and Node.js" - Designing Classes for Serialization (1) June 18, 2023June 27, 2023 cpptutorLeave a comment Data held in memory must be saved to a store in order to outlive the running-time of the program it belongs to. Simply dumping binary data to disk is not usually the best way, or even a practicable way, of achieving this (think security issues, endianness, 32-bits/64-bits etc.) The terms "serialization" and its companion "deserialization" are used to describe the conversion between the in-memory model and a (usually textual) disk-file representation. As an example, consider a web-browser which reads an HTML web page (and its dependencies) from a network connection and converts (deserializes) it into a format which can be manipulated through the DOM (Document Object Model). In this article we'll concentrate more on serialization, which is often a simpler task. Continue reading "Designing Classes for Serialization (1)" - Common mistakes to avoid in Modern C++ (2) May 23, 2023May 29, 2023 cpptutorLeave a comment In the previous article we looked at how to avoid falling into some of the pitfalls of Modern C++. For this article we'll cover some of the more advanced areas of Modern C++, with explanations of how to avoid problems that novice programmers often encounter. Continue reading "Common mistakes to avoid in Modern C++ (2)" - Common mistakes to avoid in Modern C++ (1) May 18, 2023May 23, 2023 cpptutorLeave a comment Learning a new programming language is hard, and is especially daunting when as a novice programmer the compiler rejects pretty much every program you write the first time you try to compile it. Of course, compiler errors and warnings are important as they encourage you to write correct code and ensure that your program actually runs at all, and works as expected. C++ has traditionally been seen as one of the harder programming languages for beginners to pick up; ideally you should already have some coding experience under your belt in (an)other programming language(s) before trying to learn Modern C++. This mini-series intends to examine the parts of C++ that novice (and more experienced) programmers sometimes fail to understand fully, leading to buggy and/or sub-optimal code. Continue reading "Common mistakes to avoid in Modern C++ (1)" - Generic classes in Modern C++ April 22, 2023May 3, 2023 cpptutorLeave a comment In this article we're going to continue the theme of a class-designer's toolkit, looking at adding generics (template functionality) to C++ classes. Templates have been around since C++98, and almost all C++ programmers will have used them: std::vector has the type int as a parameter, itself being a specialization of a generic class. Any user-defined type can be used as a type parameter, having equal status to the built-in types. Generic classes Classes which are preceded by a the template keyword are automatically turned into generic classes. These classes can have any number of template parameters, we're going to concentrate on classes having just one, this being a template type parameter. The class definition starts of as follows: Continue reading "Generic classes in Modern C++" - Posts navigation Older posts Blog at WordPress.com. Learn Modern C++ Blog at WordPress.com. * Subscribe Subscribed + [croppe] Learn Modern C++ Join 31 other subscribers [ ] Sign me up + Already have a WordPress.com account? Log in now. * + [croppe] Learn Modern C++ + Customize + Subscribe Subscribed + Sign up + Log in + Report this content + View site in Reader + Manage subscriptions + Collapse this bar Loading Comments... Write a Comment... [ ] Email (Required) [ ] Name (Required) [ ] Website [ ] [Post Comment] [b]