SlideShare a Scribd company logo
The C++ Core
Guidelines for
Safer Code
Rainer Grimm
Training, Coaching and,
Technology Consulting
www.ModernesCpp.de
Guidelines
Best Practices for the Usage of C++
 Why do we need guidelines?
 C++ is a complex language in a complex domain.
 A new C++ standard is published all three years.
 C++ is used in safety-critical systems.
Reflect your coding habits.
Most Prominent Guidelines
 MISRA C++
 Motor Industry Software Reliability Association
 Based on MISRA C
 Industry standard in automotive, avionic, and medicine domain
 Published 2008 C++03
 AUTOSAR C++14
 Based on C++14
 More and more used in automotive domain (BMW)
 C++ Core Guidelines
 Community driven
Overview
 Philosophy
 Interfaces
 Functions
 Classes and class hierarchies
 Enumerations
 Resource management
 Expressions and statements
 Error handling
 Constants and immutability
 Templates and generic programming
 Concurrency
 The standard library
 Guideline support library
Syntactic Form
 About 350 rules and a few hundred pages
 Each rule follows a similar structure
 The rule itself
 A rule reference number
 Reason(s)
 Example(s)
 Alternative(s)
 Exception(s)
 Enforcement
 See also(s)
 Note(s)
 Discussion
Guidelines Support Library (GSL)
A small library for supporting the guidelines of the C++ core
guidelines.
 Implementations are available for
 Windows, Clang, and GCC
 GSL-lite works with C++98, and C++03
 Components
 Views
 Owner
 Assertions
 Utilities
 Concepts
Interfaces
I.11: Never transfer ownership by a raw pointer (T*)
 func(value)
 func has an independent copy of value and the runtime is the owner
 func(pointer*)
 pointer is borrowed but can be zero
 func is not the owner and must not delete the pointer
 func(reference&)
 reference is borrowed but can’t be zero
 func is not the owner and must not delete the reference
 func(std::unique_ptr)
 std::unique_ptr is the owner of the pointer
 func(std::shared_ptr)
 std::shared_ptr is an additional owner of the pointer
 std::shared_ptr extends the lifetime of the pointer
Interfaces
I.13: Do not pass an array as a single pointer
 What if n is wrong?
 Use span from the GSL
Functions
F.43: Never (directly or indirectly) return a pointer or a
reference to a local object
Classes
C.2: Use class if the class has an invariant; use struct if
the data members can vary independently
 The data members can vary independently
 The data members has an invariant
Classes
C.20: If you can avoid defining any default operations, do
C.21: If you define or =delete any default operation,
define or =delete them all
Sticky Bits - Becoming a Rule of Zero Hero
Enum
Enum.3: Prefer enum classes over “plain” enums
 Can only be accessed in the scope of the enumeration.
 Don't implicitly convert to int.
 Don't pollute the global namespace.
 The default type is int, but you can adjust it.
Resource Management
R.1: Manage resources automatically using resource
handles and RAII (Resource Acquisition Is Initialization)
 RAII-Idiom (Resource Acquisition Is Initialization)
 The lifetime of a resource is bound to an automatic object.
 The resource will be initialized in the constructor of the object;
released in the destructor of the object.
 Used
 Containers of the Standard Template Library and std::string
 Smart pointers
 Locks
 std::jthread (C++20)
Resource Management
Expressions and Statements
ES.28: Use lambdas for complex initialization,
especially of const variables
but widget x should be const
Expressions and Statements
ES.100: Don’t mix signed and unsigned arithmetic
mixed arithmetic with GCC, Clang, and MSVC
Concurrency and Parallelism
CP.8: Don’t try to use volatile for synchronization
 std::atomic
 Atomic (thread-safe) access to shared state.
 volatile
 Access to special memory, for which read and write optimisations
are not allowed.
Java volatile C++ atomic== C++ volatile!=
Concurrency and Parallelism
CP.9: Whenever feasible use tools to validate your
concurrent code
Thread Sanitizer detects data races at runtime.
g++ threadArguments.cpp -fsanitize=thread -g –o threadArguments
skip
Concurrency and Parallelism
Error Handling
E.7: State your preconditions
E.8: State your postconditions
 Precondition: should hold upon entry in a function.
 Postcondition: should hold upon exit from the function
 Assertion: should hold at its point in the computation.
Constants and Immutability
Con.2: By default, make member functions const
The method read should be const!
Constants and Immutability
 Physical constness:
 The object is const and can not be changed.
 Logical constness:
 The object is const but could be changed.
Templates and Generic Programming
 Usage  Definition
T.10: Specify concepts for all template arguments
 Concepts are a compile-time predicate.
