Convert Figma logo to code with AI

isocpp logoCppCoreGuidelines

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++

42,437
5,422
42,437
249

Top Related Projects

6,118

Guidelines Support Library

Abseil Common Libraries (C++)

Style guides for Google-originated open-source projects

Collaborative Collection of C++ Best Practices. This online resource is part of Jason Turner's collection of C++ Best Practices resources. See README.md for more information.

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

Quick Overview

The CppCoreGuidelines repository is a collaborative effort to create a set of guidelines for writing modern C++ code. It aims to help developers write cleaner, safer, and more efficient C++ by providing best practices and recommendations. The guidelines are maintained by the Standard C++ Foundation and are widely respected in the C++ community.

Pros

  • Comprehensive coverage of C++ best practices
  • Regularly updated to reflect modern C++ standards
  • Backed by industry experts and the C++ community
  • Provides rationale and examples for each guideline

Cons

  • Can be overwhelming for beginners due to the sheer volume of guidelines
  • Some guidelines may be controversial or not universally applicable
  • Requires frequent updates to keep pace with evolving C++ standards
  • May conflict with established practices in some organizations

Getting Started

To get started with the C++ Core Guidelines:

  1. Visit the repository at https://github.com/isocpp/CppCoreGuidelines
  2. Read the README.md file for an introduction
  3. Browse the CppCoreGuidelines.md file for the full set of guidelines
  4. Consider using a static analysis tool that supports the guidelines, such as:
    • Clang-Tidy
    • Visual Studio's C++ Core Check
    • PVS-Studio

Example usage with Clang-Tidy:

clang-tidy your_file.cpp -checks=cppcoreguidelines-*

Note that the C++ Core Guidelines are not a code library, but rather a set of recommendations for writing C++ code. Therefore, there are no code examples or installation instructions in the traditional sense. Instead, developers are encouraged to read and apply the guidelines in their C++ projects.

Competitor Comparisons

6,118

Guidelines Support Library

Pros of GSL

  • Provides concrete implementations of guidelines
  • Offers lightweight, header-only library for easy integration
  • Includes useful abstractions like span for safer array handling

Cons of GSL

  • Narrower scope, focusing on specific guidelines implementation
  • May require more frequent updates to align with evolving C++ standards
  • Less comprehensive coverage of C++ best practices

Code Comparison

CppCoreGuidelines (conceptual guideline):

// Guideline: Use span<T> for array parameters
void process_array(span<int> arr);

GSL (concrete implementation):

#include <gsl/span>
void process_array(gsl::span<int> arr) {
    // Implementation using GSL's span
}

Summary

CppCoreGuidelines provides a comprehensive set of best practices and guidelines for modern C++ development, covering a wide range of topics. It offers explanations, rationales, and examples for each guideline.

GSL, on the other hand, is a practical implementation of some of these guidelines. It provides a library of types and functions that developers can directly use in their code to follow the guidelines more easily.

While CppCoreGuidelines is more extensive and educational, GSL offers immediate, practical tools for implementing specific guidelines in code. Both projects complement each other, with GSL serving as a practical companion to the broader CppCoreGuidelines.

Abseil Common Libraries (C++)

Pros of Abseil

  • Provides a comprehensive set of C++ library components
  • Actively maintained and updated by Google
  • Offers performance-optimized implementations of common utilities

Cons of Abseil

  • Larger codebase and potential overhead for smaller projects
  • May introduce dependencies on Google-specific coding practices
  • Steeper learning curve for developers unfamiliar with Google's C++ style

Code Comparison

CppCoreGuidelines (example guideline):

// C.47: Define and initialize member variables in the order of member declaration
class Widget {
    int x = 0;
    int y = 0;
    int z = 0;
};

Abseil (example utility):

#include "absl/strings/str_cat.h"

std::string result = absl::StrCat("Hello, ", name, "! You are ", age, " years old.");

Summary

CppCoreGuidelines focuses on providing best practices and guidelines for writing modern C++ code, while Abseil offers a collection of concrete, production-ready C++ library components. CppCoreGuidelines is more about coding standards and practices, whereas Abseil provides actual implementations of utilities and data structures. Developers may find value in using both resources: CppCoreGuidelines for overall code quality and style, and Abseil for efficient, well-tested library components.

Style guides for Google-originated open-source projects

Pros of Google Style Guide

  • Covers multiple languages (C++, Python, Java, etc.)
  • More concise and easier to digest for beginners
  • Includes specific formatting rules and examples

Cons of Google Style Guide

  • Less comprehensive than CppCoreGuidelines
  • May not reflect the latest C++ standards and best practices
  • More opinionated and less flexible in some areas

Code Comparison

CppCoreGuidelines:

void f(int* p)
{
    // Expects: p != nullptr
    // ...
    if (p == nullptr) throw std::runtime_error("null pointer");
    // ...
}

Google Style Guide:

void F(int* p) {
  // ...
  if (p == nullptr) {
    LOG(FATAL) << "Unexpected null pointer";
  }
  // ...
}

