The C++ Core Guidelines website is a living, online, community-developed document containing up-to-date idioms and advice from C++ community thought leaders that are continuously updated as new C++ standards emerge and better approaches are discovered for creating fast, robust, safe, interoperable, and sustainable C++ software.
Resource Information | Details |
---|---|
Resource name | C++ Core Guidelines |
Website | https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines |
Editors | Bjarne Stroustrup and Herb Sutter |
Version | July 2, 2024 (Git commit 50afe0234ce4f2f6bde7d9b0d86e926bd479f9aa) |
Focus | Language idioms and guidelines |
Background
Bjarne Stroustrup created C++ in 1979 while he was at Bell Labs as "C with Objects" (which was just a set of macros that generated C code before being compiled by a C compiler) with the first public version of Bell Labs Cfront C++ released in 1985. C++ was initially designed and built on top of C and designed to interoperate with C at a fundamental level. (This legacy with C has been both one of C++'s greatest strengths and weaknesses.) In the years since then, C++ has undergone several International Standards Organization (ISO) standards updates and is currently developing an updated ISO C++ standard every three years, starting with ISO C++11 (where C++23 is the most recent approved standard at the time of this writing). Over the last 40 years, billions of lines of C++ code have been written and deployed with varying degrees of quality, safety, robustness, and sustainability. The way that C++ has evolved over the last 40+ years from a thin layer on top of C (together with the need for backward compatibility supporting billions of lines of previously written C++ code), has given rise to a vast and complex language and standard library. When used in raw form by undisciplined developers, C++ has been used to arguably create some of the most confusing, fragile, unsafe, and unsecure software in existence (rivaling C in this respect). However, despite this, C++ has seen a resurgence in popularity and usage in recent years, with C++ being the fastest-growing language in 2022 and overtaking C to become the second most popular language (behind Python) in June 2024.9,10
Over the years, as C++ has evolved, numerous authors have written books on how to successfully use the C++ programming language to create quality C++ software.2 However, with each new C++ standard (which includes new language features and library extensions) and with updated experience and new idioms, much of the guidance in these prior books becomes obsolete (and even counter-productive) as time passes. While some authors have put out updated versions of their books to compensate for new C++ standards and idioms (e.g., Scott Meyer's popular Effective C++ book4,6,7 and other Effective C++ books5,8), this is unsustainable and leaves a hole in guidance for the most effective usage of modern C++ as it continues to evolve.
Overview of the C++ Core Guidelines
Would it not be nice if there was a way to take the best advice and idioms from the excellent books written over the years by thought leaders in the C++ community and update and combine them with modern language features, libraries, and idioms? And what if these combined guidelines could be constantly kept up-to-date as new C++ standards come out and the C++ community responds to the shifting needs of the software development community? That is precisely what the C++ Core Guidelines website seeks to do. Bjarne Stroustrup (the inventor of C++ and still active in the C++ community) and Herb Sutter (author of many excellent books on C++ and active member of the C++ standards committee) are the primary editors of the C++ Core Guidelines site, and the content has been contributed by at least 330+ other C++ authors and developers.
The C++ Core Guidelines site is currently organized as a single github.io
page that is broken up into major sections covering several areas, including:
- In: Introduction
- P: Philosophy
- I: Interfaces
- F: Functions
- C: Classes and class hierarchies
- Enum: Enumerations
- R: Resource management
- ES: Expressions and statements
- Per: Performance
- CP: Concurrency and parallelism
- E: Error handling
- Con: Constants and immutability
- T: Templates and generic programming
- CPL: C-style programming
- SF: Source files
- SL: The Standard Library
with additional supporting sections and material.
These guidelines are based only on the standard C++ language and standard C++ library; with the addition of the small and simple "GSL: Guidelines support library".3 (The latter is needed to codify essential idioms that the C++ standard library does not directly support.)
A significant motivation and focus of the C++ Core Guidelines is the creation of safe and secure C++ software. For example, the main page contains some form of the words "safe", "check", "bounds" (i.e., "bounds check"), and "secure" over 600 times!
Some Important Details
When converted to a single PDF file, the main C++ Core Guidelines page1 is currently an 869-page book.
(Therefore, this would represent the largest book ever written on C++ coding guidelines.)
In addition, some of the material has not yet been filled in, and there are placeholders for the missing material denoted by ???
(currently 270 of these).
(So many issues are still yet to be addressed.)
As a github.io
site, the C++ Core Guidelines site is generated from a set of Markdown files maintained in a collaborative GitHub repository.
The backend GitHub repository currently has 1963 watchers, 5.5k forks, 42k stars, and over 330 contributors.
Therefore, this is a significant effort to define guidelines and best practices for modern C++.
Many of the C++ Core Guidelines have LLVM Clang-Tidy checks that can be applied to ensure that C++ software follows them.
(At the time of this writing, there are 30 of the cppcoreguidelines-*
clang tidy checks, of which eight can be automatically fixed using the auto-refactoring feature of Clang-Tidy.11
Therefore, most C++ Core Guidelines are not checked and/or cannot be easily checked or enforced by a static analysis tool.)
The Microsoft C++ compiler also supports enforcing many of these guidelines for Windows development environments.
Summary
The C++ Core Guidelines site represents a significant step forward in the development, dissemination, and enforcement (through tools like Clang-Tidy) of idioms and best practices for developing quality, sustainable C++ software.
While there is more work to be done in expanding the coverage of these guidelines and tweaking the existing guidelines (and you can help with that effort by submitting issues and pull requests to the CppCoreGuidelines
GitHub project), the most significant challenge will be on refactoring the large amount of legacy C++ software that does not follow these guidelines.
For that, the C++ community will need to continue to develop and improve C++ static analysis and automated refactoring tools.
References
- 1C++ Core Guidelines: github.io site
- 2C++ Coding Guidelines Books
- 3GSL: Guidelines support library".
-
4Effective C++, First Edition
Scott Meyers. Addison-Wesley 1992 -
5More Effective C++
Scott Meyers. Addison-Wesley, 1996 -
6Effective C++, Second Edition
Scott Meyers. Addison-Wesley, 1997 -
7Effective C++, Third Edition
Scott Meyers. Addison-Wesley, 2005 -
8Effective Modern C++
Scott Meyers. O'Reilly. 2015 -
9TIOBE Programming Language Index News (June 2024): C++ Rises to Second Place
Megan Crouse, TechRepublic, June 11, 2024 - 10Tiobe Programming Language Index
- 11Extra Clang Tidy Checks