├── .clang-format ├── .github └── workflows │ ├── build.yml │ └── slides.yml ├── .gitignore ├── .gitpod.Dockerfile ├── .gitpod.yml ├── .gitpod └── .vscode │ └── settings.json ├── 01_01_introduction ├── CMakeLists.txt └── introduction.md ├── 01_02_infrastructure_resources ├── CMakeLists.txt ├── code │ └── cpp-example │ │ ├── .gitignore │ │ ├── CMakeLists.txt │ │ ├── MathFunctions │ │ ├── CMakeLists.txt │ │ ├── MathFunctions.cxx │ │ ├── MathFunctions.h │ │ └── test_MathFunctions.cxx │ │ ├── README.md │ │ ├── TutorialConfig.h.in │ │ ├── cmake │ │ └── ECMEnableSanitizers.cmake │ │ └── tutorial.cxx └── infrastructure_resources.md ├── 01_03_raii_move-semantics ├── CMakeLists.txt ├── attachments │ ├── bender-who-cares.jpg │ ├── business-spiderman-working-computer-office-1359497850d.jpg │ ├── c++-logo.png │ ├── cppref-shared_ptr.png │ ├── cppref-unique_ptr.png │ ├── five.png │ ├── pointers.jpg │ ├── shared_ptr-cost-heap.png │ ├── shared_ptr-ctors.png │ ├── shared_ptr-machinery.png │ ├── shared_ptr_cost-sync.png │ ├── smart-idea.jpeg │ ├── stone-wheel.png │ ├── tenor-1940754097.png │ ├── three.png │ ├── todolist.png │ ├── value-categories-00-l-vs-r.excalidraw │ ├── value-categories-00-l-vs-r.png │ ├── value-categories-01-l-vs-r-move.excalidraw │ ├── value-categories-01-l-vs-r-move.png │ ├── value-categories-02-move-semantic.excalidraw │ ├── value-categories-02-move-semantic.png │ ├── value-categories-03-rvalue.excalidraw │ ├── value-categories-03-rvalue.png │ ├── value-categories-04-general.excalidraw │ ├── value-categories-04-general.png │ ├── whoarewe-performances.png │ ├── zach-math-thinking.jpg │ └── zero.png ├── code │ └── raii_buffer.cpp └── raii_move-semantic.md ├── 01_04_generic_programming_1_templates ├── CMakeLists.txt ├── code │ ├── debug_print_specializations.cpp │ ├── demangle_helper.hpp │ ├── fold_print.cpp │ └── type_list.hpp ├── generic_programming_part1.md └── templates_part1.md ├── 02_01_functional_generic_utilities ├── CMakeLists.txt ├── code │ ├── apply.cpp │ ├── apply_tuple_bug.cpp │ ├── ast.cpp │ ├── empty_variant_optional.cpp │ ├── lambda_capture.cpp │ ├── tuple_storage.cpp │ └── tuple_storage_apply.cpp ├── functional_generic_utilities.md └── lambdas_cppref.png ├── 02_03_generic_programming_part2 ├── CMakeLists.txt ├── attachments │ ├── quiz-answer.jpg │ ├── quiz-question.jpg │ └── yoda.jpg ├── code │ ├── swap.cpp │ ├── swap_cpo.cpp │ └── swap_tag_invoke.cpp ├── generic_programming_part2_concepts.md └── generic_programming_part2_cpos.md ├── 02_05_example_concept_based_design ├── CMakeLists.txt ├── code │ ├── cursor.hpp │ ├── ex1_filter.cpp │ ├── ex2_to_vector.cpp │ ├── ex3_vector_as_cursor.cpp │ ├── ex4_cursor_algorithm_concept.cpp │ ├── sol1_filter.cpp │ ├── sol2_to_vector.cpp │ ├── sol3_vector_as_cursor.cpp │ ├── sol4_cursor_algorithm_concept.cpp │ └── test_cursor.cpp └── example_concept_based_design.md ├── 03_01_data_oriented_design ├── CMakeLists.txt ├── code │ ├── cache_block_size.cpp │ ├── cache_false_sharing.cpp │ ├── dram_block_size.cpp │ ├── dram_burst_mode.cpp │ └── nbody │ │ ├── CMakeLists.txt │ │ ├── common.hpp │ │ ├── dod.cpp │ │ ├── dod.hpp │ │ ├── main.cpp │ │ ├── oop.cpp │ │ └── oop.hpp ├── data_oriented_design.md ├── data_oriented_tips.md ├── examples │ ├── CMakeLists.txt │ └── struct_layout.cpp └── images │ ├── cache_block_size.svg │ ├── cache_no_prefetch_latency.drawio.svg │ ├── cache_prefetch_block_size_random.svg │ ├── cache_prefetch_block_size_sequential.svg │ ├── cache_prefetch_latency.drawio │ ├── cache_prefetch_latency.drawio.svg │ ├── data_access_patterns.drawio │ ├── dod_nbody_example.drawio.svg │ ├── dram_all_elements.drawio.svg │ ├── dram_block_reads.drawio.svg │ ├── dram_block_size_random.svg │ ├── dram_block_size_sequential.svg │ └── dram_every_8th.drawio.svg ├── 03_02_data_access_algorithms_ranges ├── CMakeLists.txt ├── code │ └── ranges_snippets.cpp └── containers_algorithms_ranges.md ├── 03_02b_mdspan ├── CMakeLists.txt ├── Row_and_column_major_order.png ├── code │ └── mdspan.cpp ├── md_span_implementation_status.png └── mdspan.md ├── 03_04_future ├── CMakeLists.txt ├── coroutines.md ├── future.md └── images │ └── coroutines.drawio ├── 0x_0x_coroutines ├── CMakeLists.txt ├── code │ └── task.cpp └── coroutines.md ├── CMakeLists.txt ├── README.md ├── cmake └── Marp.cmake ├── images └── repo_link_qr.gif ├── slides-support ├── README.md ├── common │ ├── 4k-slide-bg-white.png │ ├── 4k-slide-bg.png │ ├── footer.png │ ├── title-bg1.png │ ├── title-bg2.png │ └── title-bg3.png ├── docs │ ├── environment.png │ ├── preview.png │ └── settings.png └── themes │ └── cscs.css └── utility ├── .gitignore └── line_graph.py /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: '-4' 3 | AlignAfterOpenBracket: Align 4 | AlignConsecutiveAssignments: 'false' 5 | AlignConsecutiveBitFields: 'false' 6 | AlignConsecutiveDeclarations: 'false' 7 | AlignConsecutiveMacros: 'false' 8 | AlignEscapedNewlines: 'Left' 9 | AlignOperands: 'true' 10 | AlignTrailingComments: 'false' 11 | AllowAllParametersOfDeclarationOnNextLine: 'false' 12 | AllowShortBlocksOnASingleLine: 'Always' 13 | AllowShortCaseLabelsOnASingleLine: 'true' 14 | AllowShortFunctionsOnASingleLine: 'All' 15 | AllowShortIfStatementsOnASingleLine: 'true' 16 | AllowShortLambdasOnASingleLine: 'All' 17 | AllowShortLoopsOnASingleLine: 'true' 18 | AlwaysBreakAfterReturnType: 'None' 19 | AlwaysBreakBeforeMultilineStrings: 'true' 20 | AlwaysBreakTemplateDeclarations: 'Yes' 21 | BinPackArguments: 'true' 22 | BinPackParameters: 'true' 23 | BitFieldColonSpacing: 'Both' 24 | BraceWrapping: 25 | AfterCaseLabel: false 26 | AfterClass: false 27 | AfterControlStatement: 'MultiLine' 28 | AfterEnum: false 29 | AfterFunction: false 30 | AfterNamespace: false 31 | AfterStruct: false 32 | AfterUnion: false 33 | AfterExternBlock: true 34 | BeforeCatch: true 35 | BeforeElse: true 36 | BeforeLambdaBody: false 37 | BeforeWhile: false 38 | IndentBraces: false 39 | SplitEmptyFunction: false 40 | SplitEmptyRecord: false 41 | SplitEmptyNamespace: false 42 | BreakBeforeBinaryOperators: NonAssignment 43 | BreakBeforeBraces: Custom 44 | BreakBeforeTernaryOperators: 'true' 45 | BreakConstructorInitializers: 'BeforeColon' 46 | BreakInheritanceList: 'BeforeColon' 47 | BreakStringLiterals: 'true' 48 | ColumnLimit: '0' 49 | ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' 50 | ContinuationIndentWidth: '4' 51 | Cpp11BracedListStyle: 'false' 52 | DeriveLineEnding: 'true' 53 | FixNamespaceComments: true 54 | IncludeBlocks: Regroup 55 | IncludeCategories: 56 | - Regex: '<([[:alnum:].]|_)+>' 57 | Priority: 4 58 | - Regex: '(<|")((C|c)atch.*)/' 59 | Priority: 5 60 | - Regex: '.*' 61 | Priority: 1 62 | IndentCaseBlocks: 'false' 63 | IndentCaseLabels: 'true' 64 | IndentExternBlock: 'false' 65 | IndentGotoLabels: 'true' 66 | IndentPPDirectives: 'BeforeHash' 67 | IndentWidth: '4' 68 | IndentWrappedFunctionNames: 'false' 69 | KeepEmptyLinesAtTheStartOfBlocks: 'false' 70 | Language: Cpp 71 | MaxEmptyLinesToKeep: '3' 72 | NamespaceIndentation: Inner 73 | PointerAlignment: Left 74 | SortIncludes: 'true' 75 | SortUsingDeclarations: 'false' 76 | SpaceAfterCStyleCast: 'false' 77 | SpaceAfterLogicalNot: 'false' 78 | SpaceAfterTemplateKeyword: 'true' 79 | SpaceBeforeAssignmentOperators: 'true' 80 | SpaceBeforeCaseColon: 'false' 81 | SpaceBeforeCpp11BracedList: 'false' 82 | SpaceBeforeCtorInitializerColon: 'true' 83 | SpaceBeforeInheritanceColon: 'true' 84 | SpaceBeforeParens: ControlStatements 85 | SpaceBeforeRangeBasedForLoopColon: 'true' 86 | SpaceBeforeSquareBrackets: 'false' 87 | SpaceInEmptyBlock: 'false' 88 | SpaceInEmptyParentheses: 'false' 89 | SpacesBeforeTrailingComments: '1' 90 | SpacesInAngles: 'false' 91 | SpacesInCStyleCastParentheses: 'false' 92 | SpacesInConditionalStatement: 'false' 93 | SpacesInContainerLiterals: 'true' 94 | SpacesInParentheses: 'false' 95 | SpacesInSquareBrackets: 'false' 96 | Standard: c++20 97 | TabWidth: '4' 98 | UseCRLF: 'false' 99 | UseTab: Never 100 | ... 101 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - master 8 | pull_request: 9 | branches: 10 | - main 11 | - master 12 | 13 | jobs: 14 | build-test: 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | os: [windows-latest, ubuntu-latest] 19 | c_compiler: [gcc, clang, cl] 20 | build_type: [Debug, Release] 21 | cxx_standard: ["20"] 22 | include: 23 | - c_compiler: gcc 24 | cxx_compiler: g++ 25 | - c_compiler: clang 26 | cxx_compiler: clang++ 27 | - c_compiler: cl 28 | cxx_compiler: cl 29 | - os: windows-latest 30 | test_name: .\Bin\UnitTest.exe 31 | - os: ubuntu-latest 32 | test_name: ./Bin/UnitTest 33 | exclude: 34 | - os: windows-latest 35 | c_compiler: gcc 36 | - os: windows-latest 37 | c_compiler: clang 38 | - os: ubuntu-latest 39 | c_compiler: cl 40 | 41 | name: ${{ matrix.c_compiler }} - ${{ matrix.build_type }} - ${{ matrix.os }} - c++${{ matrix.cxx_standard }} 42 | 43 | runs-on: ${{matrix.os}} 44 | 45 | steps: 46 | - uses: actions/checkout@v3 47 | - uses: seanmiddleditch/gha-setup-ninja@v3 48 | - uses: seanmiddleditch/gha-setup-vsdevenv@master 49 | - name: Install LLVM and Clang 50 | uses: KyleMayes/install-llvm-action@v1 51 | with: 52 | version: "17.0" 53 | 54 | - name: Create Build Environment 55 | run: cmake -E make_directory ${{runner.workspace}}/build 56 | 57 | - name: Configure CMake 58 | working-directory: ${{runner.workspace}}/build 59 | env: 60 | CC: ${{matrix.c_compiler}} 61 | CXX: ${{matrix.cxx_compiler}} 62 | run: cmake -S ${{github.workspace}} -B ${{runner.workspace}}/build -G Ninja -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} -DCPP_COURSE_BUILD_SLIDES:BOOL=OFF 63 | 64 | - name: Build 65 | working-directory: ${{runner.workspace}}/build 66 | run: cmake --build . --config ${{matrix.build_type}} 67 | 68 | - name: CTest 69 | working-directory: ${{runner.workspace}}/build 70 | run: ctest 71 | -------------------------------------------------------------------------------- /.github/workflows/slides.yml: -------------------------------------------------------------------------------- 1 | name: Slides 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | pull_request: 9 | branches: 10 | - master 11 | - main 12 | 13 | jobs: 14 | slides: 15 | name: slides 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v2 19 | - uses: seanmiddleditch/gha-setup-ninja@v3 20 | 21 | - name: Install dependencies 22 | shell: bash 23 | run: | 24 | sudo apt update 25 | sudo apt install chromium-browser 26 | wget https://github.com/marp-team/marp-cli/releases/download/v3.2.1/marp-cli-v3.2.1-linux.tar.gz 27 | tar -xf ./marp-cli-v3.2.1-linux.tar.gz 28 | sudo ln -s $(pwd)/marp /usr/bin/marp 29 | 30 | - name: Create Build Environment 31 | run: cmake -E make_directory ${{runner.workspace}}/build 32 | 33 | - name: Configure CMake 34 | shell: bash 35 | working-directory: ${{runner.workspace}}/build 36 | run: | 37 | cmake -S $GITHUB_WORKSPACE -B . -DCMAKE_BUILD_TYPE=Debug 38 | 39 | - name: Build 40 | working-directory: ${{runner.workspace}}/build 41 | shell: bash 42 | run: cmake --build . --config Debug 43 | 44 | - name: Copy slides 45 | working-directory: ${{runner.workspace}}/build 46 | shell: bash 47 | run: | 48 | mkdir slides 49 | # For the moment we just PDF slides, because HTML have problem with images urls 50 | cp ./*.pdf ./slides/ 51 | 52 | - name: Archive slides 53 | uses: actions/upload-artifact@v3 54 | with: 55 | name: slides 56 | path: ${{runner.workspace}}/build/slides 57 | 58 | deploy: 59 | if: github.ref_name == 'main' 60 | needs: slides 61 | 62 | # Grant GITHUB_TOKEN the permissions required to make a Pages deployment 63 | permissions: 64 | pages: write # to deploy to Pages 65 | id-token: write # to verify the deployment originates from an appropriate source 66 | 67 | environment: 68 | name: github-pages 69 | url: ${{ steps.deployment.outputs.page_url }} 70 | 71 | runs-on: ubuntu-latest 72 | steps: 73 | - name: Download slides 74 | uses: actions/download-artifact@v3 75 | with: 76 | name: slides 77 | 78 | - name: Generate GitHub Pages index 79 | shell: bash 80 | run: | 81 | echo "

Slides from the CSCS Advanced C++ Course 2023

" >> index.html 82 | echo "" >> index.html 85 | 86 | - name: Publish artifact with slides to GitHub Pages 87 | uses: actions/upload-pages-artifact@v2 88 | with: 89 | path: . 90 | 91 | - name: Deploy to GitHub Pages 92 | id: deployment 93 | uses: actions/deploy-pages@v2 94 | with: 95 | artifact_name: 'github-pages' 96 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.html 3 | *.pdf 4 | build/ 5 | .cache/ 6 | /.vscode 7 | .venv/ 8 | compile_commands.json 9 | -------------------------------------------------------------------------------- /.gitpod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gitpod/workspace-full 2 | 3 | ENV CLANG_VERSION 17 4 | 5 | USER root 6 | 7 | RUN echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-${CLANG_VERSION} main" >> /etc/apt/sources.list && \ 8 | echo "deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-${CLANG_VERSION} main" >> /etc/apt/sources.list && \ 9 | wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc && \ 10 | apt-get update -qq && \ 11 | DEBIAN_FRONTEND=noninteractive apt-get install -qq -y clang-${CLANG_VERSION} libomp-${CLANG_VERSION}-dev clangd-${CLANG_VERSION} && \ 12 | rm -rf /var/lib/apt/lists/* 13 | 14 | USER gitpod 15 | 16 | RUN brew install marp-cli 17 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | 2 | image: 3 | file: .gitpod.Dockerfile 4 | 5 | tasks: 6 | - init: | 7 | ln -s /workspace/cpp-course-2023/.gitpod/.vscode /workspace/cpp-course-2023/.vscode 8 | cmake -S . -B $(pwd)/build -DCPP_COURSE_BUILD_SLIDES_PDF=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 9 | cmake --build $(pwd)/build 10 | ln -s $(pwd)/build/compile_commands.json $(pwd) 11 | 12 | vscode: 13 | extensions: 14 | - marp-team.marp-vscode 15 | -------------------------------------------------------------------------------- /.gitpod/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "markdown.marp.themes": ["./slides-support/themes/cscs.css"], 3 | "markdown.marp.enableHtml": true, 4 | "clangd.path": "clangd-17" 5 | } 6 | -------------------------------------------------------------------------------- /01_01_introduction/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if (${CPP_COURSE_BUILD_SLIDES}) 2 | add_marp_slides(01_01_introduction introduction.md) 3 | endif() -------------------------------------------------------------------------------- /01_01_introduction/introduction.md: -------------------------------------------------------------------------------- 1 | --- 2 | marp: true 3 | theme: cscs 4 | # class: lead 5 | paginate: true 6 | backgroundColor: #fff 7 | backgroundImage: url('../slides-support/common/4k-slide-bg-white.png') 8 | size: 16:9 9 | --- 10 | 11 | # **Marp** 12 | ![bg cover](../slides-support/common/title-bg3.png) 13 | 14 | 15 | 16 | 17 | ### Title topic 18 | 19 | #### Authors, Writers & Teachers 20 | 21 | --- 22 | 23 | # How to write slides 24 | Split pages by horizontal ruler (`---`). It's very simple! :satisfied: 25 | 26 | ```markdown 27 | # Slide 1 28 | 29 | foobar 30 | 31 | --- 32 | 33 | # Slide 2 34 | 35 | foobar 36 | ``` 37 | 38 | --- 39 | 40 | # A Slide with two columns 41 | 42 |
43 |
44 | 45 | ## Code 46 | 47 | ```c++ 48 | void f(int x) { 49 | } 50 | ``` 51 | 52 |
53 |
54 | 55 | ## Explanations 56 | 57 | declaration of a function f... 58 | 59 |
60 |
61 | 62 | --- 63 | 64 | # Testing Math 65 | 66 | Writing an expression here: $ax^2+bx+c$ 67 | 68 | $$ I_{xx}=\int\int_Ry^2f(x,y)\cdot{}dydx $$ 69 | 70 | $$ 71 | f(x) = \int_{-\infty}^\infty 72 | \hat f(\xi)\,e^{2 \pi i \xi x} 73 | \,d\xi 74 | $$ 75 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if (${CPP_COURSE_BUILD_SLIDES}) 2 | add_marp_slides(01_02_infrastructure_resources infrastructure_resources.md) 3 | endif() 4 | 5 | add_subdirectory(code/cpp-example) 6 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.cache 3 | /build 4 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # TODO 1: Update the minimum required version to 3.15 2 | 3 | cmake_minimum_required(VERSION 3.10) 4 | 5 | # set the project name and version 6 | project(Tutorial VERSION 1.0) 7 | 8 | # specify the C++ standard 9 | add_library(tutorial_compiler_flags INTERFACE) 10 | target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_17) 11 | 12 | set(gcc_like_cxx "$") 13 | set(msvc_cxx "$") 14 | target_compile_options(tutorial_compiler_flags INTERFACE 15 | "$<${gcc_like_cxx}:$>" 16 | "$<${msvc_cxx}:$>" 17 | ) 18 | 19 | # configure a header file to pass some of the CMake settings 20 | # to the source code 21 | configure_file(TutorialConfig.h.in TutorialConfig.h) 22 | 23 | # add the MathFunctions library 24 | add_subdirectory(MathFunctions) 25 | 26 | # add the executable 27 | add_executable(Tutorial tutorial.cxx) 28 | 29 | target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags) 30 | 31 | # add the binary tree to the search path for include files 32 | # so that we will find TutorialConfig.h 33 | target_include_directories(Tutorial PUBLIC 34 | "${PROJECT_BINARY_DIR}" 35 | ) 36 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/MathFunctions/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | Include(FetchContent) 2 | 3 | FetchContent_Declare( 4 | Catch2 5 | GIT_REPOSITORY https://github.com/catchorg/Catch2.git 6 | GIT_TAG v3.4.0 # or a later release 7 | ) 8 | 9 | FetchContent_MakeAvailable(Catch2) 10 | 11 | # create the MathFunctions library 12 | add_library(MathFunctions MathFunctions.cxx) 13 | 14 | # state that anybody linking to us needs to include the current source dir 15 | # to find MathFunctions.h, while we don't. 16 | target_include_directories(MathFunctions 17 | INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} 18 | ) 19 | 20 | # link our compiler flags interface library 21 | target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) 22 | 23 | add_executable(testmathfunctions test_MathFunctions.cxx) 24 | target_link_libraries(testmathfunctions PUBLIC MathFunctions tutorial_compiler_flags) 25 | target_link_libraries(testmathfunctions PRIVATE Catch2::Catch2WithMain) 26 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/MathFunctions/MathFunctions.cxx: -------------------------------------------------------------------------------- 1 | #include "MathFunctions.h" 2 | 3 | #include 4 | 5 | namespace mathfunctions { 6 | double sqrt(double x) { return std::sqrt(x); } 7 | 8 | unsigned int Factorial(unsigned int number) { 9 | return number <= 1 ? number : Factorial(number - 1) * number; 10 | } 11 | 12 | } // namespace mathfunctions 13 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/MathFunctions/MathFunctions.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mathfunctions { 4 | double sqrt(double x); 5 | unsigned int Factorial( unsigned int number ); 6 | } 7 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/MathFunctions/test_MathFunctions.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MathFunctions.h" 3 | 4 | using mathfunctions::Factorial; 5 | 6 | TEST_CASE( "Factorials are computed", "[factorial]" ) { 7 | REQUIRE( Factorial(1) == 1 ); 8 | REQUIRE( Factorial(2) == 2 ); 9 | REQUIRE( Factorial(3) == 6 ); 10 | REQUIRE( Factorial(10) == 3628800 ); 11 | } -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/README.md: -------------------------------------------------------------------------------- 1 | # Sample project with tests using Catch2 2 | 3 | Setup with: 4 | ```bash 5 | cmake -S . -B build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=1 6 | if [ ! -e compile_commands.json ]; then 7 | ln -s build/compile_commands.json . 8 | fi 9 | ``` 10 | run with 11 | ```bash 12 | cmake --build build 13 | ``` 14 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/TutorialConfig.h.in: -------------------------------------------------------------------------------- 1 | // the configured options and settings for Tutorial 2 | #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ 3 | #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ 4 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/cmake/ECMEnableSanitizers.cmake: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2014 Mathieu Tarral 2 | # 3 | # SPDX-License-Identifier: BSD-3-Clause 4 | 5 | #[=======================================================================[.rst: 6 | ECMEnableSanitizers 7 | ------------------- 8 | 9 | Enable compiler sanitizer flags. 10 | 11 | The following sanitizers are supported: 12 | 13 | - Address Sanitizer 14 | - Memory Sanitizer 15 | - Thread Sanitizer 16 | - Leak Sanitizer 17 | - Undefined Behaviour Sanitizer 18 | 19 | All of them are implemented in Clang, depending on your version, and 20 | there is an work in progress in GCC, where some of them are currently 21 | implemented. 22 | 23 | This module will check your current compiler version to see if it 24 | supports the sanitizers that you want to enable 25 | 26 | Usage 27 | ===== 28 | 29 | Simply add:: 30 | 31 | include(ECMEnableSanitizers) 32 | 33 | to your ``CMakeLists.txt``. Note that this module is included in 34 | :kde-module:`KDECompilerSettings`, so projects using that module do not need to also 35 | include this one. 36 | 37 | The sanitizers are not enabled by default. Instead, you must set 38 | ``ECM_ENABLE_SANITIZERS`` (either in your ``CMakeLists.txt`` or on the 39 | command line) to a semicolon-separated list of sanitizers you wish to enable. 40 | The options are: 41 | 42 | - address 43 | - memory 44 | - thread 45 | - leak 46 | - undefined 47 | - fuzzer 48 | 49 | The sanitizers "address", "memory" and "thread" are mutually exclusive. You 50 | cannot enable two of them in the same build. 51 | 52 | "leak" requires the "address" sanitizer. 53 | 54 | .. note:: 55 | 56 | To reduce the overhead induced by the instrumentation of the sanitizers, it 57 | is advised to enable compiler optimizations (``-O1`` or higher). 58 | 59 | Example 60 | ======= 61 | 62 | This is an example of usage:: 63 | 64 | mkdir build 65 | cd build 66 | cmake -DECM_ENABLE_SANITIZERS='address;leak;undefined' .. 67 | 68 | .. note:: 69 | 70 | Most of the sanitizers will require Clang. To enable it, use:: 71 | 72 | -DCMAKE_CXX_COMPILER=clang++ 73 | 74 | Since 1.3.0. 75 | #]=======================================================================] 76 | 77 | # MACRO check_compiler_version 78 | #----------------------------- 79 | macro (check_compiler_version gcc_required_version clang_required_version msvc_required_version) 80 | if ( 81 | ( 82 | CMAKE_CXX_COMPILER_ID MATCHES "GNU" 83 | AND 84 | CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${gcc_required_version} 85 | ) 86 | OR 87 | ( 88 | CMAKE_CXX_COMPILER_ID MATCHES "Clang" 89 | AND 90 | CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${clang_required_version} 91 | ) 92 | OR 93 | ( 94 | CMAKE_CXX_COMPILER_ID MATCHES "MSVC" 95 | AND 96 | CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${msvc_required_version} 97 | ) 98 | ) 99 | # error ! 100 | message(FATAL_ERROR "You ask to enable the sanitizer ${CUR_SANITIZER}, 101 | but your compiler ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION} 102 | does not support it ! 103 | You should use at least GCC ${gcc_required_version}, Clang ${clang_required_version} 104 | or MSVC ${msvc_required_version} 105 | (99.99 means not implemented yet)") 106 | endif () 107 | endmacro () 108 | 109 | # MACRO check_compiler_support 110 | #------------------------------ 111 | macro (enable_sanitizer_flags sanitize_option) 112 | if (${sanitize_option} MATCHES "address") 113 | check_compiler_version("4.8" "3.1" "19.28") 114 | if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") 115 | set(XSAN_COMPILE_FLAGS "-fsanitize=address") 116 | else() 117 | set(XSAN_COMPILE_FLAGS "-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls") 118 | set(XSAN_LINKER_FLAGS "asan") 119 | endif() 120 | elseif (${sanitize_option} MATCHES "thread") 121 | check_compiler_version("4.8" "3.1" "99.99") 122 | set(XSAN_COMPILE_FLAGS "-fsanitize=thread") 123 | set(XSAN_LINKER_FLAGS "tsan") 124 | elseif (${sanitize_option} MATCHES "memory") 125 | check_compiler_version("99.99" "3.1" "99.99") 126 | set(XSAN_COMPILE_FLAGS "-fsanitize=memory") 127 | elseif (${sanitize_option} MATCHES "leak") 128 | check_compiler_version("4.9" "3.4" "99.99") 129 | set(XSAN_COMPILE_FLAGS "-fsanitize=leak") 130 | set(XSAN_LINKER_FLAGS "lsan") 131 | elseif (${sanitize_option} MATCHES "undefined") 132 | check_compiler_version("4.9" "3.1" "99.99") 133 | set(XSAN_COMPILE_FLAGS "-fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls") 134 | elseif (${sanitize_option} MATCHES "fuzzer") 135 | check_compiler_version("99.99" "6.0" "99.99") 136 | set(XSAN_COMPILE_FLAGS "-fsanitize=fuzzer") 137 | else () 138 | message(FATAL_ERROR "Compiler sanitizer option \"${sanitize_option}\" not supported.") 139 | endif () 140 | endmacro () 141 | 142 | if (ECM_ENABLE_SANITIZERS) 143 | if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC") 144 | # for each element of the ECM_ENABLE_SANITIZERS list 145 | foreach ( CUR_SANITIZER ${ECM_ENABLE_SANITIZERS} ) 146 | # lowercase filter 147 | string(TOLOWER ${CUR_SANITIZER} CUR_SANITIZER) 148 | # check option and enable appropriate flags 149 | enable_sanitizer_flags ( ${CUR_SANITIZER} ) 150 | # TODO: GCC will not link pthread library if enabled ASan 151 | if(CMAKE_C_COMPILER_ID MATCHES "Clang") 152 | set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XSAN_COMPILE_FLAGS}" ) 153 | endif() 154 | set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XSAN_COMPILE_FLAGS}" ) 155 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") 156 | link_libraries(${XSAN_LINKER_FLAGS}) 157 | endif() 158 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 159 | string(REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") 160 | string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") 161 | endif () 162 | endforeach() 163 | else() 164 | message(STATUS "Tried to enable sanitizers (-DECM_ENABLE_SANITIZERS=${ECM_ENABLE_SANITIZERS}), \ 165 | but compiler (${CMAKE_CXX_COMPILER_ID}) does not have sanitizer support") 166 | endif() 167 | endif() 168 | -------------------------------------------------------------------------------- /01_02_infrastructure_resources/code/cpp-example/tutorial.cxx: -------------------------------------------------------------------------------- 1 | // A simple program that computes the square root of a number 2 | #include 3 | #include 4 | #include 5 | 6 | #include "MathFunctions.h" 7 | #include "TutorialConfig.h" 8 | 9 | int main(int argc, char *argv[]) { 10 | if (argc < 2) { 11 | // report version 12 | std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." 13 | << Tutorial_VERSION_MINOR << std::endl; 14 | std::cout << "Usage: " << argv[0] << " number" << std::endl; 15 | return 1; 16 | } 17 | 18 | // convert input to double 19 | const double inputValue = std::stod(argv[1]); 20 | 21 | const double outputValue = mathfunctions::sqrt(inputValue); 22 | 23 | std::cout << "The square root of " << inputValue << " is " << outputValue 24 | << std::endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /01_03_raii_move-semantics/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if (${CPP_COURSE_BUILD_SLIDES}) 2 | add_marp_slides(01_03_raii_move-semantics raii_move-semantic.md) 3 | endif() 4 | 5 | add_executable(01_03_raii_buffer 6 | code/raii_buffer.cpp 7 | ) 8 | -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/bender-who-cares.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/bender-who-cares.jpg -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/business-spiderman-working-computer-office-1359497850d.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/business-spiderman-working-computer-office-1359497850d.jpg -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/c++-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/c++-logo.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/cppref-shared_ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/cppref-shared_ptr.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/cppref-unique_ptr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/cppref-unique_ptr.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/five.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/five.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/pointers.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/pointers.jpg -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/shared_ptr-cost-heap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/shared_ptr-cost-heap.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/shared_ptr-ctors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/shared_ptr-ctors.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/shared_ptr-machinery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/shared_ptr-machinery.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/shared_ptr_cost-sync.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/shared_ptr_cost-sync.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/smart-idea.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/smart-idea.jpeg -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/stone-wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/stone-wheel.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/tenor-1940754097.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/tenor-1940754097.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/three.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/three.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/todolist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/todolist.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-00-l-vs-r.excalidraw: -------------------------------------------------------------------------------- 1 | { 2 | "type": "excalidraw", 3 | "version": 2, 4 | "source": "https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor", 5 | "elements": [ 6 | { 7 | "type": "rectangle", 8 | "version": 123, 9 | "versionNonce": 764019558, 10 | "isDeleted": false, 11 | "id": "kVWoRRwZPwzHIAaXHC4cL", 12 | "fillStyle": "hachure", 13 | "strokeWidth": 1, 14 | "strokeStyle": "solid", 15 | "roughness": 1, 16 | "opacity": 100, 17 | "angle": 0, 18 | "x": 95.013671875, 19 | "y": 112.47265625, 20 | "strokeColor": "#1e1e1e", 21 | "backgroundColor": "transparent", 22 | "width": 160.4296875, 23 | "height": 160.4296875, 24 | "seed": 1686603066, 25 | "groupIds": [], 26 | "frameId": null, 27 | "roundness": { 28 | "type": 3 29 | }, 30 | "boundElements": [], 31 | "updated": 1696408200070, 32 | "link": null, 33 | "locked": false 34 | }, 35 | { 36 | "type": "rectangle", 37 | "version": 314, 38 | "versionNonce": 1203211322, 39 | "isDeleted": false, 40 | "id": "TzfntMWXffO0FfCxCcELg", 41 | "fillStyle": "hachure", 42 | "strokeWidth": 1, 43 | "strokeStyle": "solid", 44 | "roughness": 1, 45 | "opacity": 100, 46 | "angle": 0, 47 | "x": 283.22534482672495, 48 | "y": 114.02584911863863, 49 | "strokeColor": "#1e1e1e", 50 | "backgroundColor": "transparent", 51 | "width": 160.4296875, 52 | "height": 160.4296875, 53 | "seed": 1765736122, 54 | "groupIds": [], 55 | "frameId": null, 56 | "roundness": { 57 | "type": 3 58 | }, 59 | "boundElements": [], 60 | "updated": 1696408210516, 61 | "link": null, 62 | "locked": false 63 | }, 64 | { 65 | "type": "text", 66 | "version": 43, 67 | "versionNonce": 463495237, 68 | "isDeleted": false, 69 | "id": "0CKU4O39yaKU9Wd2j0Wew", 70 | "fillStyle": "hachure", 71 | "strokeWidth": 1, 72 | "strokeStyle": "solid", 73 | "roughness": 1, 74 | "opacity": 100, 75 | "angle": 0, 76 | "x": 121.548828125, 77 | "y": 72.5859375, 78 | "strokeColor": "#1e1e1e", 79 | "backgroundColor": "transparent", 80 | "width": 116.47988891601562, 81 | "height": 25, 82 | "seed": 220403578, 83 | "groupIds": [], 84 | "frameId": null, 85 | "roundness": null, 86 | "boundElements": [], 87 | "updated": 1696408290603, 88 | "link": null, 89 | "locked": false, 90 | "fontSize": 20, 91 | "fontFamily": 1, 92 | "text": "has identity", 93 | "textAlign": "left", 94 | "verticalAlign": "top", 95 | "containerId": null, 96 | "originalText": "has identity", 97 | "lineHeight": 1.25, 98 | "baseline": 18 99 | }, 100 | { 101 | "type": "text", 102 | "version": 190, 103 | "versionNonce": 893940363, 104 | "isDeleted": false, 105 | "id": "AFYwcRu6a7OzwbhbSsl1r", 106 | "fillStyle": "hachure", 107 | "strokeWidth": 1, 108 | "strokeStyle": "solid", 109 | "roughness": 1, 110 | "opacity": 100, 111 | "angle": 0, 112 | "x": 312.8518524169922, 113 | "y": 74.125, 114 | "strokeColor": "#1e1e1e", 115 | "backgroundColor": "transparent", 116 | "width": 102.81990051269531, 117 | "height": 25, 118 | "seed": 1988499514, 119 | "groupIds": [], 120 | "frameId": null, 121 | "roundness": null, 122 | "boundElements": [], 123 | "updated": 1696408290603, 124 | "link": null, 125 | "locked": false, 126 | "fontSize": 20, 127 | "fontFamily": 1, 128 | "text": "no identity", 129 | "textAlign": "left", 130 | "verticalAlign": "top", 131 | "containerId": null, 132 | "originalText": "no identity", 133 | "lineHeight": 1.25, 134 | "baseline": 18 135 | }, 136 | { 137 | "type": "text", 138 | "version": 12, 139 | "versionNonce": 2006323109, 140 | "isDeleted": false, 141 | "id": "rqYHzBxgSDar_bKxK448p", 142 | "fillStyle": "hachure", 143 | "strokeWidth": 1, 144 | "strokeStyle": "solid", 145 | "roughness": 1, 146 | "opacity": 100, 147 | "angle": 0, 148 | "x": 139.447265625, 149 | "y": 175.7265625, 150 | "strokeColor": "#1e1e1e", 151 | "backgroundColor": "transparent", 152 | "width": 56.61994934082031, 153 | "height": 25, 154 | "seed": 413567610, 155 | "groupIds": [], 156 | "frameId": null, 157 | "roundness": null, 158 | "boundElements": [], 159 | "updated": 1696408290603, 160 | "link": null, 161 | "locked": false, 162 | "fontSize": 20, 163 | "fontFamily": 1, 164 | "text": "lvalue", 165 | "textAlign": "left", 166 | "verticalAlign": "top", 167 | "containerId": null, 168 | "originalText": "lvalue", 169 | "lineHeight": 1.25, 170 | "baseline": 18 171 | }, 172 | { 173 | "type": "text", 174 | "version": 233, 175 | "versionNonce": 5237035, 176 | "isDeleted": false, 177 | "id": "zgwzMga65l1Ymt7dW8TI2", 178 | "fillStyle": "hachure", 179 | "strokeWidth": 1, 180 | "strokeStyle": "solid", 181 | "roughness": 1, 182 | "opacity": 100, 183 | "angle": 0, 184 | "x": 335.9974014063148, 185 | "y": 183.13522411863863, 186 | "strokeColor": "#1e1e1e", 187 | "backgroundColor": "transparent", 188 | "width": 59.95994567871094, 189 | "height": 25, 190 | "seed": 1193487162, 191 | "groupIds": [], 192 | "frameId": null, 193 | "roundness": null, 194 | "boundElements": [], 195 | "updated": 1696408290603, 196 | "link": null, 197 | "locked": false, 198 | "fontSize": 20, 199 | "fontFamily": 1, 200 | "text": "rvalue", 201 | "textAlign": "left", 202 | "verticalAlign": "top", 203 | "containerId": null, 204 | "originalText": "rvalue", 205 | "lineHeight": 1.25, 206 | "baseline": 18 207 | } 208 | ], 209 | "appState": { 210 | "gridSize": null, 211 | "viewBackgroundColor": "#ffffff" 212 | }, 213 | "files": {} 214 | } -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-00-l-vs-r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/value-categories-00-l-vs-r.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-01-l-vs-r-move.excalidraw: -------------------------------------------------------------------------------- 1 | { 2 | "type": "excalidraw", 3 | "version": 2, 4 | "source": "https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor", 5 | "elements": [ 6 | { 7 | "type": "rectangle", 8 | "version": 123, 9 | "versionNonce": 747634326, 10 | "isDeleted": false, 11 | "id": "ByIfc64HFcgwjK2GS3MTw", 12 | "fillStyle": "hachure", 13 | "strokeWidth": 1, 14 | "strokeStyle": "solid", 15 | "roughness": 1, 16 | "opacity": 100, 17 | "angle": 0, 18 | "x": 299.013671875, 19 | "y": 166.47265625, 20 | "strokeColor": "#1e1e1e", 21 | "backgroundColor": "transparent", 22 | "width": 160.4296875, 23 | "height": 160.4296875, 24 | "seed": 895471370, 25 | "groupIds": [], 26 | "frameId": null, 27 | "roundness": { 28 | "type": 3 29 | }, 30 | "boundElements": [], 31 | "updated": 1696408246913, 32 | "link": null, 33 | "locked": false 34 | }, 35 | { 36 | "type": "rectangle", 37 | "version": 199, 38 | "versionNonce": 2114310422, 39 | "isDeleted": false, 40 | "id": "n6vqCkxnSoEbQY_1ivyzj", 41 | "fillStyle": "hachure", 42 | "strokeWidth": 1, 43 | "strokeStyle": "solid", 44 | "roughness": 1, 45 | "opacity": 100, 46 | "angle": 0, 47 | "x": 486.732421875, 48 | "y": 348.984375, 49 | "strokeColor": "#1e1e1e", 50 | "backgroundColor": "transparent", 51 | "width": 160.4296875, 52 | "height": 160.4296875, 53 | "seed": 1061885066, 54 | "groupIds": [], 55 | "frameId": null, 56 | "roundness": { 57 | "type": 3 58 | }, 59 | "boundElements": [], 60 | "updated": 1696408246913, 61 | "link": null, 62 | "locked": false 63 | }, 64 | { 65 | "type": "text", 66 | "version": 42, 67 | "versionNonce": 1724253654, 68 | "isDeleted": false, 69 | "id": "QKv6PAIkqr8z-nx7Grsua", 70 | "fillStyle": "hachure", 71 | "strokeWidth": 1, 72 | "strokeStyle": "solid", 73 | "roughness": 1, 74 | "opacity": 100, 75 | "angle": 0, 76 | "x": 325.548828125, 77 | "y": 126.5859375, 78 | "strokeColor": "#1e1e1e", 79 | "backgroundColor": "transparent", 80 | "width": 116.47988891601562, 81 | "height": 25, 82 | "seed": 718009162, 83 | "groupIds": [], 84 | "frameId": null, 85 | "roundness": null, 86 | "boundElements": [], 87 | "updated": 1696408246948, 88 | "link": null, 89 | "locked": false, 90 | "fontSize": 20, 91 | "fontFamily": 1, 92 | "text": "has identity", 93 | "textAlign": "left", 94 | "verticalAlign": "top", 95 | "containerId": null, 96 | "originalText": "has identity", 97 | "lineHeight": 1.25, 98 | "baseline": 18 99 | }, 100 | { 101 | "type": "text", 102 | "version": 189, 103 | "versionNonce": 1424386698, 104 | "isDeleted": false, 105 | "id": "UhJDHqr0wl1BOdh8AC7_Q", 106 | "fillStyle": "hachure", 107 | "strokeWidth": 1, 108 | "strokeStyle": "solid", 109 | "roughness": 1, 110 | "opacity": 100, 111 | "angle": 0, 112 | "x": 516.8518524169922, 113 | "y": 128.125, 114 | "strokeColor": "#1e1e1e", 115 | "backgroundColor": "transparent", 116 | "width": 102.81990051269531, 117 | "height": 25, 118 | "seed": 407042570, 119 | "groupIds": [], 120 | "frameId": null, 121 | "roundness": null, 122 | "boundElements": [], 123 | "updated": 1696408246949, 124 | "link": null, 125 | "locked": false, 126 | "fontSize": 20, 127 | "fontFamily": 1, 128 | "text": "no identity", 129 | "textAlign": "left", 130 | "verticalAlign": "top", 131 | "containerId": null, 132 | "originalText": "no identity", 133 | "lineHeight": 1.25, 134 | "baseline": 18 135 | }, 136 | { 137 | "type": "text", 138 | "version": 418, 139 | "versionNonce": 586341142, 140 | "isDeleted": false, 141 | "id": "t7U87G5hyTfl5hyQHSDKo", 142 | "fillStyle": "hachure", 143 | "strokeWidth": 1, 144 | "strokeStyle": "solid", 145 | "roughness": 1, 146 | "opacity": 100, 147 | "angle": 4.71238898038469, 148 | "x": 203.00059509277344, 149 | "y": 233.0531234741211, 150 | "strokeColor": "#1e1e1e", 151 | "backgroundColor": "transparent", 152 | "width": 115.61990356445312, 153 | "height": 25, 154 | "seed": 917186762, 155 | "groupIds": [], 156 | "frameId": null, 157 | "roundness": null, 158 | "boundElements": [], 159 | "updated": 1696408246949, 160 | "link": null, 161 | "locked": false, 162 | "fontSize": 20, 163 | "fontFamily": 1, 164 | "text": "not movable", 165 | "textAlign": "left", 166 | "verticalAlign": "top", 167 | "containerId": null, 168 | "originalText": "not movable", 169 | "lineHeight": 1.25, 170 | "baseline": 18 171 | }, 172 | { 173 | "type": "text", 174 | "version": 600, 175 | "versionNonce": 1586268490, 176 | "isDeleted": false, 177 | "id": "mby9_bCAPPFoo0EAA2yUq", 178 | "fillStyle": "hachure", 179 | "strokeWidth": 1, 180 | "strokeStyle": "solid", 181 | "roughness": 1, 182 | "opacity": 100, 183 | "angle": 4.71238898038469, 184 | "x": 222.38792419433594, 185 | "y": 416.1177978515625, 186 | "strokeColor": "#1e1e1e", 187 | "backgroundColor": "transparent", 188 | "width": 73.89993286132812, 189 | "height": 25, 190 | "seed": 496992138, 191 | "groupIds": [], 192 | "frameId": null, 193 | "roundness": null, 194 | "boundElements": [], 195 | "updated": 1696408246949, 196 | "link": null, 197 | "locked": false, 198 | "fontSize": 20, 199 | "fontFamily": 1, 200 | "text": "movable", 201 | "textAlign": "left", 202 | "verticalAlign": "top", 203 | "containerId": null, 204 | "originalText": "movable", 205 | "lineHeight": 1.25, 206 | "baseline": 18 207 | }, 208 | { 209 | "type": "text", 210 | "version": 11, 211 | "versionNonce": 89327702, 212 | "isDeleted": false, 213 | "id": "FTrCmJwHaYSIlW0v58io4", 214 | "fillStyle": "hachure", 215 | "strokeWidth": 1, 216 | "strokeStyle": "solid", 217 | "roughness": 1, 218 | "opacity": 100, 219 | "angle": 0, 220 | "x": 343.447265625, 221 | "y": 229.7265625, 222 | "strokeColor": "#1e1e1e", 223 | "backgroundColor": "transparent", 224 | "width": 56.61994934082031, 225 | "height": 25, 226 | "seed": 962264650, 227 | "groupIds": [], 228 | "frameId": null, 229 | "roundness": null, 230 | "boundElements": [], 231 | "updated": 1696408246949, 232 | "link": null, 233 | "locked": false, 234 | "fontSize": 20, 235 | "fontFamily": 1, 236 | "text": "lvalue", 237 | "textAlign": "left", 238 | "verticalAlign": "top", 239 | "containerId": null, 240 | "originalText": "lvalue", 241 | "lineHeight": 1.25, 242 | "baseline": 18 243 | }, 244 | { 245 | "type": "text", 246 | "version": 117, 247 | "versionNonce": 1937821706, 248 | "isDeleted": false, 249 | "id": "KEV4ZiZFoo6tqmjBqYcYE", 250 | "fillStyle": "hachure", 251 | "strokeWidth": 1, 252 | "strokeStyle": "solid", 253 | "roughness": 1, 254 | "opacity": 100, 255 | "angle": 0, 256 | "x": 539.5044784545898, 257 | "y": 418.09375, 258 | "strokeColor": "#1e1e1e", 259 | "backgroundColor": "transparent", 260 | "width": 59.95994567871094, 261 | "height": 25, 262 | "seed": 918070538, 263 | "groupIds": [], 264 | "frameId": null, 265 | "roundness": null, 266 | "boundElements": [], 267 | "updated": 1696408246949, 268 | "link": null, 269 | "locked": false, 270 | "fontSize": 20, 271 | "fontFamily": 1, 272 | "text": "rvalue", 273 | "textAlign": "left", 274 | "verticalAlign": "top", 275 | "containerId": null, 276 | "originalText": "rvalue", 277 | "lineHeight": 1.25, 278 | "baseline": 18 279 | } 280 | ], 281 | "appState": { 282 | "gridSize": null, 283 | "viewBackgroundColor": "#ffffff" 284 | }, 285 | "files": {} 286 | } -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-01-l-vs-r-move.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/value-categories-01-l-vs-r-move.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-02-move-semantic.excalidraw: -------------------------------------------------------------------------------- 1 | { 2 | "type": "excalidraw", 3 | "version": 2, 4 | "source": "https://marketplace.visualstudio.com/items?itemName=pomdtr.excalidraw-editor", 5 | "elements": [ 6 | { 7 | "type": "rectangle", 8 | "version": 124, 9 | "versionNonce": 715934884, 10 | "isDeleted": false, 11 | "id": "ByIfc64HFcgwjK2GS3MTw", 12 | "fillStyle": "hachure", 13 | "strokeWidth": 1, 14 | "strokeStyle": "solid", 15 | "roughness": 1, 16 | "opacity": 100, 17 | "angle": 0, 18 | "x": 299.013671875, 19 | "y": 166.47265625, 20 | "strokeColor": "#1e1e1e", 21 | "backgroundColor": "transparent", 22 | "width": 160.4296875, 23 | "height": 160.4296875, 24 | "seed": 895471370, 25 | "groupIds": [], 26 | "frameId": null, 27 | "roundness": { 28 | "type": 3 29 | }, 30 | "boundElements": [], 31 | "updated": 1696408576113, 32 | "link": null, 33 | "locked": false 34 | }, 35 | { 36 | "type": "rectangle", 37 | "version": 202, 38 | "versionNonce": 2001440676, 39 | "isDeleted": false, 40 | "id": "n6vqCkxnSoEbQY_1ivyzj", 41 | "fillStyle": "hachure", 42 | "strokeWidth": 1, 43 | "strokeStyle": "solid", 44 | "roughness": 1, 45 | "opacity": 100, 46 | "angle": 0, 47 | "x": 486.732421875, 48 | "y": 348.984375, 49 | "strokeColor": "#1e1e1e", 50 | "backgroundColor": "transparent", 51 | "width": 160.4296875, 52 | "height": 160.4296875, 53 | "seed": 1061885066, 54 | "groupIds": [], 55 | "frameId": null, 56 | "roundness": { 57 | "type": 3 58 | }, 59 | "boundElements": [], 60 | "updated": 1696408580335, 61 | "link": null, 62 | "locked": false 63 | }, 64 | { 65 | "type": "text", 66 | "version": 45, 67 | "versionNonce": 1376506645, 68 | "isDeleted": false, 69 | "id": "QKv6PAIkqr8z-nx7Grsua", 70 | "fillStyle": "hachure", 71 | "strokeWidth": 1, 72 | "strokeStyle": "solid", 73 | "roughness": 1, 74 | "opacity": 100, 75 | "angle": 0, 76 | "x": 325.548828125, 77 | "y": 126.5859375, 78 | "strokeColor": "#1e1e1e", 79 | "backgroundColor": "transparent", 80 | "width": 116.47988891601562, 81 | "height": 25, 82 | "seed": 718009162, 83 | "groupIds": [], 84 | "frameId": null, 85 | "roundness": null, 86 | "boundElements": [], 87 | "updated": 1696426480129, 88 | "link": null, 89 | "locked": false, 90 | "fontSize": 20, 91 | "fontFamily": 1, 92 | "text": "has identity", 93 | "textAlign": "left", 94 | "verticalAlign": "top", 95 | "containerId": null, 96 | "originalText": "has identity", 97 | "lineHeight": 1.25, 98 | "baseline": 18 99 | }, 100 | { 101 | "type": "text", 102 | "version": 192, 103 | "versionNonce": 60708027, 104 | "isDeleted": false, 105 | "id": "UhJDHqr0wl1BOdh8AC7_Q", 106 | "fillStyle": "hachure", 107 | "strokeWidth": 1, 108 | "strokeStyle": "solid", 109 | "roughness": 1, 110 | "opacity": 100, 111 | "angle": 0, 112 | "x": 516.8518524169922, 113 | "y": 128.125, 114 | "strokeColor": "#1e1e1e", 115 | "backgroundColor": "transparent", 116 | "width": 102.81990051269531, 117 | "height": 25, 118 | "seed": 407042570, 119 | "groupIds": [], 120 | "frameId": null, 121 | "roundness": null, 122 | "boundElements": [], 123 | "updated": 1696426480130, 124 | "link": null, 125 | "locked": false, 126 | "fontSize": 20, 127 | "fontFamily": 1, 128 | "text": "no identity", 129 | "textAlign": "left", 130 | "verticalAlign": "top", 131 | "containerId": null, 132 | "originalText": "no identity", 133 | "lineHeight": 1.25, 134 | "baseline": 18 135 | }, 136 | { 137 | "type": "text", 138 | "version": 421, 139 | "versionNonce": 1269475445, 140 | "isDeleted": false, 141 | "id": "t7U87G5hyTfl5hyQHSDKo", 142 | "fillStyle": "hachure", 143 | "strokeWidth": 1, 144 | "strokeStyle": "solid", 145 | "roughness": 1, 146 | "opacity": 100, 147 | "angle": 4.71238898038469, 148 | "x": 203.00059509277344, 149 | "y": 233.0531234741211, 150 | "strokeColor": "#1e1e1e", 151 | "backgroundColor": "transparent", 152 | "width": 115.61990356445312, 153 | "height": 25, 154 | "seed": 917186762, 155 | "groupIds": [], 156 | "frameId": null, 157 | "roundness": null, 158 | "boundElements": [], 159 | "updated": 1696426480130, 160 | "link": null, 161 | "locked": false, 162 | "fontSize": 20, 163 | "fontFamily": 1, 164 | "text": "not movable", 165 | "textAlign": "left", 166 | "verticalAlign": "top", 167 | "containerId": null, 168 | "originalText": "not movable", 169 | "lineHeight": 1.25, 170 | "baseline": 18 171 | }, 172 | { 173 | "type": "text", 174 | "version": 603, 175 | "versionNonce": 403309915, 176 | "isDeleted": false, 177 | "id": "mby9_bCAPPFoo0EAA2yUq", 178 | "fillStyle": "hachure", 179 | "strokeWidth": 1, 180 | "strokeStyle": "solid", 181 | "roughness": 1, 182 | "opacity": 100, 183 | "angle": 4.71238898038469, 184 | "x": 222.38792419433594, 185 | "y": 416.1177978515625, 186 | "strokeColor": "#1e1e1e", 187 | "backgroundColor": "transparent", 188 | "width": 73.89993286132812, 189 | "height": 25, 190 | "seed": 496992138, 191 | "groupIds": [], 192 | "frameId": null, 193 | "roundness": null, 194 | "boundElements": [], 195 | "updated": 1696426480130, 196 | "link": null, 197 | "locked": false, 198 | "fontSize": 20, 199 | "fontFamily": 1, 200 | "text": "movable", 201 | "textAlign": "left", 202 | "verticalAlign": "top", 203 | "containerId": null, 204 | "originalText": "movable", 205 | "lineHeight": 1.25, 206 | "baseline": 18 207 | }, 208 | { 209 | "type": "text", 210 | "version": 36, 211 | "versionNonce": 1233637845, 212 | "isDeleted": false, 213 | "id": "FTrCmJwHaYSIlW0v58io4", 214 | "fillStyle": "hachure", 215 | "strokeWidth": 1, 216 | "strokeStyle": "solid", 217 | "roughness": 1, 218 | "opacity": 100, 219 | "angle": 0, 220 | "x": 343.447265625, 221 | "y": 229.7265625, 222 | "strokeColor": "#1e1e1e", 223 | "backgroundColor": "transparent", 224 | "width": 56.61994934082031, 225 | "height": 25, 226 | "seed": 962264650, 227 | "groupIds": [], 228 | "frameId": null, 229 | "roundness": null, 230 | "boundElements": [], 231 | "updated": 1696426480130, 232 | "link": null, 233 | "locked": false, 234 | "fontSize": 20, 235 | "fontFamily": 1, 236 | "text": "lvalue", 237 | "textAlign": "left", 238 | "verticalAlign": "top", 239 | "containerId": null, 240 | "originalText": "lvalue", 241 | "lineHeight": 1.25, 242 | "baseline": 18 243 | }, 244 | { 245 | "type": "text", 246 | "version": 153, 247 | "versionNonce": 1358340603, 248 | "isDeleted": false, 249 | "id": "KEV4ZiZFoo6tqmjBqYcYE", 250 | "fillStyle": "hachure", 251 | "strokeWidth": 1, 252 | "strokeStyle": "solid", 253 | "roughness": 1, 254 | "opacity": 100, 255 | "angle": 0, 256 | "x": 534.7232284545898, 257 | "y": 417.1484375, 258 | "strokeColor": "#1e1e1e", 259 | "backgroundColor": "transparent", 260 | "width": 69.81993103027344, 261 | "height": 25, 262 | "seed": 918070538, 263 | "groupIds": [], 264 | "frameId": null, 265 | "roundness": null, 266 | "boundElements": [], 267 | "updated": 1696426480130, 268 | "link": null, 269 | "locked": false, 270 | "fontSize": 20, 271 | "fontFamily": 1, 272 | "text": "prvalue", 273 | "textAlign": "left", 274 | "verticalAlign": "top", 275 | "containerId": null, 276 | "originalText": "prvalue", 277 | "lineHeight": 1.25, 278 | "baseline": 18 279 | }, 280 | { 281 | "type": "rectangle", 282 | "version": 237, 283 | "versionNonce": 1084152484, 284 | "isDeleted": false, 285 | "id": "BAAvnaV3bHR8TZPbEznmN", 286 | "fillStyle": "hachure", 287 | "strokeWidth": 1, 288 | "strokeStyle": "solid", 289 | "roughness": 1, 290 | "opacity": 100, 291 | "angle": 0, 292 | "x": 298.21484375, 293 | "y": 345.69140625, 294 | "strokeColor": "#1e1e1e", 295 | "backgroundColor": "transparent", 296 | "width": 160.4296875, 297 | "height": 160.4296875, 298 | "seed": 1503829148, 299 | "groupIds": [], 300 | "frameId": null, 301 | "roundness": { 302 | "type": 3 303 | }, 304 | "boundElements": [], 305 | "updated": 1696408576113, 306 | "link": null, 307 | "locked": false 308 | }, 309 | { 310 | "type": "text", 311 | "version": 153, 312 | "versionNonce": 88801243, 313 | "isDeleted": false, 314 | "id": "-bjAOEv91uP7KidtO6vTS", 315 | "fillStyle": "hachure", 316 | "strokeWidth": 1, 317 | "strokeStyle": "solid", 318 | "roughness": 1, 319 | "opacity": 100, 320 | "angle": 0, 321 | "x": 345.33203125, 322 | "y": 416.03125, 323 | "strokeColor": "#1e1e1e", 324 | "backgroundColor": "transparent", 325 | "width": 62.599945068359375, 326 | "height": 25, 327 | "seed": 1692182812, 328 | "groupIds": [], 329 | "frameId": null, 330 | "roundness": null, 331 | "boundElements": [], 332 | "updated": 1696426482505, 333 | "link": null, 334 | "locked": false, 335 | "fontSize": 20, 336 | "fontFamily": 1, 337 | "text": "xvalue", 338 | "textAlign": "left", 339 | "verticalAlign": "top", 340 | "containerId": null, 341 | "originalText": "xvalue", 342 | "lineHeight": 1.25, 343 | "baseline": 18 344 | } 345 | ], 346 | "appState": { 347 | "gridSize": null, 348 | "viewBackgroundColor": "#ffffff" 349 | }, 350 | "files": {} 351 | } -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-02-move-semantic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/value-categories-02-move-semantic.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-03-rvalue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/value-categories-03-rvalue.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/value-categories-04-general.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/value-categories-04-general.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/whoarewe-performances.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/whoarewe-performances.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/zach-math-thinking.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/zach-math-thinking.jpg -------------------------------------------------------------------------------- /01_03_raii_move-semantics/attachments/zero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eth-cscs/cpp-course-2023/1c4923995cbe58c3210e725f4609609ec8dbc692/01_03_raii_move-semantics/attachments/zero.png -------------------------------------------------------------------------------- /01_03_raii_move-semantics/code/raii_buffer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Abstraction around a generic memory buffer. As always, be careful managing 6 | // buffers like these, but if you're going to use them at least ensure that 7 | // resources are freed correctly by using RAII and rule of zero/five! 8 | class buffer { 9 | using buffer_type = char[]; 10 | 11 | public: 12 | // Construct a buffer that is backed by memory 13 | buffer(std::size_t n) : p( 14 | // What's the difference between these? 15 | // 1. 16 | std::make_unique(n) 17 | // 2. 18 | // new char[n] 19 | // 20 | // 3. 21 | // new char[n]() 22 | // 23 | // See overload 2) of 24 | // https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique and 25 | // 2.2 of https://en.cppreference.com/w/cpp/language/new. 26 | // 27 | // Also see https://quick-bench.com/q/RjAoY0EgtzOOiiajSQUuiB4rn74. 28 | ) {} 29 | 30 | 31 | // All of the following special member functions can be omitted (rule of 32 | // zero), or may be explicitly defaulted/deleted (rule of five). 33 | 34 | // Default construction results in an empty buffer 35 | buffer() = default; 36 | 37 | // Move construction empties the source buffer 38 | buffer(buffer&&) = default; 39 | // Move assignment empties the source buffer 40 | buffer& operator=(buffer&&) = default; 41 | 42 | // Copy construction is disabled thanks to unique_ptr having a deleted copy constructor 43 | buffer(buffer const&) = default; 44 | // Copy assignment is deleted thanks to unique_ptr having a deleted copy assignment operator 45 | buffer& operator=(buffer const&) = default; 46 | 47 | // Destructor will release the buffer, if p contains one 48 | ~buffer() = default; 49 | 50 | bool valid() const noexcept { 51 | return bool(p); 52 | } 53 | 54 | private: 55 | std::unique_ptr p; 56 | }; 57 | 58 | int main() { 59 | buffer b(1024); 60 | assert(b.valid()); 61 | 62 | buffer b2; 63 | assert(!b2.valid()); 64 | 65 | b2 = std::move(b); 66 | assert(!b.valid()); 67 | assert(b2.valid()); 68 | 69 | // use buffer... 70 | 71 | // b2 will release the underlying buffer, b will not release anything 72 | // because the contained unique_ptr is empty. This all happens without 73 | // explicitly writing a single move/copy constructor/assignment operator or 74 | // destructor. 75 | } 76 | -------------------------------------------------------------------------------- /01_04_generic_programming_1_templates/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if (${CPP_COURSE_BUILD_SLIDES}) 2 | # add_marp_slides(01_04_generic_programming_part1 generic_programming_part1.md) 3 | add_marp_slides(01_04_templates_part1 templates_part1.md) 4 | endif() 5 | 6 | add_executable(01_04_fold_print code/fold_print.cpp) 7 | 8 | add_executable(01_04_debug_print_specializations code/debug_print_specializations.cpp) 9 | target_include_directories(01_04_debug_print_specializations PUBLIC code) 10 | -------------------------------------------------------------------------------- /01_04_generic_programming_1_templates/code/debug_print_specializations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "demangle_helper.hpp" 3 | 4 | template 5 | struct X { 6 | X() { 7 | std::cout << "Primary : " << debug::print_type, T, U>(", ") << "\n"; 8 | } 9 | }; 10 | 11 | template 12 | struct X { 13 | X() { 14 | std::cout << "Specialization : " << debug::print_type, T, typename T::extra_type>(", ") << "\n"; 15 | } 16 | }; 17 | 18 | struct A { using value_type = int; }; 19 | struct B { using extra_type = int; }; 20 | struct C { using extra_type = float; }; 21 | struct D { using extra_type = char; }; 22 | 23 | int main() { 24 | X a; // uses primary - A::value_type not a match 25 | X b; // uses specialization - B::extra_type = int 26 | X c; // uses primary - C::extra_type not int 27 | X b1; // uses primary - B::extra_type not char 28 | X d; // uses specialization, D::extra_type = char 29 | const char *ptr = "This is a test"; 30 | std::cout << "ptr type is : " << debug::print_type("") << std::endl; 31 | std::cout << "std::free is : " << debug::print_type("") << std::endl; 32 | std::cout << "no params < > : " << debug::print_type<>("") << std::endl; 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /01_04_generic_programming_1_templates/code/demangle_helper.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // gcc and clang both provide this header 6 | #if __has_include() 7 | # include 8 | using cxxabi_supported__ = std::true_type; 9 | #else 10 | using cxxabi_supported__ = std::false_type; 11 | // create some dummy function to make the compiler happy in the true_type instantiation 12 | namespace abi { 13 | template char* __cxa_demangle(Ts... ts) { return nullptr; } 14 | } 15 | #endif 16 | 17 | // -------------------------------------------------------------------- 18 | namespace debug::detail { 19 | // default : use built-in typeid to get the best info we can 20 | template 21 | struct demangle_helper 22 | { 23 | char const* type_id() const { return typeid(T).name(); } 24 | }; 25 | 26 | // if available : demangle an arbitrary c++ type using gnu utility 27 | template 28 | struct demangle_helper 29 | { 30 | demangle_helper() 31 | : demangled_{abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr), std::free} 32 | { 33 | } 34 | 35 | char const* type_id() const { return demangled_ ? demangled_.get() : typeid(T).name(); } 36 | 37 | private: 38 | std::unique_ptr demangled_; 39 | }; 40 | 41 | template 42 | using cxx_type_id = demangle_helper; 43 | } 44 | 45 | // -------------------------------------------------------------------- 46 | // print type information 47 | // usage : std::cout << debug::print_type("separator") 48 | // separator is appended if the number of types > 1 49 | // -------------------------------------------------------------------- 50 | namespace debug { 51 | template // print a single type 52 | inline std::string print_type(const char* = "") 53 | { 54 | return std::string(detail::cxx_type_id().type_id()); 55 | } 56 | 57 | template <> // fallback for an empty type 58 | inline std::string print_type<>(char const*) 59 | { 60 | return "<>"; 61 | } 62 | 63 | template // print a list of types 64 | inline std::enable_if_t print_type(const char* delim = "") 65 | { 66 | std::string temp(print_type()); 67 | return temp + delim + print_type(delim); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /01_04_generic_programming_1_templates/code/fold_print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void print1(Ts... vals) 5 | { 6 | (std::cout << ... << vals); 7 | } 8 | 9 | template 10 | void print2(const char *delim, Ts... vals) 11 | { 12 | auto showdelim = [](const char *delim, const auto& param) -> const auto& { 13 | std::cout << delim; 14 | return param; 15 | }; 16 | 17 | (std::cout << ... << showdelim(delim, vals) ) << std::endl ; 18 | } 19 | 20 | void print3(const char *delim) {} 21 | 22 | template 23 | void print3(const char *delim, T first, Ts... vals) 24 | { 25 | auto showdelim = [](const char *delim, const auto& param) -> const auto& { 26 | std::cout << delim; 27 | return param; 28 | }; 29 | std::cout << first; 30 | (std::cout << ... << showdelim(delim, vals) ) << std::endl ; 31 | } 32 | 33 | int main() 34 | { 35 | std::cout << std::endl << "method 1:" << std::endl; 36 | print1(", ", 1, "charlike", 3.14, std::string("stringlike"), static_cast(65), 6); 37 | std::cout << std::endl << "method 2:" << std::endl; 38 | print2(", ", 1, "charlike", 3.14, std::string("stringlike"), static_cast(65), 6); 39 | std::cout << std::endl << "method 3:" << std::endl; 40 | print3(", ", 1, "charlike", 3.14, std::string("stringlike"), static_cast(65), 6); 41 | 42 | std::cout << std::endl << "method 1:" << std::endl; 43 | print1(", "); 44 | std::cout << std::endl << "method 2:" << std::endl; 45 | print2(", "); 46 | std::cout << std::endl << "method 3:" << std::endl; 47 | print3(", "); 48 | } 49 | -------------------------------------------------------------------------------- /01_04_generic_programming_1_templates/code/type_list.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace util { 8 | 9 | // mp_list: generic type list 10 | template 11 | struct mp_list {}; 12 | 13 | // mp_rename: A<...> -> B<...> 14 | template class B> 15 | struct mp_rename_impl; 16 | 17 | template