Advanced Programming Techniques (Amazing C++) Bartosz Mindur Applied Physics and Computer Science Summer School '20
Kraków, 2020-07-16
[1 / 37] www.menti.com/28f8u5o5xr Which programming language do you use the most?
cpp h s a
b python a
c v a y j
csharp l b m e s s a
18
[2 / 37] Agenda References C++ history C++11/14 - must be used C++17 - should be used C++20 - may (eventually) be used ...
[3 / 37] www.menti.com/uaw75janh7 What is your current knowledge of C++?
Core C++ 3.1
C++ STL ! e k e 2.2 i l n
o d o N C++ 11/14 G 2.4
C++17 1.4
17
[4 / 37] References and tools
[5 / 37] C++ links and cool stu
C++ links Online tools
ISO C++ Compiler Explorer cpp references cpp insights c & cpp programming cpp.sh #include <C++> repl.it LernCpp.com Quick Bench cplusplus.com Online GDB CppCon piaza.io CppCast codiva.io Bartek Filipek ... O ine tools
CMake Valgrind GDB Docker Clang Static Analyzer [6 / 37] Things you (probably) already know well
[7 / 37] C++ basics variables conversion and casts references implicit pointers explicit functions static statements dynamic loops exceptions declaration function templates initialization class templates operator overloading smart pointers classes basics of the STL constructors & destructor containers fields iterators methods algorithms inheritance building programs types compilation virtual functions linking polymorphism libraries multiple inheritance tools
[8 / 37] www.menti.com/32rn4 y3j What is the most important C++ feature?
polymorphism templates encapsulation s r e t n i e inheritance o c p n a s
w hardware accessibility i e r e s g s
n multithreading i a l generic programming c
8
[9 / 37] C++ history
[10 / 37] The Design of C++
The Design of C++, a lecture by Bjarne Stroustrup This video has been recorded in March, 1994 [link]
The Design of C++ , lecture by Bjarne Stroustr… Do obejrze… Udostępnij
[11 / 37] C++ Timeline
[link]
[12 / 37] C++11/C++14
[13 / 37] Move semantics Value categories (simpli ed) Special members lvalue T::T(const T&& other) or T::T(T&& other) T& operator=(T&& other) e.g. named variable something which stands on the left of the operator= prvalue - pure rvalue
e.g. temporary variable (without name) something which stands on the right of the operator= xvalue - 'eXpiring' value
std::move(x) rvalue = prvalue + xvalue [Ref], [Ref], [Ref] and Code sample [14 / 37] Constant expressions - constexpr
The constexpr #include
Braced initialization list - {} Initialization with std::initializer_list int foo(){ int i{1}; int arr[] = {1, 2, 3, 4, 5}; template
[Ref], [Ref] and Code sample
[16 / 37] Type interface auto
For variables, speci es that the type of the variable that is being declared will be automatically deduced from its initializer
For functions, speci es that the return type will be deduced from its return statements [Ref]
#include
const auto c0 = a; // type of c0 is int, holding a copy of a
auto d = {1, 2}; // OK: type of d is std::initializer_list
// auto int x; // valid C++98, error as of C++11 // auto x; // valid C, error in C++ } [17 / 37] Range-based for loop
Executes a for loop over a range
#include
template
template
int main() { int main() { std::array
} }
Code sample #1 Code sample #2 and [Ref] [18 / 37] Explicit override and final virtual function speci er override
#include
Do not forget of virtual destructors [Ref], [Ref] and Code sample [Ref] and Code sample
[19 / 37] Lambdas
#include
[ captures ] ( params ) spec -> ret { body } int a = {5}; auto f2 = [=] () { std::cout << "simple lambda: " << a << "\n"; captures - a comma-separated list of zero or more }; f2(); captures with =, &, this params - a list of parameters, as in named auto f3 = [&] () { a = 17; functions std::cout << "simple lambda: " << a << "\n"; spec - optional sequence of specifiers (e.g. }; mutable) f3(); } ret - return type, if not present it's implied by the function return statements body - function body Code sample #1 and Code explanation, Code sample #2 [Ref] Code sample #3 and Code explanation [20 / 37] Template aliases and static assertions using static_assert
type alias is a name that refers to a previously performs compile-time assertion checking defined type (similar to typedef) no runtime costs alias template is a name that refers to a family of types int main() { using T1 = char; using T2 = int; template
[Ref] [Ref] and Code sample
[21 / 37] Variadic templates & parameter pack
Parameter pack Expand parameter pack
a template parameter pack is a template parameter #include
[22 / 37] Template template parameters (pre C++11 standard)
Sometimes we would like to pass into the template the names of template template parameters do not a template type without fixing its values have to be specified This is what template template parameters are created for template
[23 / 37] Compile-time decisions and type_traits
Compile-time 'if'' statement Possible implantation
#include
[24 / 37] Asynchronous execution the function template std::async runs given #include
[26 / 37] Structured binding declaration
Useful way to work with pair, tupe or set and map auto [a, b, c, ...] = expression;
std::pair a(0, 1.0f); struct Point { auto [x, y] = a; double x; double y; }; std::pair a(0, 1.0f); const auto [x, y] = a; Point GetStartPoint() { return { 0.0, 0.0 }; } std::tuple a {0, 1.0f, "string", 3.14, }; auto& [ refA, refB, refC, refD ] = a; const auto [x, y] = GetStartPoint();
Code sample #1, Code sample #2 and [Ref]
[27 / 37] Class template argument deduction
In order to instantiate a class template, every In C++17 deduction of template types is possible template argument must be known for classes in
any declaration that specifies initialization of a Before C++17 all templates arguments have to be variable speci ed directly, or using function templates - new expressions function-style casts expressions like unsigned(f) // direct usage std::tuple
Code sample and [Ref]
[28 / 37] Initialization statements for if and switch
In the init section you can specify a new variable, similarly to the init section in for loop if (init; condition) and switch (init; condition)
{ //C++ code before C++17 // Structured bindings + if initializer auto val = GetValue(); if (auto [iter, succeeded] = m.insert(value); if (condition(val)) succeeded) // on success { else use(iter); // ok // on false... // ... } } // iter and succeeded are destroyed here
if (auto val = GetValue(); condition(val)) // on success [Ref] and Code sample else // on false...
[29 / 37] Nested namespaces
Namespaces provide a method for preventing name con icts in large projects Nasted namespaces to be used in more convenient way of grouping namespace inside other namespace
// pre C++ 17 code // C++ 17 code namespace MyCompany { namespace MyCompany::SecretProject::SafetySystem { namespace SecretProject { class SuperArmor { namespace SafetySystem { // ... class SuperArmor { }; // ... class SuperShield { }; // ... class SuperShield { }; // ... } }; } // SafetySystem } // SecretProject } // MyCompany [Ref]
[30 / 37] __has_include preprocessor directive
__has_include
result of 1 only means that a file with the specified name exists it does not mean that the file, when included, would not cause an error etc. e.g. one could use experimental features or standard without changing the code
#if __has_include(
// rest of code
Code sample and [Ref] [31 / 37] Fold expressions
C++11 introduced variadic templates Variadic templates required some additional code when you wanted to implement 'recursive' functions - rule to stop it
// C++11 code // C++17 code auto Sum() { template
// C++17 code template
[32 / 37] Attributes
[[noreturn]] void terminate() noexcept {} Introduces in C++11 [[deprecated("use BetterFunc")]] void f() { } struct [[deprecated]] OldStruct { }; [[noreturn]] void switch_fun(char c) { switch (c) { case 'a': In C++14 added f(); // Warning! fallthrough case 'b': [[deprecated]] and [[deprecated("reason")]] terminate(); [[fallthrough]]; // Warning suppressed case 'c': terminate(); In C++17 added } } [[fallthrough]] - for switch void foo() { [[maybe_unused]] - OK if not used int x = 13; // warning [[maybe_unused]] int y = 13; // no warning [[nodiscard]] - return value shall no be omitted }
[[nodiscard]] inline int Compute() {return 1;} [Ref] and Code sample int main () { Compute(); // Warning! return value of a // nodiscard function is discarded } [33 / 37] C++20
[34 / 37] Not yet (fully) implemented Much to be learned (soon)
Concepts Coroutines Ranges Modules ...
[35 / 37] www.menti.com/317wc755y5 What is the most important C++ feature?
0
[36 / 37] Thank you for your time (any questions?)
[37 / 37]