The CppCoreGuidelines example emphasizes the use of contracts and exceptions, while the Google Style Guide example focuses on consistent formatting and logging practices. Both aim to improve code quality and readability, but with different approaches and priorities.

Collaborative Collection of C++ Best Practices. This online resource is part of Jason Turner's collection of C++ Best Practices resources. See README.md for more information.

Pros of cppbestpractices

  • More concise and easier to digest for beginners
  • Covers a broader range of topics, including build systems and tooling
  • Regularly updated with community contributions

Cons of cppbestpractices

  • Less comprehensive and detailed than CppCoreGuidelines
  • Lacks the backing of the ISO C++ committee
  • May not always reflect the most up-to-date best practices

Code Comparison

CppCoreGuidelines:

void f(int* p)
{
    // ...
    delete p;
}

cppbestpractices:

void f(std::unique_ptr<int> p)
{
    // ...
}

The CppCoreGuidelines example shows manual memory management, while cppbestpractices promotes the use of smart pointers for better resource management.

Both repositories aim to improve C++ coding practices, but they differ in scope and depth. CppCoreGuidelines provides a more comprehensive and authoritative set of guidelines, while cppbestpractices offers a more accessible and broader overview of C++ best practices, including tooling and ecosystem considerations.

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.

Pros of llvm-project

  • Comprehensive toolchain: Includes compiler, debugger, and other development tools
  • Active development: Frequent updates and contributions from a large community
  • Wide language support: Handles multiple programming languages beyond C++

Cons of llvm-project

  • Steeper learning curve: More complex due to its extensive feature set
  • Larger codebase: Requires more resources to build and maintain
  • Less focused on C++ best practices: Covers broader aspects of compiler technology

Code comparison

CppCoreGuidelines:

void f(int* p)
{
    // Guideline: Don't use raw pointers for ownership
    // Use std::unique_ptr instead
}

llvm-project:

Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
                 const Twine &Name = "") {
  return Insert(GetElementPtrInst::Create(nullptr, Ptr, IdxList), Name);
}

The CppCoreGuidelines example demonstrates a best practice for C++ programming, while the llvm-project code shows a low-level operation used in compiler implementation.

CppCoreGuidelines focuses on providing guidelines for writing safe, efficient, and maintainable C++ code. In contrast, llvm-project is a comprehensive toolchain that includes the actual implementation of compilers and related tools, dealing with lower-level operations and supporting multiple programming languages.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

C++ Core Guidelines

"Within C++ is a smaller, simpler, safer language struggling to get out." -- Bjarne Stroustrup

The C++ Core Guidelines are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations. Their design encourages general applicability and broad adoption but they can be freely copied and modified to meet your organization's needs.

Getting started

The guidelines themselves are found at CppCoreGuidelines. The document is in GH-flavored MarkDown. It is intentionally kept simple, mostly in ASCII, to allow automatic post-processing such as language translation and reformatting. The editors maintain one version formatted for browsing. Note that it is manually integrated and can be slightly older than the version in the master branch.

The Guidelines are a constantly evolving document without a strict "release" cadence. Bjarne Stroustrup periodically reviews the document and increments the version number in the introduction. Checkins that increment the version number are tagged in git.

Many of the guidelines make use of the header-only Guidelines Support Library. One implementation is available at GSL: Guidelines Support Library.

Background and scope

The aim of the guidelines is to help people to use modern C++ effectively. By "modern C++" we mean C++11 and newer. In other words, what would you like your code to look like in 5 years' time, given that you can start now? In 10 years' time?

The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management, and concurrency. Such rules affect application architecture and library design. Following the rules will lead to code that is statically type-safe, has no resource leaks, and catches many more programming logic errors than is common in code today. And it will run fast -- you can afford to do things right.

We are less concerned with low-level issues, such as naming conventions and indentation style. However, no topic that can help a programmer is out of bounds.

Our initial set of rules emphasizes safety (of various forms) and simplicity. They may very well be too strict. We expect to have to introduce more exceptions to better accommodate real-world needs. We also need more rules.

You will find some of the rules contrary to your expectations or even contrary to your experience. If we haven't suggested that you change your coding style in any way, we have failed! Please try to verify or disprove rules! In particular, we'd really like to have some of our rules backed up with measurements or better examples.

You will find some of the rules obvious or even trivial. Please remember that one purpose of a guideline is to help someone who is less experienced or coming from a different background or language to get up to speed.

The rules are designed to be supported by an analysis tool. Violations of rules will be flagged with references (or links) to the relevant rule. We do not expect you to memorize all the rules before trying to write code.

The rules are meant for gradual introduction into a code base. We plan to build tools for that and hope others will too.

Contributions and LICENSE

Comments and suggestions for improvements are most welcome. We plan to modify and extend this document as our understanding improves and the language and the set of available libraries improve. More details are found at CONTRIBUTING and LICENSE.

Thanks to DigitalOcean for hosting the Standard C++ Foundation website.