Geant4 C++11 Features Guidelines

Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: . You may toggle all summaries with the big arrow button:

Toggle all summaries

About This Document

This document attemps to define the guidelines for use of new C++11 features relevant to Geant4 code development. They were compiled by

  • Ivana Hřivnáčová; Institut de Physique Nucleaire (IPNO), Universite Paris-Sud, CNRS- IN2P3, Orsay, France
    on behalf of Geant4 C++11 Task Force group

from the following sources:

  • Effective Modern C++ by Scot Meyers (O'Reilly). Copyright 2015 Scot Meyers. 978-1-491-90399-9.
  • ALICE O² C++ Style Guide.
  • cplusplus.com
  • Stack Overflow
  • It is using style sheets of C++ Google Style guide, Revision 3.274 (link) under the CC-By 3.0 License further modified by ALICE O² project.
  • See also tutorial slides on C++11 guidelines.

To help understanding the examples, the following colour highlighting is applied:

  • The recommended code, using C++11 features, is presented in black colour on light green background (or in a green frame if PDF).
    // code using C++11 features
  • The code using C++98 features, which can be improved with use of C++11 features, is presented in blue colour on light green background ((or in a green frame if PDF).
    // code using C++98 features
    // which can be improved with use of C++11 features
  • The code, which is discouraged as it can be improved with C++11 features, is presented in red colour on light red background (or in a red frame if PDF).
    // code which is error-prone or wrong 
    // and which can be avoided using C++11 features

Deducing Types and auto

auto

link
Prefer auto to explicit type declarations.

Type Deduction

link
auto-typed variables can be subject of pitfalls.

Modern Coding Style

Braced Initialization

link
Distinguish between () and {} when creating objects.

range-based for loop

link
Prefer range-based for loop when iterating over all elements in a container or a braced initilizer list.

nullptr

link
Prefer nullptr to 0 and NULL.

Alias Declarations

link
Prefer alias declarations to typedefs.

constexpr

link
Use constexpr whenever possible.

Scoped enums

link
Prefer scoped enums to unscoped enums.

Deleted Functions

link
Prefer deleted functions to private undefined ones.

Overriding Functions

link
Declare overriding functions override or final.

Explicit Constructors

link
Declare constructors with one argument explicit, except for copy constructors and constructors with std::initializer_list.

Delegating and inheriting constructors

link
Use delegating and inheriting constructors when they reduce code duplication.

Special member functions generation

link
Understand special member functions generation.

Smart Pointers

Smart pointers are objects that act like pointers, but automate ownership. There are two main semantics for ownership: unique and shared ownership.

Unique ownership ensures that there can be only one smart pointer to the object. If that smart pointer goes out of scope it will free the pointee.

Shared ownership allows to have multiple pointers to an object without deciding who is the exclusive owner. Thus the owners can be free'd in any order and the pointer will stay valid until the last one is freed, in which case the pointee is also freed.

Smart pointers are extremely useful for preventing memory leaks, and are essential for writing exception-safe code. They also formalize and document the ownership of dynamically allocated memory.

std::unique_ptr

link
Use std::unique_ptr for exclusive-ownership resource management.

std::shared_ptr

link
Use std::shared_ptr for shared-ownership resource management.

std::weak_ptr

link
Use std::weak_ptr for std::shared_ptr-like pointers that can dangle.

make Functions

link
Prefer std::make_unique(*) for std::make_shared to direct use of new.

SL and Lambda Expressions

Lambda Expressions

link
Understand lambda expressions.

Algorithms

link
Prefer algorithm calls to handwritten loops.

Default capture modes

link
Avoid default capture modes in lambda expressions.

Hashed containers

link
Since C++11 the standard library provides unordered containers in headers <unordered_set>, <unordered_multiset>, <unordered_map> and <unordered_multimap>.

Concurrency

Threading support

link

C++11 threading libraries should be used through the Geant4 interface and not directly.