Skip to content

Instantly share code, notes, and snippets.

@lsegal
Last active July 11, 2021 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsegal/d7c1a16719f864a817790c65d6001e37 to your computer and use it in GitHub Desktop.
Save lsegal/d7c1a16719f864a817790c65d6001e37 to your computer and use it in GitHub Desktop.
Modern C++ resources

Top Modern C++ Features (an abridged ad-hoc list)

Note: most of this document is pulled from resources at cppreference.com. See pages like C++11, C++14, C++17, C++20 for a good summary changelog of each specific C++ specification. Certain topics are linked elsewhere when the site provides a more thorough explanations of a given feature.

RAII

If you have not already read about "RAII" (Resource Acquisition is Initialization), do so first. RAII controls the lifetime of an object, aka when pointers get destructed, when files get closed, when locks or resources get released, and so on.

RAII is not a change to the language, so it's not technically "modern", but it is a fundamental concept of all modern STL objects as well as a conventional expectation in modern C++ that your code is "RAII-compliant". It is core to understanding how many new STL features are built.

Syntax additions

STL additions (a very abridged grab-bag)

Smart pointers

Functional programming

  • std::function is the high-level type you use to receive lambdas/function pointers aka callbacks (C++11)
  • std::bind generates a "bound" function that can add additional parameters to a function callback. Necessary if you want to pass an object method as a callback and need the this context bound. See also "placeholders" (defined in the article). (C++11)

Threads and data access management

  • std::thread is a standardized platform-independent implementation to launch platform threads (POSIX, etc) (C++11)
  • std::atomic is a container type that can be used on most primitive types (ints, bools) to allow for race-free access without needing mutexes. Use this first if you're dealing with primitive data. (C++11)
  • std::mutex and std::recursive_mutex for data access synchronization across threads (C++11)
  • std::scoped_lock (C++17) and std::unique_lock (C++11) for locking thread access on mutexes (RAII lock management)

Async support

  • std::async for an abstraction on threading. Does work "later" without worrying about whether it's done on a thread or runloop or process etc. (C++11)
  • std::future is what an async call returns. It is the abstraction of the "work being done". (C++11)
  • std::promise even more abstraction on futures. Since promises rely on futures quite explicitly and don't necessarily add new behavior, futures are usually all you need. (C++11)

High level containers

  • std::tuple allows creating lists of heterogeneous objects for arguments or return types without defining an explicit struct to hold them all. Ex: auto [a, b] = std::tuple<int, bool>{1, true}; (C++11)
  • std::optional is a way to express "nullable" types without requiring pointers. (C++17)
  • std::variant is the modern way to express a "union" aka polymorphic value. (C++17)

String/IO manipulations

  • <regex> is now a standardized library header (C++11)
  • <filesystem> header standardizes filesystem operations across platforms, as well as the specifics of path representation via std::filesystem::path. (C++17 but also available in Boost)
  • std::string_view (more detail) is going to (eventually) take over as the abstracted "string interface" type, allowing for u8, u16, or u32 string types to be passed around without needing direct knowledge about internals. (C++17)

Chrono / Time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment