CERN Accelerating science

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.
  • 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


Prefer auto to explicit type declarations.

Type Deduction

auto-typed variables can be subject of pitfalls.

Modern Coding Style

Braced Initialization

Distinguish between () and {} when creating objects.

range-based for loop

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


Prefer nullptr to 0 and NULL.

Alias Declarations

Prefer alias declarations to typedefs.


Use constexpr whenever possible.

Scoped enums

Prefer scoped enums to unscoped enums.

Deleted Functions

Prefer deleted functions to private undefined ones.

Overriding Functions

Declare overriding functions override or final.

Explicit Constructors

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

Delegating and inheriting constructors

Use delegating and inheriting constructors when they reduce code duplication.

Special member functions generation

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.


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


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


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

make Functions

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

SL and Lambda Expressions

Lambda Expressions

Understand lambda expressions.


Prefer algorithm calls to handwritten loops.

Default capture modes

Avoid default capture modes in lambda expressions.

Hashed containers

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


Threading support


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