C++ Core Guidelines {#main}¶
Oct 3, 2024
Editors:
This is a living document under continuous improvement. Had it been an open-source (code) project, this would have been release 0.8. Copying, use, modification, and creation of derivative works from this project is licensed under an MIT-style license. Contributing to this project requires agreeing to a Contributor License. See the accompanying LICENSE file for details. We make this project available to "friendly users" to use, copy, modify, and derive from, hoping for constructive input.
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. When commenting, please note the introduction that outlines our aims and general approach. The list of contributors is here.
Problems:
- The sets of rules have not been completely checked for completeness, consistency, or enforceability.
- Triple question marks (???) mark known missing information
- Update reference sections; many pre-C++11 sources are too old.
- For a more-or-less up-to-date to-do list see: To-do: Unclassified proto-rules
You can read an explanation of the scope and structure of this Guide or just jump straight in:
- In: Introduction
- P: Philosophy
- I: Interfaces
- F: Functions
- C: Classes and class hierarchies
- Enum: Enumerations
- R: Resource management
- ES: Expressions and statements
- Per: Performance
- CP: Concurrency and parallelism
- E: Error handling
- Con: Constants and immutability
- T: Templates and generic programming
- CPL: C-style programming
- SF: Source files
- SL: The Standard Library
Supporting sections:
- A: Architectural ideas
- NR: Non-Rules and myths
- RF: References
- Pro: Profiles
- GSL: Guidelines support library
- NL: Naming and layout suggestions
- FAQ: Answers to frequently asked questions
- Appendix A: Libraries
- Appendix B: Modernizing code
- Appendix C: Discussion
- Appendix D: Supporting tools
- Glossary
- To-do: Unclassified proto-rules
You can sample rules for specific language features:
- assignment: regular types -- prefer initialization -- copy -- move -- other operations -- default
class
: data -- invariant -- members -- helpers -- concrete types -- ctors, =, and dtors -- hierarchy -- operatorsconcept
: rules -- in generic programming -- template arguments -- semantics- constructor:
invariant --
establish invariant --
throw
-- default -- not needed --explicit
-- delegating --virtual
- derived
class
: when to use -- as interface -- destructors -- copy -- getters and setters -- multiple inheritance -- overloading -- slicing --dynamic_cast
- destructor: and constructors -- when needed? -- must not fail
- exception:
errors --
throw
-- for errors only --noexcept
-- minimizetry
-- what if no exceptions? for
: range-for and for -- for and while -- for-initializer -- empty body -- loop variable -- loop variable type ???- function: naming -- single operation -- no throw -- arguments -- argument passing -- multiple return values -- pointers -- lambdas
inline
: small functions -- in headers- initialization:
always --
prefer
{}
-- lambdas -- default member initializers -- class members -- factory functions - lambda expression: when to use
- operator: conventional -- avoid conversion operators -- and lambdas
public
,private
, andprotected
: information hiding -- consistency --protected
static_assert
: compile-time checking -- and conceptsstruct
: for organizing data -- use if no invariant -- no private memberstemplate
: abstraction -- containers -- conceptsunsigned
: and signed -- bit manipulationvirtual
: interfaces -- notvirtual
-- destructor -- never fail
You can look at design concepts used to express the rules:
- assertion: ???
- error: ???
- exception: exception guarantee (???)
- failure: ???
- invariant: ???
- leak: ???
- library: ???
- precondition: ???
- postcondition: ???
- resource: ???