The C++ Core Guidelines Project
Bjarne Stroustrup
Morgan Stanley, Columbia University www.stroustrup.com
The C++ Core Guidelines Project Bjarne Stroustrup Morgan Stanley, - - PowerPoint PPT Presentation
The C++ Core Guidelines Project Bjarne Stroustrup Morgan Stanley, Columbia University www.stroustrup.com The big question What is good modern C++? Many people want to write Modern C++ What would you like your code to look
Morgan Stanley, Columbia University www.stroustrup.com
C++ Core Guidelines - Stroustrup - OSSF'19 3
C++ Core Guidelines - Stroustrup - OSSF'19
Caveat: work in progress
4
C++ Core Guidelines - Stroustrup - OSSF'19 5
C++ Core Guidelines - Stroustrup - OSSF'19 6
C++ Core Guidelines - Stroustrup - OSSF'19 7
C++ Core Guidelines - Stroustrup - OSSF'19 8
How?
Stroustrup - Guidelines - CppCon'15 9
*Usual caveats †and thanks
Stroustrup - Guidelines - CppCon'15 10
teaching is a big problem
No littering - Stroustrup - Madrid - 2019 11
No littering - Stroustrup - Madrid - 2019 12
Stroustrup - Guidelines - CppCon'15 13
C++ GSL Don’t use STL Use:
C++ Core Guidelines - Stroustrup - OSSF'19 14
C++ Core Guidelines - Stroustrup - OSSF'19 15
C++ Core Guidelines - Stroustrup - OSSF'19 16
int a[100]; span s {a}; // note: template argument deduction for (auto x : s) // note: no range error, not nullptr check cout << x << '\n';
C++ Core Guidelines - Stroustrup - OSSF'19 17
Elements size
Stroustrup - Guidelines - CppCon'15 18
Stroustrup - Guidelines - CppCon'15 19
void f(X* p) { // … delete p; // looks innocent enough } void g() { X* q = new X; // looks innocent enough f(q); // … do a lot of work here … q->use(); // Ouch! Read/scramble random memory }
Stroustrup - Guidelines - CppCon'15 20
enclosing its owner’s scope
Stroustrup - Guidelines - CppCon'15 21
Stroustrup - Guidelines - CppCon'15 22
Object pointer pointer pointer Call stack Object
Stroustrup - Guidelines - CppCon'15 23
template<SemiRegular T> class vector {
// the anchors the allocated memory T* space; // just a position indicator T* end; // just a position indicator // … };
template<typename T> using owner = T;
Stroustrup - Guidelines - CppCon'15 24
vector<int*> // returning non-owner f(int* p) // return p would be OK { int x = 4; // return &x would be bad: local int* q = new int{7}; // return q would be bad: owner vector<int*> res = {p, &x, q}; return res; // Bad: { unknown, pointer to local, owner } }
Stroustrup - Guidelines - CppCon'15 25
Stroustrup - Guidelines - CppCon'15 26
Stroustrup - Guidelines - CppCon'15 27
Stroustrup - Guidelines - CppCon'15 28
Supporting sections
questions
C++ Core Guidelines - Stroustrup - OSSF'19 29
C++ Core Guidelines - Stroustrup - OSSF'19 30
C++ Core Guidelines - Stroustrup - OSSF'19 31
Parameter passing semantic rules:
Value return semantic rules:
C++ Core Guidelines - Stroustrup - OSSF'19 32