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: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
Modern Coding Style
range-based for loop
for loop
when iterating over all elements in a container or a braced initilizer list.Explicit Constructors
explicit
, except for copy constructors and constructors with std::initializer_list
.Delegating and inheriting constructors
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.
SL and Lambda Expressions
Hashed containers
<unordered_set>
, <unordered_multiset>
, <unordered_map>
and <unordered_multimap>
.Concurrency
Threading support
C++11 threading libraries should be used through the Geant4 interface and not directly.