Templates and Generic Programming
 Core language concepts
 Same
 DerivedFrom
 ConvertibleTo
 Common
 Integral
 Signed Integral
 Unsigned Integral
 Assignable
 Swappable
 Comparison concepts
 Boolean
 EqualityComparable
 StrictTotallyOrdered
 Object concepts
 Destructible
 Constructible
 DefaultConstructible
 MoveConstructible
 Copy Constructible
 Movable
 Copyable
 Semiregular
 Regular
 Callable concepts
 Callable
 RegularCallable
 Predicate
 Relation
 StrictWeakOrder
Templates and Generic Programming

More Related Content

PPTX
Summary of effective modern c++ item1 2
PPTX
Java generics(Under The Hood Of The Compiler) by Harmeet singh
PPT
Exceptions
PPTX
FUNDAMENTAL OF C
PDF
Big Brother helps you
ODT
(4) cpp automatic arrays_pointers_c-strings_exercises
PPT
Chap04
PDF
Notes part 8
Summary of effective modern c++ item1 2
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Exceptions
FUNDAMENTAL OF C
Big Brother helps you
(4) cpp automatic arrays_pointers_c-strings_exercises
Chap04
Notes part 8

What's hot (17)

PDF
Objective-C to Swift - Swift Cloud Workshop 3
PDF
Flag Waiving
PPTX
Cpu-fundamental of C
PPT
Basic of c &c++
PPTX
PDF
Lesson 9. Pattern 1. Magic numbers
PPTX
Constants and variables in c programming
PDF
Functions
PDF
Diving into monads in cats library
PPT
Token and operators
DOCX
PDF
12 computer science_notes_ch01_overview_of_cpp
PDF
Pattern Matching using Computational and Automata Theory
PPT
Csharp4 operators and_casts
PPTX
Parametricity
PPTX
Java New Programming Features
PPT
Getting started with c++
Objective-C to Swift - Swift Cloud Workshop 3
Flag Waiving
Cpu-fundamental of C
Basic of c &c++
Lesson 9. Pattern 1. Magic numbers
Constants and variables in c programming
Functions
Diving into monads in cats library
Token and operators
12 computer science_notes_ch01_overview_of_cpp
Pattern Matching using Computational and Automata Theory
Csharp4 operators and_casts
Parametricity
Java New Programming Features
Getting started with c++
Ad

Similar to The c++coreguidelinesforsavercode (20)

PPT
CSharp_03_ClassesStructs_and_introduction
PPTX
Introduction to c_plus_plus (6)
PPTX
Introduction to c_plus_plus
PPTX
Modern C++
PPT
Glimpses of C++0x
DOCX
unit 5.docx...............................
PPT
5 introduction-to-c
PDF
Cyclcone a safe dialect of C
PDF
C++ Interview Questions and Answers PDF By ScholarHat
PDF
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
PPTX
Gude for C++11 in Apache Traffic Server
PPTX
CSharp Presentation
DOCX
GSP 125 Entire Course NEW
PPTX
C traps and pitfalls for C++ programmers
PPTX
Team-7 SP.pptxdfghjksdfgduytredfghjkjhgffghj
PPTX
C Language (All Concept)
PDF
Bound and Checked
PPTX
1. Introduction to C# Programming Langua
PPTX
C++ language
CSharp_03_ClassesStructs_and_introduction
Introduction to c_plus_plus (6)
Introduction to c_plus_plus
Modern C++
Glimpses of C++0x
unit 5.docx...............................
5 introduction-to-c
Cyclcone a safe dialect of C
C++ Interview Questions and Answers PDF By ScholarHat
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Gude for C++11 in Apache Traffic Server
CSharp Presentation
GSP 125 Entire Course NEW
C traps and pitfalls for C++ programmers
Team-7 SP.pptxdfghjksdfgduytredfghjkjhgffghj
C Language (All Concept)
Bound and Checked
1. Introduction to C# Programming Langua
C++ language
Ad

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administration Chapter 2
PPTX
history of c programming in notes for students .pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Understanding Forklifts - TECH EHS Solution
PPT
Introduction Database Management System for Course Database
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
medical staffing services at VALiNTRY
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction to Artificial Intelligence
System and Network Administration Chapter 2
history of c programming in notes for students .pptx
Nekopoi APK 2025 free lastest update
2025 Textile ERP Trends: SAP, Odoo & Oracle
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Odoo POS Development Services by CandidRoot Solutions
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
medical staffing services at VALiNTRY
ISO 45001 Occupational Health and Safety Management System
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

The c++coreguidelinesforsavercode

  • 1. The C++ Core Guidelines for Safer Code Rainer Grimm Training, Coaching and, Technology Consulting www.ModernesCpp.de
  • 2. Guidelines Best Practices for the Usage of C++  Why do we need guidelines?  C++ is a complex language in a complex domain.  A new C++ standard is published all three years.  C++ is used in safety-critical systems. Reflect your coding habits.
  • 3. Most Prominent Guidelines  MISRA C++  Motor Industry Software Reliability Association  Based on MISRA C  Industry standard in automotive, avionic, and medicine domain  Published 2008 C++03  AUTOSAR C++14  Based on C++14  More and more used in automotive domain (BMW)  C++ Core Guidelines  Community driven
  • 4. Overview  Philosophy  Interfaces  Functions  Classes and class hierarchies  Enumerations  Resource management  Expressions and statements  Error handling  Constants and immutability  Templates and generic programming  Concurrency  The standard library  Guideline support library
  • 5. Syntactic Form  About 350 rules and a few hundred pages  Each rule follows a similar structure  The rule itself  A rule reference number  Reason(s)  Example(s)  Alternative(s)  Exception(s)  Enforcement  See also(s)  Note(s)  Discussion
  • 6. Guidelines Support Library (GSL) A small library for supporting the guidelines of the C++ core guidelines.  Implementations are available for  Windows, Clang, and GCC  GSL-lite works with C++98, and C++03  Components  Views  Owner  Assertions  Utilities  Concepts
  • 7. Interfaces I.11: Never transfer ownership by a raw pointer (T*)  func(value)  func has an independent copy of value and the runtime is the owner  func(pointer*)  pointer is borrowed but can be zero  func is not the owner and must not delete the pointer  func(reference&)  reference is borrowed but can’t be zero  func is not the owner and must not delete the reference  func(std::unique_ptr)  std::unique_ptr is the owner of the pointer  func(std::shared_ptr)  std::shared_ptr is an additional owner of the pointer  std::shared_ptr extends the lifetime of the pointer
  • 8. Interfaces I.13: Do not pass an array as a single pointer  What if n is wrong?  Use span from the GSL
  • 9. Functions F.43: Never (directly or indirectly) return a pointer or a reference to a local object
  • 10. Classes C.2: Use class if the class has an invariant; use struct if the data members can vary independently  The data members can vary independently  The data members has an invariant
  • 11. Classes C.20: If you can avoid defining any default operations, do C.21: If you define or =delete any default operation, define or =delete them all Sticky Bits - Becoming a Rule of Zero Hero
  • 12. Enum Enum.3: Prefer enum classes over “plain” enums  Can only be accessed in the scope of the enumeration.  Don't implicitly convert to int.  Don't pollute the global namespace.  The default type is int, but you can adjust it.
  • 13. Resource Management R.1: Manage resources automatically using resource handles and RAII (Resource Acquisition Is Initialization)  RAII-Idiom (Resource Acquisition Is Initialization)  The lifetime of a resource is bound to an automatic object.  The resource will be initialized in the constructor of the object; released in the destructor of the object.  Used  Containers of the Standard Template Library and std::string  Smart pointers  Locks  std::jthread (C++20)
  • 15. Expressions and Statements ES.28: Use lambdas for complex initialization, especially of const variables but widget x should be const
  • 16. Expressions and Statements ES.100: Don’t mix signed and unsigned arithmetic mixed arithmetic with GCC, Clang, and MSVC
  • 17. Concurrency and Parallelism CP.8: Don’t try to use volatile for synchronization  std::atomic  Atomic (thread-safe) access to shared state.  volatile  Access to special memory, for which read and write optimisations are not allowed. Java volatile C++ atomic== C++ volatile!=
  • 18. Concurrency and Parallelism CP.9: Whenever feasible use tools to validate your concurrent code Thread Sanitizer detects data races at runtime. g++ threadArguments.cpp -fsanitize=thread -g –o threadArguments skip
  • 20. Error Handling E.7: State your preconditions E.8: State your postconditions  Precondition: should hold upon entry in a function.  Postcondition: should hold upon exit from the function  Assertion: should hold at its point in the computation.
  • 21. Constants and Immutability Con.2: By default, make member functions const The method read should be const!
  • 22. Constants and Immutability  Physical constness:  The object is const and can not be changed.  Logical constness:  The object is const but could be changed.
  • 23. Templates and Generic Programming  Usage  Definition T.10: Specify concepts for all template arguments  Concepts are a compile-time predicate.
  • 24. Templates and Generic Programming  Core language concepts  Same  DerivedFrom  ConvertibleTo  Common  Integral  Signed Integral  Unsigned Integral  Assignable  Swappable  Comparison concepts  Boolean  EqualityComparable  StrictTotallyOrdered  Object concepts  Destructible  Constructible  DefaultConstructible  MoveConstructible  Copy Constructible  Movable  Copyable  Semiregular  Regular  Callable concepts  Callable  RegularCallable  Predicate  Relation  StrictWeakOrder
  • 25. Templates and Generic Programming