├── book
├── .gitignore
├── imgs
│ ├── long-long.0.png
│ ├── mcpp-standard-demo.gif
│ └── mcpp-logo.svg
├── README.md
├── src
│ ├── cpp11
│ │ ├── 01-default-and-delete.md
│ │ ├── 13-long-long.md
│ │ ├── 00-auto-and-decltype.md
│ │ ├── 09-list-initialization.md
│ │ └── 12-nullptr.md
│ ├── fqa.md
│ ├── SUMMARY.md
│ ├── base
│ │ └── chapter_0.md
│ ├── changelog.md
│ └── README.md
├── en
│ ├── src
│ │ ├── cpp11
│ │ │ └── 01-default-and-delete.md
│ │ ├── fqa.md
│ │ ├── SUMMARY.md
│ │ ├── base
│ │ │ └── chapter_0.md
│ │ ├── changelog.md
│ │ └── README.md
│ └── book.toml
├── book.toml
└── build.sh
├── dslings
├── cpp14
│ └── README.md
├── cpp17
│ └── README.md
├── cpp20
│ └── README.md
├── cpp23
│ └── README.md
├── en
│ ├── xmake.lua
│ ├── cpp11
│ │ ├── 01-default-and-delete-2.cpp
│ │ ├── 08-literal-type-1.cpp
│ │ ├── 14-type-alias-3.cpp
│ │ ├── 01-default-and-delete-0.cpp
│ │ ├── 09-list-initialization-1.cpp
│ │ ├── 07-constexpr-0.cpp
│ │ ├── 03-trailing-return-type.cpp
│ │ ├── 00-auto-and-decltype-0.cpp
│ │ ├── 00-auto-and-decltype-3.cpp
│ │ ├── 00-auto-and-decltype-1.cpp
│ │ ├── 06-scoped-enums-0.cpp
│ │ ├── 02-final-and-override-1.cpp
│ │ ├── 09-list-initialization-0.cpp
│ │ ├── 02-final-and-override-0.cpp
│ │ ├── 14-type-alias-0.cpp
│ │ ├── 12-nullptr-1.cpp
│ │ ├── 09-list-initialization-2.cpp
│ │ ├── 13-long-long-1.cpp
│ │ ├── 13-long-long-0.cpp
│ │ ├── 14-type-alias-2.cpp
│ │ ├── 01-default-and-delete-1.cpp
│ │ ├── 12-nullptr-2.cpp
│ │ ├── 00-auto-and-decltype-2.cpp
│ │ ├── 12-nullptr-0.cpp
│ │ ├── 14-type-alias-1.cpp
│ │ ├── 08-literal-type-0.cpp
│ │ ├── 04-rvalue-references.cpp
│ │ ├── 00-auto-and-decltype-4.cpp
│ │ ├── 09-list-initialization-3.cpp
│ │ ├── 06-scoped-enums-1.cpp
│ │ ├── 11-inherited-constructors-0.cpp
│ │ ├── 07-constexpr-1.cpp
│ │ ├── 05-move-semantics-0.cpp
│ │ ├── 02-final-and-override-2.cpp
│ │ ├── 11-inherited-constructors-2.cpp
│ │ ├── 10-delegating-constructors-0.cpp
│ │ ├── 10-delegating-constructors-1.cpp
│ │ ├── 05-move-semantics-2.cpp
│ │ ├── 11-inherited-constructors-1.cpp
│ │ └── 05-move-semantics-1.cpp
│ └── hello-mcpp.cpp
├── xmake.lua
├── cpp11
│ ├── 01-default-and-delete-2.cpp
│ ├── 08-literal-type-1.cpp
│ ├── 07-constexpr-0.cpp
│ ├── 01-default-and-delete-0.cpp
│ ├── 14-type-alias-3.cpp
│ ├── 09-list-initialization-1.cpp
│ ├── 06-scoped-enums-0.cpp
│ ├── 03-trailing-return-type.cpp
│ ├── 00-auto-and-decltype-0.cpp
│ ├── 00-auto-and-decltype-3.cpp
│ ├── 14-type-alias-0.cpp
│ ├── 00-auto-and-decltype-1.cpp
│ ├── 02-final-and-override-1.cpp
│ ├── 09-list-initialization-0.cpp
│ ├── 02-final-and-override-0.cpp
│ ├── 12-nullptr-2.cpp
│ ├── 13-long-long-0.cpp
│ ├── 13-long-long-1.cpp
│ ├── 09-list-initialization-2.cpp
│ ├── 12-nullptr-1.cpp
│ ├── 14-type-alias-2.cpp
│ ├── 01-default-and-delete-1.cpp
│ ├── 14-type-alias-1.cpp
│ ├── 12-nullptr-0.cpp
│ ├── 00-auto-and-decltype-2.cpp
│ ├── 08-literal-type-0.cpp
│ ├── 04-rvalue-references.cpp
│ ├── 06-scoped-enums-1.cpp
│ ├── 00-auto-and-decltype-4.cpp
│ ├── 07-constexpr-1.cpp
│ ├── 11-inherited-constructors-0.cpp
│ ├── 05-move-semantics-0.cpp
│ ├── 09-list-initialization-3.cpp
│ ├── 02-final-and-override-2.cpp
│ ├── 11-inherited-constructors-2.cpp
│ ├── 10-delegating-constructors-0.cpp
│ ├── 05-move-semantics-2.cpp
│ ├── 10-delegating-constructors-1.cpp
│ ├── 11-inherited-constructors-1.cpp
│ └── 05-move-semantics-1.cpp
├── d2x
│ ├── honly_logger.hpp
│ ├── common.lua
│ └── common.hpp
└── hello-mcpp.cpp
├── videos
├── d2x
│ ├── __init__.py
│ ├── animations.py
│ └── video.py
└── README.md
├── .devcontainer
├── postCreate.sh
└── devcontainer.json
├── .gitignore
├── .vscode
└── settings.json
├── config.xlings
├── tools
├── update_upstream.sh
└── update_upstream.bat
├── CLA.md
├── .github
└── workflows
│ └── online-ebook.yml
├── README.zh.md
├── README.zh.hant.md
└── README.md
/book/.gitignore:
--------------------------------------------------------------------------------
1 | book
--------------------------------------------------------------------------------
/dslings/cpp14/README.md:
--------------------------------------------------------------------------------
1 | # TODO
--------------------------------------------------------------------------------
/dslings/cpp17/README.md:
--------------------------------------------------------------------------------
1 | # TODO
--------------------------------------------------------------------------------
/dslings/cpp20/README.md:
--------------------------------------------------------------------------------
1 | # TODO
--------------------------------------------------------------------------------
/dslings/cpp23/README.md:
--------------------------------------------------------------------------------
1 | # TODO
--------------------------------------------------------------------------------
/videos/d2x/__init__.py:
--------------------------------------------------------------------------------
1 | __version__ = "0.1.0"
2 |
3 | from .animations import *
4 | from .video import *
--------------------------------------------------------------------------------
/book/imgs/long-long.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunrisepeak/mcpp-standard/HEAD/book/imgs/long-long.0.png
--------------------------------------------------------------------------------
/book/imgs/mcpp-standard-demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunrisepeak/mcpp-standard/HEAD/book/imgs/mcpp-standard-demo.gif
--------------------------------------------------------------------------------
/book/README.md:
--------------------------------------------------------------------------------
1 | **现代C++标准(cppref)**
2 |
3 | - C++11: [中](https://zh.cppreference.com/w/cpp/11) / [En](https://en.cppreference.com/w/cpp/11)
4 |
--------------------------------------------------------------------------------
/dslings/en/xmake.lua:
--------------------------------------------------------------------------------
1 | add_includedirs("..")
2 |
3 | target("00-0-hello-mcpp")
4 | set_languages("cxx11")
5 | add_files("hello-mcpp.cpp")
6 |
7 | includes("cpp11")
8 |
--------------------------------------------------------------------------------
/book/src/cpp11/01-default-and-delete.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ./01-default-and-delete.html
7 | [English]: ../en/cpp11/01-default-and-delete.html
8 |
9 | # ...
--------------------------------------------------------------------------------
/book/src/fqa.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ./fqa.html
7 | [English]: ../en/fqa.html
8 |
9 | # 常见问题
10 |
11 | 更多问题和反馈 -> [教程论坛交流版块](https://forum.d2learn.org/category/20)
12 |
--------------------------------------------------------------------------------
/book/en/src/cpp11/01-default-and-delete.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ../../cpp11/01-default-and-delete.html
7 | [English]: ./01-default-and-delete.html
8 |
9 | # Defaulted and Deleted Functions
10 |
--------------------------------------------------------------------------------
/book/en/src/fqa.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ../fqa.html
7 | [English]: ./fqa.html
8 |
9 | # Frequently Asked Questions
10 |
11 | More questions and feedback -> [Tutorial Forum Discussion Section](https://forum.d2learn.org/category/20)
12 |
--------------------------------------------------------------------------------
/.devcontainer/postCreate.sh:
--------------------------------------------------------------------------------
1 | # postCreate.sh
2 | #!/usr/bin/env bash
3 | set -euo pipefail
4 |
5 | sudo apt-get update
6 | sudo apt-get install -y ncurses-bin libtinfo6 libncursesw6 curl ca-certificates git
7 |
8 | if ! command -v xlings >/dev/null 2>&1; then
9 | curl -fsSL https://d2learn.org/xlings-install.sh | bash
10 | fi
11 |
12 | echo "xlings installed"
--------------------------------------------------------------------------------
/book/book.toml:
--------------------------------------------------------------------------------
1 | # docs: https://rust-lang.github.io/mdBook
2 | [book]
3 | title = "mcpp-standard | 动手学现代C++核心语言特性"
4 | author = "sunrispeak"
5 | language = "zh"
6 |
7 | [build]
8 | build-dir = "book"
9 |
10 | [output.html]
11 | git-repository-url = "https://github.com/Sunrisepeak/mcpp-standard"
12 |
13 | [preprocessor.foo]
14 | # Add any additional configurations
15 |
--------------------------------------------------------------------------------
/book/en/book.toml:
--------------------------------------------------------------------------------
1 | # docs: https://rust-lang.github.io/mdBook
2 | [book]
3 | title = "mcpp-standard | D2X - Modern C++ Core Language Features"
4 | author = "sunrispeak"
5 | language = "en"
6 |
7 | [build]
8 | build-dir = "../book/en"
9 |
10 | [output.html]
11 | git-repository-url = "https://github.com/Sunrisepeak/mcpp-standard"
12 |
13 | [preprocessor.foo]
14 | # Add any additional configurations
15 |
--------------------------------------------------------------------------------
/book/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # srcirpt dir
4 | SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
5 |
6 | echo "[Top] - Building Chinese book..."
7 | cd "$SCRIPT_DIR"
8 | mdbook build
9 |
10 | echo "[Sub - 1] - Building English book..."
11 | cd "$SCRIPT_DIR"
12 | cd en && mdbook build
13 |
14 | echo "[imgs] - copy imgs to output directory..."
15 | cd "$SCRIPT_DIR"
16 | cp -r imgs book/
17 |
18 | echo "Build completed."
19 |
20 | # python -m http.server --directory book/book
21 |
--------------------------------------------------------------------------------
/.devcontainer/devcontainer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mcpp-standard (Codespaces)",
3 | "image": "mcr.microsoft.com/devcontainers/cpp:ubuntu-24.04",
4 | "customizations": {
5 | "vscode": {
6 | "extensions": [
7 | "ms-vscode.cpptools",
8 | "twxs.cmake",
9 | "ms-vscode.cmake-tools",
10 | "formulahendry.code-runner"
11 | ]
12 | }
13 | },
14 | "remoteUser": "vscode",
15 | "postCreateCommand": "/bin/bash .devcontainer/postCreate.sh",
16 | "postAttachCommand": "xlings checker"
17 | }
18 |
--------------------------------------------------------------------------------
/dslings/xmake.lua:
--------------------------------------------------------------------------------
1 | if is_host("windows") then
2 | set_encodings("source:utf-8", "target:utf-8")
3 | set_toolchains("gcc")
4 | add_ldflags("-static")
5 | end
6 |
7 | local lang = "en"
8 | if d2x and d2x.private and d2x.private.mcpp then
9 | lang = d2x.private.mcpp.lang or "en"
10 | end
11 |
12 | if lang == "zh" then
13 | add_includedirs(".")
14 |
15 | target("00-0-hello-mcpp")
16 | set_languages("cxx11")
17 | add_files("hello-mcpp.cpp")
18 |
19 | includes("cpp11")
20 | else
21 | includes("en")
22 | end
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Compiled Object files
5 | *.slo
6 | *.lo
7 | *.o
8 | *.obj
9 |
10 | # Precompiled Headers
11 | *.gch
12 | *.pch
13 |
14 | # Compiled Dynamic libraries
15 | *.so
16 | *.dylib
17 | *.dll
18 |
19 | # Fortran module files
20 | *.mod
21 | *.smod
22 |
23 | # Compiled Static libraries
24 | *.lai
25 | *.la
26 | *.a
27 | *.lib
28 |
29 | # Executables
30 | *.exe
31 | *.out
32 | *.app
33 |
34 | # Python
35 | __pycache__
36 |
37 | # d2x project files
38 | .xlings
39 | .vscode
40 | .xmake
41 | .zed
42 | llm.config.xlings
43 | media
44 | build
45 | .cache/
46 | dslings/compile_commands.json
47 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "C_Cpp.default.includePath": [
3 | // local include path
4 | "${default}",
5 | "${workspaceFolder}",
6 | "${workspaceFolder}/dslings",
7 | ],
8 |
9 | "C_Cpp.files.exclude": {
10 | "**/.vscode": true,
11 | "**/build": true,
12 | "**/.xmake": true,
13 | "**/.xlings": true,
14 | "**/.DS_Store": true
15 | },
16 |
17 | "files.autoSave": "afterDelay", // or "onFocusChange", "onWindowChange", "off"
18 | "files.autoSaveDelay": 1000,
19 | "files.associations": {
20 | "ostream": "cpp"
21 | } // Only relevant for "afterDelay", in milliseconds
22 | }
--------------------------------------------------------------------------------
/book/src/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | - [README](./README.md)
4 |
5 | # 阅读准备
6 |
7 | - [序言](./base/chapter_0.md)
8 | - [使用说明](./base/chapter_1.md)
9 |
10 | # C++11核心语言特性
11 |
12 | - [auto和decltype - 类型自动推导](./cpp11/00-auto-and-decltype.md)
13 | - [...](./cpp11/01-default-and-delete.md)
14 | - [列表初始化](./cpp11/09-list-initialization.md)
15 | - [委托构造函数](./cpp11/10-delegating-constructors.md)
16 | - [继承构造函数](./cpp11/11-inherited-constructors.md)
17 | - [nullptr - 指针字面量](./cpp11/12-nullptr.md)
18 | - [long long - 64位整数类型](./cpp11/13-long-long.md)
19 | - [using - 类型别名和别名模板](./cpp11/14-type-alias.md)
20 |
21 | # C++14核心语言特性
22 |
23 | # 其他
24 |
25 | - [更新日志](changelog.md)
26 | - [常见问题](fqa.md)
27 |
--------------------------------------------------------------------------------
/book/en/src/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | - [README](./README.md)
4 |
5 | # Getting Started
6 |
7 | - [Preface](./base/chapter_0.md)
8 | - [Usage Guide](./base/chapter_1.md)
9 |
10 | # C++11 Core Language Features
11 |
12 | - [Type Deduction - auto and decltype](./cpp11/00-auto-and-decltype.md)
13 | - [Defaulted and Deleted Functions](./cpp11/01-default-and-delete.md)
14 | - [List Initialization](./cpp11/09-list-initialization.md)
15 | - [Delegating Constructors](./cpp11/10-delegating-constructors.md)
16 | - [Inherited Constructors](./cpp11/11-inherited-constructors.md)
17 | - [nullptr - Pointer Literal](./cpp11/12-nullptr.md)
18 | - [long long - 64-bit Integer Type](./cpp11/13-long-long.md)
19 | - [using - Type Alias and Alias Template](./cpp11/14-type-alias.md)
20 |
21 | # C++14 Core Language Features
22 |
23 | # Additional Resources
24 |
25 | - [Changelog](changelog.md)
26 | - [Frequently Asked Questions](fqa.md)
27 |
--------------------------------------------------------------------------------
/dslings/cpp11/01-default-and-delete-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-2.cpp
4 | //
5 | // Exercise/练习: cpp11 | 01 - default and delete | 禁止函数重载
6 | //
7 | // Tips/提示: 修复编译器错误, 删除错误的函数调用
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker default-and-delete-2
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | void func(int x) {
23 | std::cout << "x = " << x << std::endl;
24 | }
25 |
26 | // 显式删除float参数的重载
27 | void func(float) = delete;
28 |
29 | int main() {
30 |
31 | func(1); // int
32 | func(1.1f); // float
33 |
34 | D2X_WAIT
35 |
36 | return 0;
37 | }
--------------------------------------------------------------------------------
/videos/d2x/animations.py:
--------------------------------------------------------------------------------
1 | from manim import *
2 |
3 | class DHighlight(Succession):
4 | def __init__(self, mobject, color=YELLOW, scale_factor=1.1, **kwargs):
5 | start_mobject = mobject.copy()
6 | target_mobject = mobject.copy().scale(scale_factor).set_color(color)
7 | super().__init__(
8 | Transform(mobject, target_mobject),
9 | Transform(mobject, start_mobject),
10 | run_time=1, # default run_time
11 | **kwargs
12 | )
13 |
14 | class DCameraMove(Succession):
15 | def __init__(self, camera, target_mobject, scale_factor=0.5, **kwargs):
16 | super().__init__(
17 | ApplyMethod(camera.frame.scale, 1 / scale_factor),
18 | ApplyMethod(camera.frame.move_to, target_mobject),
19 | ApplyMethod(camera.frame.scale, scale_factor),
20 | run_time=1, # total run_time
21 | **kwargs
22 | )
--------------------------------------------------------------------------------
/dslings/en/cpp11/01-default-and-delete-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-2.cpp
4 | //
5 | // Exercise: cpp11 | 01 - default and delete | Disabling function overloading
6 | //
7 | // Tips: Fix compiler errors, delete incorrect function calls
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker default-and-delete-2
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | void func(int x) {
23 | std::cout << "x = " << x << std::endl;
24 | }
25 |
26 | // Explicitly delete float parameter overload
27 | void func(float) = delete;
28 |
29 | int main() {
30 |
31 | func(1); // int
32 | func(1.1f); // float
33 |
34 | D2X_WAIT
35 |
36 | return 0;
37 | }
--------------------------------------------------------------------------------
/dslings/cpp11/08-literal-type-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/08-literal-type-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 08 - literal type | 自定义字面值类型
6 | //
7 | // Tips/提示: 根据编译器的输出, 修复编译器报错, 了解如何定义字面值类型
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/named_req/LiteralType.html
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker literal-type-1
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | struct Vector {
22 | int x, y;
23 | Vector(int x_, int y_) : x(x_), y(y_) { }
24 | };
25 |
26 | constexpr Vector add(const Vector& v1, const Vector& v2) {
27 | return Vector(v1.x + v2.x, v1.y + v2.y);
28 | }
29 |
30 | int main() {
31 |
32 | constexpr Vector v1{1, 2}, v2{2, 3};
33 | constexpr Vector v3 = add(v1, v2);
34 |
35 | std::cout << "[ " << v3.x << ", " << v3.y << " ]" << std::endl;
36 |
37 | D2X_WAIT
38 |
39 | return 0;
40 | }
--------------------------------------------------------------------------------
/dslings/cpp11/07-constexpr-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/07-constexpr-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 07 - constexpr | 编译期计算基础: constexpr 和 const的区别
6 | //
7 | // Tips/提示: 修复编译器报错, 并了解编译器变量和编译期函数计算
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/constexpr
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker constexpr
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | int sum_for_1_to(int n) {
22 | return n == 1 ? 1 : n + sum_for_1_to(n - 1);
23 | }
24 |
25 | int main() {
26 |
27 | { // 1. 运行时变量、常量和编译期变量
28 | int size1 = 10;
29 | const int size2 = size1 + 10;
30 | constexpr int size3 = 10 * 3;
31 |
32 | int arr1[size1]; // 选择正确的sizex作为数组大小
33 | }
34 |
35 | { // 2. 编译期计算基础
36 | constexpr int s = sum_for_1_to(4);
37 | d2x_assert_eq(s, 1 + 2 + 3 + 4);
38 | }
39 |
40 | D2X_WAIT
41 |
42 | return 0;
43 | }
--------------------------------------------------------------------------------
/dslings/cpp11/01-default-and-delete-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 01 - default and delete | 显示指定构造函数生成行为
6 | //
7 | // Tips/提示: 根据编译器提示使用`= default`和`= delete`修复错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker default-and-delete
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | // default和delete显式控制 -> 编译器默认构造函数的生成行为
23 | struct A { };
24 | struct B {
25 | B(int x) { std::cout << "B(int x)" << std::endl; }
26 | };
27 | struct C {
28 | C() { }
29 | C(int x = 1) { std::cout << "C(int x = 1)" << std::endl; }
30 | };
31 |
32 | int main() { // 不要直接修改main函数中的代码
33 |
34 | A a;
35 | B b;
36 | C c(1);
37 |
38 | D2X_WAIT
39 |
40 | return 0;
41 | }
--------------------------------------------------------------------------------
/dslings/cpp11/14-type-alias-3.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/14-type-alias-3.cpp
4 | //
5 | // Exercise/练习: cpp11 | 14 - type alias | 标准库中的别名模板应用
6 | //
7 | // Tips/提示: 使用别名模板实现标准库风格的_t/_v模板
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 | #include
22 |
23 | int main() {
24 |
25 | template
26 | D2X_YOUR_ANSWER my_add_pointer_t = typename std::add_pointer;
27 |
28 | int c = 20;
29 | my_add_pointer_t ptr = &c;
30 |
31 | bool ok = std::is_same, int*>::value;
32 |
33 | d2x_assert(ok);
34 | d2x_assert_eq(*ptr, 20);
35 |
36 | D2X_WAIT
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/08-literal-type-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/08-literal-type-1.cpp
4 | //
5 | // Exercise: cpp11 | 08 - literal type | Custom literal types
6 | //
7 | // Tips: Fix compiler errors based on compiler output, understand how to define literal types
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/named_req/LiteralType.html
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker literal-type-1
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | struct Vector {
22 | int x, y;
23 | Vector(int x_, int y_) : x(x_), y(y_) { }
24 | };
25 |
26 | constexpr Vector add(const Vector& v1, const Vector& v2) {
27 | return Vector(v1.x + v2.x, v1.y + v2.y);
28 | }
29 |
30 | int main() {
31 |
32 | constexpr Vector v1{1, 2}, v2{2, 3};
33 | constexpr Vector v3 = add(v1, v2);
34 |
35 | std::cout << "[ " << v3.x << ", " << v3.y << " ]" << std::endl;
36 |
37 | D2X_WAIT
38 |
39 | return 0;
40 | }
--------------------------------------------------------------------------------
/config.xlings:
--------------------------------------------------------------------------------
1 | -- https://xlings.d2learn.org
2 |
3 | xname = "mcpp-standard" -- project name
4 |
5 | -- xim-deps
6 | xim = {
7 | vscode = "", -- config by checker.editor
8 | cpp = "", -- language:[windows:mingw], [linux:gcc], [macos:clang]
9 | mdbook = "0.4.43", -- for d2x book
10 | xppcmds = {
11 | "xmake project -k compile_commands --project=dslings"
12 | }
13 | }
14 |
15 | d2x = {
16 | checker = {
17 | name = "dslings",
18 | editor = "vscode", -- option: vscode, nvim, zed
19 | },
20 |
21 | commands = {
22 | update = {
23 | linux = "tools/update_upstream.sh",
24 | macosx = "tools/update_upstream.sh",
25 | windows = [[tools\update_upstream.bat]],
26 | },
27 | },
28 |
29 | private = {
30 | -- project private attributes
31 | mcpp = {
32 | lang = "en", -- option: en, zh
33 | }
34 | },
35 | }
36 |
37 | xim[d2x.checker.editor or "vscode"] = "" -- config deps(default version
38 | --xlings_llm_config = "llm.config.xlings"
39 |
--------------------------------------------------------------------------------
/dslings/cpp11/09-list-initialization-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 09 - list initialization | 默认初始化语法陷阱
6 | //
7 | // Tips/提示: 根据编译器的输出, 修复编译器报错, 了解默认初始化语法陷阱
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker list-initialization
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | struct Object {
23 | Object() {
24 | x = 0;
25 | std::cout << "Object()" << std::endl;
26 | }
27 | Object(int x) : x(x) {
28 | std::cout << "Object(int): " << x << std::endl;
29 | }
30 | int x;
31 | };
32 |
33 | int main() {
34 |
35 | Object obj1();
36 | Object obj2(2);
37 |
38 | d2x_assert_eq(obj1.x, 0);
39 | d2x_assert_eq(obj2.x, 2);
40 |
41 | D2X_WAIT
42 |
43 | return 0;
44 | }
--------------------------------------------------------------------------------
/dslings/cpp11/06-scoped-enums-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/06-scoped-enums-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 06 - scoped enums | 传统枚举类型潜在问题
6 | //
7 | // Tips/提示: 通过编译器的错误提示修复代码, 并理解传统枚举类型的潜在问题
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/enum
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker scoped-enums
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | enum Color {
22 | RED,
23 | GREEN,
24 | BLUE,
25 | ORANGE // 1.类型冲突 - 橙色
26 | };
27 |
28 | enum Fruit {
29 | Apple,
30 | Banana,
31 | ORANGE // 1.类型冲突 - 橙子
32 | };
33 |
34 | int main() {
35 |
36 | Color color = RED;
37 | Fruit fruit = Apple;
38 |
39 | d2x_assert_eq(color, RED);
40 | d2x_assert_eq(fruit, Apple);
41 |
42 | // 2.符合语法, 但逻辑错误的类型匹配
43 | if (color == Apple) { // 不要删除这行代码
44 | // 代码会运行到这里
45 | D2X_WAIT
46 | }
47 |
48 | if (fruit == RED) {
49 | D2X_WAIT
50 | }
51 |
52 | D2X_WAIT
53 |
54 | return 0;
55 | }
--------------------------------------------------------------------------------
/dslings/d2x/honly_logger.hpp:
--------------------------------------------------------------------------------
1 | #ifndef LOGGER_HPP__HONLY
2 | #define LOGGER_HPP__HONLY
3 |
4 | #include
5 |
6 | #ifndef HONLY_LOGGER_TAG
7 | #define HONLY_LOGGER_TAG "HONLY"
8 | #endif
9 |
10 | #define LOG_ENABLE true
11 | #define _HONLY_LOG(fd, ...) if (LOG_ENABLE) { fprintf (fd, __VA_ARGS__); fprintf (fd, "\033[0m\n"); fflush(stdout); }
12 | #define HONLY_LOGI_P(...) { fprintf (stdout, "\033[32m[%s LOGI]: - ", HONLY_LOGGER_TAG); _HONLY_LOG(stdout, __VA_ARGS__); }
13 | #define HONLY_LOGI(...) { fprintf (stdout, "\033[32m[%s LOGI]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
14 | #define HONLY_LOGD(...) { fprintf (stdout, "\033[37m[%s LOGD]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
15 | #define HONLY_LOGW(...) { fprintf (stdout, "\033[33m[%s LOGW]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
16 | #define HONLY_LOGE(...) { fprintf (stdout, "\033[31m[%s LOGE]: %s: %s:%d - ❌ | ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
17 |
18 | #endif
--------------------------------------------------------------------------------
/dslings/en/cpp11/14-type-alias-3.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/14-type-alias-3.cpp
4 | //
5 | // Exercise: cpp11 | 14 - type alias | Standard Library Style Alias Templates
6 | //
7 | // Tips: Use alias templates to implement standard library style _t/_v templates
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // Discussion Forum: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker Command:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 | #include
22 |
23 | int main() {
24 |
25 | template
26 | D2X_YOUR_ANSWER my_add_pointer_t = typename std::add_pointer;
27 |
28 | int c = 20;
29 | my_add_pointer_t ptr = &c;
30 |
31 | bool ok = std::is_same, int*>::value;
32 |
33 | d2x_assert(ok);
34 | d2x_assert_eq(*ptr, 20);
35 |
36 | D2X_WAIT
37 |
38 | return 0;
39 | }
--------------------------------------------------------------------------------
/videos/d2x/video.py:
--------------------------------------------------------------------------------
1 | from manim import *
2 |
3 | def mcpp_video_start(scene, target_title):
4 |
5 | title = Text(target_title)
6 | logo = VGroup(
7 | Tex(r"\textit{\underline{mcpp-standard}}"),
8 | Tex(r"\textit{+}", color=RED)
9 | ).arrange(RIGHT, buff=0.1).scale(0.8)
10 |
11 | scene.play(Write(logo), run_time=0.8)
12 | scene.bring_to_front(logo)
13 |
14 | scene.play(
15 | logo.animate.to_corner(UP + LEFT),
16 | FadeIn(title),
17 | )
18 |
19 | return title, logo
20 |
21 | def mcpp_video_end(scene, logo, obj_group=VGroup()):
22 |
23 | scene.play(
24 | logo.animate.move_to(ORIGIN),
25 | FadeOut(obj_group),
26 | run_time=0.7
27 | )
28 |
29 | scene.wait(0.5)
30 |
31 | ending = VGroup(
32 | Text("开源交互式教程", color=RED).scale(0.9),
33 | Tex(r"\textit{\underline{https://github.com/Sunrisepeak/mcpp-standard}}"),
34 | ).arrange(DOWN, buff=0.15).scale(0.8)
35 |
36 | scene.play(
37 | ReplacementTransform(logo[1], ending[0]),
38 | ReplacementTransform(logo[0], ending[1]),
39 | )
40 |
41 | scene.play(FadeOut(ending), run_time = 3)
--------------------------------------------------------------------------------
/dslings/en/cpp11/01-default-and-delete-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-0.cpp
4 | //
5 | // Exercise: cpp11 | 01 - default and delete | Explicitly specifying constructor generation behavior
6 | //
7 | // Tips: Use `= default` and `= delete` to fix errors based on compiler hints
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker default-and-delete
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | // default and delete explicitly control -> compiler default constructor generation behavior
23 | struct A { };
24 | struct B {
25 | B(int x) { std::cout << "B(int x)" << std::endl; }
26 | };
27 | struct C {
28 | C() { }
29 | C(int x = 1) { std::cout << "C(int x = 1)" << std::endl; }
30 | };
31 |
32 | int main() { // Do not directly modify the code in the main function
33 |
34 | A a;
35 | B b;
36 | C c(1);
37 |
38 | D2X_WAIT
39 |
40 | return 0;
41 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/09-list-initialization-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-1.cpp
4 | //
5 | // Exercise: cpp11 | 09 - list initialization | Default initialization syntax pitfalls
6 | //
7 | // Tips: Fix compiler errors based on compiler output, understand default initialization syntax pitfalls
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker list-initialization
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | struct Object {
23 | Object() {
24 | x = 0;
25 | std::cout << "Object()" << std::endl;
26 | }
27 | Object(int x) : x(x) {
28 | std::cout << "Object(int): " << x << std::endl;
29 | }
30 | int x;
31 | };
32 |
33 | int main() {
34 |
35 | Object obj1();
36 | Object obj2(2);
37 |
38 | d2x_assert_eq(obj1.x, 0);
39 | d2x_assert_eq(obj2.x, 2);
40 |
41 | D2X_WAIT
42 |
43 | return 0;
44 | }
--------------------------------------------------------------------------------
/dslings/cpp11/03-trailing-return-type.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/03-trailing-return-type.cpp
4 | //
5 | // Exercise/练习: cpp11 | 03 - trailing return type
6 | //
7 | // Tips/提示: 替换D2X_YOUR_ANSWER为正确的类型, 通过所有检查
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_declaration
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker trailing-return-type
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | int add0(double a, int b) {
22 | return a + b;
23 | }
24 |
25 | auto add1(double a, int b) -> int {
26 | return a + b;
27 | }
28 |
29 | template
30 | auto add2(const T1 &a, const T2 &b) -> D2X_YOUR_ANSWER {
31 | return a + b;
32 | }
33 |
34 | auto add3 = [](double a, double b) -> D2X_YOUR_ANSWER {
35 | return a + b;
36 | };
37 |
38 | int main() {
39 |
40 | d2x_assert_eq(add0(1.1, 2), 3);
41 | d2x_assert_eq(add1(1.1, 2), 3);
42 | d2x_assert_eq(add2(1.1, 2), 3.1);
43 | d2x_assert_eq(add2(1, 2.1), 3.1);
44 | d2x_assert_eq(add3(1.1, 2.1), 3);
45 |
46 | D2X_WAIT
47 |
48 | return 0;
49 | }
--------------------------------------------------------------------------------
/dslings/cpp11/00-auto-and-decltype-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 00 - auto and decltype | 自动类型推导
6 | //
7 | // Tips/提示: 使用 auto 和 decltype 修复代码中的错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // 练习交流讨论: http://forum.d2learn.org/post/357
15 | //
16 | // Auto-Checker/自动检测命令:
17 | //
18 | // d2x checker auto-and-decltype
19 | //
20 |
21 | #include
22 |
23 | int main() {
24 |
25 | // 0. 声明定义
26 | int a = 1;
27 | auto a1 = a; // a1 的类型是 int
28 | int b = 2;
29 | D2X_YOUR_ANSWER b1 = b;
30 |
31 | decltype(b) b2 = b; // b2 的类型是 int
32 | D2X_YOUR_ANSWER a2 = a;
33 |
34 | char c = 'c';
35 | D2X_YOUR_ANSWER c1 = c;
36 | D2X_YOUR_ANSWER c2 = c;
37 |
38 | d2x_assert_eq(a, a1);
39 | d2x_assert_eq(a1, a2);
40 | d2x_assert_eq(b, b1);
41 | d2x_assert_eq(b1, b2);
42 |
43 | D2X_WAIT
44 |
45 | return 0;
46 | }
--------------------------------------------------------------------------------
/dslings/d2x/common.lua:
--------------------------------------------------------------------------------
1 | function get_local_lang()
2 | --local config = platform.get_config_info()
3 | local local_lang = "en" -- Default language
4 | -- TODO: config language by xlings.json
5 | if is_host("linux") then
6 | local tmp_local_lang = os.getenv("LANG") or "en"
7 | if tmp_local_lang:find("zh") then
8 | local_lang = "zh"
9 | end
10 | elseif is_host("windows") then
11 | local tmp_local_lang = nil
12 | if find_tool("wmic") then -- Windows 10 and earlier
13 | -- wmic is deprecated in Windows 11, but still available
14 | tmp_local_lang = os.iorun([[wmic os get locale]])
15 | else -- win11+
16 | tmp_local_lang = os.iorun([[powershell -NoProfile -ExecutionPolicy Bypass -Command "'{0:X4}' -f (Get-Culture).LCID"]])
17 | end
18 | if tmp_local_lang and tmp_local_lang:find("0804") then
19 | local_lang = "zh"
20 | end
21 | elseif is_host("macosx") then
22 | local tmp_local_lang = os.iorun([[defaults read -g AppleLocale]])
23 | if tmp_local_lang and tmp_local_lang:find("zh") then
24 | local_lang = "zh"
25 | end
26 | end
27 |
28 | return local_lang
29 | end
30 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/07-constexpr-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/07-constexpr-0.cpp
4 | //
5 | // Exercise: cpp11 | 07 - constexpr | Compile-time computation basics: Difference between constexpr and const
6 | //
7 | // Tips: Fix compiler errors and understand compile-time variables and compile-time function computation
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/constexpr
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker constexpr
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | int sum_for_1_to(int n) {
22 | return n == 1 ? 1 : n + sum_for_1_to(n - 1);
23 | }
24 |
25 | int main() {
26 |
27 | { // 1. Runtime variables, constants, and compile-time variables
28 | int size1 = 10;
29 | const int size2 = size1 + 10;
30 | constexpr int size3 = 10 * 3;
31 |
32 | int arr1[size1]; // Choose the correct sizex as array size
33 | }
34 |
35 | { // 2. Compile-time computation basics
36 | constexpr int s = sum_for_1_to(4);
37 | d2x_assert_eq(s, 1 + 2 + 3 + 4);
38 | }
39 |
40 | D2X_WAIT
41 |
42 | return 0;
43 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/03-trailing-return-type.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/03-trailing-return-type.cpp
4 | //
5 | // Exercise: cpp11 | 03 - trailing return type
6 | //
7 | // Tips: Replace D2X_YOUR_ANSWER with the correct type to pass all checks
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_declaration
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker trailing-return-type
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | int add0(double a, int b) {
22 | return a + b;
23 | }
24 |
25 | auto add1(double a, int b) -> int {
26 | return a + b;
27 | }
28 |
29 | template
30 | auto add2(const T1 &a, const T2 &b) -> D2X_YOUR_ANSWER {
31 | return a + b;
32 | }
33 |
34 | auto add3 = [](double a, double b) -> D2X_YOUR_ANSWER {
35 | return a + b;
36 | };
37 |
38 | int main() {
39 |
40 | d2x_assert_eq(add0(1.1, 2), 3);
41 | d2x_assert_eq(add1(1.1, 2), 3);
42 | d2x_assert_eq(add2(1.1, 2), 3.1);
43 | d2x_assert_eq(add2(1, 2.1), 3.1);
44 | d2x_assert_eq(add3(1.1, 2.1), 3);
45 |
46 | D2X_WAIT
47 |
48 | return 0;
49 | }
--------------------------------------------------------------------------------
/dslings/cpp11/00-auto-and-decltype-3.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-3.cpp
4 | //
5 | // Exercise/练习: cpp11 | 00 - auto and decltype | 函数返回值类型推导
6 | //
7 | // Tips/提示: 使用 auto 和 decltype 修复代码中的错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // 练习交流讨论: http://forum.d2learn.org/post/357
15 | //
16 | // Auto-Checker/自动检测命令:
17 | //
18 | // d2x checker auto-and-decltype-3
19 | //
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | // 3. 函数返回值类型
27 |
28 | auto add_func(int a, double b) -> decltype(a + b) {
29 | return a + b;
30 | }
31 |
32 | template
33 | D2X_YOUR_ANSWER minus_func(T1 a, T2 b) -> D2X_YOUR_ANSWER {
34 | return a - b;
35 | }
36 |
37 | int main() {
38 |
39 | d2x_assert_eq(minus_func(1, 2), -1);
40 | d2x_assert_eq(minus_func(2, 1), 1);
41 | d2x_assert_eq(minus_func(1, 2.1), -1.1);
42 |
43 | D2X_WAIT
44 |
45 | return 0;
46 | }
--------------------------------------------------------------------------------
/dslings/cpp11/14-type-alias-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/14-type-alias-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 14 - type alias | 基本类型别名
6 | //
7 | // Tips/提示: 使用using关键字定义类型别名,替换传统的typedef
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. 基本类型别名定义
27 | D2X_YOUR_ANSWER Integer = int;
28 | D2X_YOUR_ANSWER = int;
29 |
30 | bool ok = std::is_same::value; d2x_assert(ok);
31 | ok = std::is_same::value; d2x_assert(ok);
32 |
33 | // 2. 使用类型别名
34 | Integer a = 42;
35 | Real b = 3.14;
36 |
37 | // 3. 验证类型别名
38 | d2x_assert_eq(a, 42);
39 | d2x_assert_eq(b, 3.14);
40 |
41 | // 4. 类型别名本质相同
42 | int c = 100;
43 | Integer d = c; // 可以赋值,因为本质都是int
44 |
45 | d2x_assert_eq(c, d);
46 |
47 | D2X_WAIT
48 |
49 | return 0;
50 | }
51 |
--------------------------------------------------------------------------------
/dslings/d2x/common.hpp:
--------------------------------------------------------------------------------
1 | #ifndef COMMON_HPP_D2X
2 | #define COMMON_HPP_D2X
3 |
4 | #include
5 | #include
6 |
7 | #include
8 |
9 | #define d2x_assert(expr) \
10 | { \
11 | if (!(expr)) { \
12 | HONLY_LOGW("❌(error) | %s", #expr); \
13 | } else { \
14 | HONLY_LOGI_P("✅ | %s", #expr); \
15 | } \
16 | }
17 |
18 | #define d2x_assert_eq(a, b) \
19 | { \
20 | if (a != b) {\
21 | HONLY_LOGW("❌ | %s == %s (%s == %s)", \
22 | #a, #b, std::to_string(a).c_str(), std::to_string(b).c_str()); \
23 | } else {\
24 | HONLY_LOGI_P("✅ | %s == %s (%s == %s)", \
25 | #a, #b, std::to_string(a).c_str(), std::to_string(b).c_str()); \
26 | } \
27 | }
28 |
29 | #define D2X_WAIT HONLY_LOGW("🥳 Delete the D2X_WAIT to continue...");
30 | #define D2X_YOUR_ANSWER
31 | #define D2X_DONT_DELETE_THIS(x) x
32 |
33 | template
34 | class d2x_is_invocable {
35 | private:
36 | template
37 | static auto test(U* f) -> decltype((*f)(std::declval()...), std::true_type());
38 |
39 | template
40 | static std::false_type test(...);
41 |
42 | public:
43 | static constexpr bool value = decltype(test(nullptr))::value;
44 | };
45 |
46 | #endif
--------------------------------------------------------------------------------
/dslings/cpp11/00-auto-and-decltype-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 00 - auto and decltype | 表达式类型推导
6 | //
7 | // Tips/提示: 使用 auto 和 decltype 修复代码中的错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // 练习交流讨论: http://forum.d2learn.org/post/357
15 | //
16 | // Auto-Checker/自动检测命令:
17 | //
18 | // d2x checker auto-and-decltype-1
19 | //
20 |
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. 表达式
27 | int a = 1;
28 | auto a1 = a + 2;
29 | D2X_YOUR_ANSWER a2 = a + 2 + 1.1;
30 |
31 | int b = 2;
32 | D2X_YOUR_ANSWER b1 = a + 0.1;
33 | decltype(a + b + 1.1) b2 = a + b + 1.1;
34 |
35 | char c = 'c';
36 | D2X_YOUR_ANSWER c1 = 1 + c;
37 | D2X_YOUR_ANSWER c2 = 2 + 'a';
38 |
39 | d2x_assert_eq(a2, a + 2 + 1.1);
40 | d2x_assert_eq(b1, a + 0.1);
41 | d2x_assert_eq(c1, 1 + c);
42 | d2x_assert_eq(c2, 2 + 'a');
43 |
44 | D2X_WAIT
45 |
46 | return 0;
47 | }
--------------------------------------------------------------------------------
/dslings/cpp11/02-final-and-override-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/02-final-and-override-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 02 - final and override
6 | //
7 | // Tips/提示: 修正代码中final的使用错误问题
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/final
11 | // - https://en.cppreference.com/w/cpp/language/override
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker final-and-override-1
16 | //
17 |
18 | #include
19 |
20 | #include
21 | #include
22 |
23 | struct A {
24 | virtual int func1() {
25 | return 1;
26 | }
27 | int func2() { return 2; }
28 | };
29 |
30 | struct B : A {
31 |
32 | int func1() {
33 | return 3;
34 | }
35 |
36 | int func2() {
37 | return 4;
38 | }
39 | };
40 |
41 | struct C : B {
42 |
43 | };
44 |
45 | int main() {
46 |
47 | B final; // 不要直接修改main函数中的代码
48 | d2x_assert_eq(final.func1(), 3); // B::func1()
49 | d2x_assert_eq(final.func2(), 4); // B::func2()
50 |
51 | A *a = &final;
52 | d2x_assert_eq(a->func1(), 3); // B::func1()
53 | d2x_assert_eq(a->func2(), 2); // A::func2()
54 |
55 | D2X_WAIT
56 |
57 | return 0;
58 | }
--------------------------------------------------------------------------------
/book/src/base/chapter_0.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ./chapter_0.html
7 | [English]: ../en/base/chapter_0.html
8 |
9 | # 序言
10 |
11 | mcpp-standard是一个开源、强调代码实践的`现代C++核心语言特性`教程项目。项目的总体结构为[Book + Video + Code + X]。为使用者提供了 在线电子书、对应的讲解视频、配套练习代码, 同时也提供了用于讨论交流的论坛和定期的学习活动...
12 |
13 | - [Book: 在线电子书](https://sunrisepeak.github.io/mcpp-standard)
14 | - [Video: 讲解视频](https://space.bilibili.com/65858958/lists/5208246?type=season)
15 | - [Code: 练习代码](https://github.com/Sunrisepeak/mcpp-standard/tree/main/dslings)
16 | - [X: mcpp论坛](https://forum.d2learn.org/category/20)
17 |
18 | ## 语言支持
19 |
20 | | 中文 | English | 仓库 |
21 | | --- | --- | --- |
22 | | [中文](./) | [English](../en/base/chapter_0.html) | [Github](https://github.com/Sunrisepeak/mcpp-standard) |
23 |
24 | ## 活动 | [ 📣 MSCP - mcpp项目学习与贡献者培养计划 ](https://moga.d2learn.org/activity/mscp/intro.html)
25 |
26 | > MSCP是一款基于mcpp-standard开源项目开发的"地球Online"风格的角色扮演游戏。在游戏中你将扮演一个"编程初学者", 为了入门"现代C++"并揭露其背后的真相, 踏上了一条充满挑战和惊奇的现代C++学习之路...
27 |
28 | - `价格:` 免费
29 | - `开发者:` [Sunrisepeak](https://github.com/Sunrisepeak)
30 | - `发行商:` [MOGA](https://moga.d2learn.org)
31 | - `发行时间:` 2025年10月
32 | - `游戏体量:` 100H - 200H之间
33 | - `标签:` 类魂系列、模拟人生、🌍Online、程序员、C++、开源、费曼学习法
34 | - [-> 游戏详情](https://moga.d2learn.org/activity/mscp/intro.html)
35 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/00-auto-and-decltype-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-0.cpp
4 | //
5 | // Exercise: cpp11 | 00 - auto and decltype | Automatic Type Deduction
6 | //
7 | // Tips: Use auto and decltype to fix errors in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // Forum: https://forum.d2learn.org/category/20
15 | //
16 | // Auto-Checker command:
17 | //
18 | // d2x checker auto-and-decltype
19 | //
20 |
21 | #include
22 |
23 | int main() {
24 |
25 | // 0. Declaration and definition
26 | int a = 1;
27 | auto a1 = a; // a1's type is int
28 | int b = 2;
29 | D2X_YOUR_ANSWER b1 = b;
30 |
31 | decltype(b) b2 = b; // b2's type is int
32 | D2X_YOUR_ANSWER a2 = a;
33 |
34 | char c = 'c';
35 | D2X_YOUR_ANSWER c1 = c;
36 | D2X_YOUR_ANSWER c2 = c;
37 |
38 | d2x_assert_eq(a, a1);
39 | d2x_assert_eq(a1, a2);
40 | d2x_assert_eq(b, b1);
41 | d2x_assert_eq(b1, b2);
42 |
43 | D2X_WAIT
44 |
45 | return 0;
46 | }
47 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/00-auto-and-decltype-3.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-3.cpp
4 | //
5 | // Exercise: cpp11 | 00 - auto and decltype | Function Return Type Deduction
6 | //
7 | // Tips: Use auto and decltype to fix errors in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // Forum: https://forum.d2learn.org/category/20
15 | //
16 | // Auto-Checker command:
17 | //
18 | // d2x checker auto-and-decltype-3
19 | //
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | // 3. Function return types
27 |
28 | auto add_func(int a, double b) -> decltype(a + b) {
29 | return a + b;
30 | }
31 |
32 | template
33 | D2X_YOUR_ANSWER minus_func(T1 a, T2 b) -> D2X_YOUR_ANSWER {
34 | return a - b;
35 | }
36 |
37 | int main() {
38 |
39 | d2x_assert_eq(minus_func(1, 2), -1);
40 | d2x_assert_eq(minus_func(2, 1), 1);
41 | d2x_assert_eq(minus_func(1, 2.1), -1.1);
42 |
43 | D2X_WAIT
44 |
45 | return 0;
46 | }
47 |
--------------------------------------------------------------------------------
/dslings/cpp11/09-list-initialization-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 09 - list initialization | 窄化检查
6 | //
7 | // Tips/提示: 根据编译器的输出, 修复编译器报错, 了解列表初始化的窄化检查
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker list-initialization
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | int main() {
23 |
24 | int a1 = 1.1;
25 | d2x_assert_eq(a1, 1);
26 |
27 | int a2 = 1.1;
28 | int a3 = { 1.1 };
29 | d2x_assert_eq(a2, 1);
30 | d2x_assert_eq(a3, 1);
31 |
32 | double b1 { 1.1 };
33 | constexpr double c1 { 2.2 };
34 |
35 | int b2 { b1 };
36 | int c2 { c1 };
37 |
38 | d2x_assert_eq(b2, 1);
39 | d2x_assert_eq(c2, 2);
40 |
41 | int arr1[] = { 1, 2.2, 3 };
42 | d2x_assert_eq(arr1[1], 2);
43 |
44 | int arr2[4] { 1, b1, c1 };
45 | d2x_assert_eq(arr2[1], 1);
46 | d2x_assert_eq(arr2[2], 2);
47 | d2x_assert_eq(arr2[3], 0);
48 |
49 | D2X_WAIT
50 |
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/00-auto-and-decltype-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-1.cpp
4 | //
5 | // Exercise: cpp11 | 00 - auto and decltype | Expression Type Deduction
6 | //
7 | // Tips: Use auto and decltype to fix errors in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // Forum: https://forum.d2learn.org/category/20
15 | //
16 | // Auto-Checker command:
17 | //
18 | // d2x checker auto-and-decltype-1
19 | //
20 |
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. Expressions
27 | int a = 1;
28 | auto a1 = a + 2;
29 | D2X_YOUR_ANSWER a2 = a + 2 + 1.1;
30 |
31 | int b = 2;
32 | D2X_YOUR_ANSWER b1 = a + 0.1;
33 | decltype(a + b + 1.1) b2 = a + b + 1.1;
34 |
35 | char c = 'c';
36 | D2X_YOUR_ANSWER c1 = 1 + c;
37 | D2X_YOUR_ANSWER c2 = 2 + 'a';
38 |
39 | d2x_assert_eq(a2, a + 2 + 1.1);
40 | d2x_assert_eq(b1, a + 0.1);
41 | d2x_assert_eq(c1, 1 + c);
42 | d2x_assert_eq(c2, 2 + 'a');
43 |
44 | D2X_WAIT
45 |
46 | return 0;
47 | }
48 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/06-scoped-enums-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/06-scoped-enums-0.cpp
4 | //
5 | // Exercise: cpp11 | 06 - scoped enums | Traditional enum type potential issues
6 | //
7 | // Tips: Fix code through compiler error prompts and understand potential issues with traditional enum types
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/enum
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker scoped-enums
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | enum Color {
22 | RED,
23 | GREEN,
24 | BLUE,
25 | ORANGE // 1. Type conflict - Orange color
26 | };
27 |
28 | enum Fruit {
29 | Apple,
30 | Banana,
31 | ORANGE // 1. Type conflict - Orange fruit
32 | };
33 |
34 | int main() {
35 |
36 | Color color = RED;
37 | Fruit fruit = Apple;
38 |
39 | d2x_assert_eq(color, RED);
40 | d2x_assert_eq(fruit, Apple);
41 |
42 | // 2. Syntactically correct, but logically wrong type matching
43 | if (color == Apple) { // Do not delete this line of code
44 | // Code will run here
45 | D2X_WAIT
46 | }
47 |
48 | if (fruit == RED) {
49 | D2X_WAIT
50 | }
51 |
52 | D2X_WAIT
53 |
54 | return 0;
55 | }
--------------------------------------------------------------------------------
/dslings/cpp11/02-final-and-override-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/02-final-and-override-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 02 - final and override
6 | //
7 | // Tips/提示: 修正代码中override的使用错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/final
11 | // - https://en.cppreference.com/w/cpp/language/override
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker final-and-override
16 | //
17 |
18 | #include
19 |
20 | #include
21 | #include
22 |
23 | struct A {
24 | virtual void func1() {
25 | std::cout << "A::func1()" << std::endl;
26 | }
27 |
28 | void func2() {
29 | std::cout << "A::func2()" << std::endl;
30 | }
31 | };
32 |
33 | struct B : A {
34 | void func1() {
35 | std::cout << "B::func1()" << std::endl;
36 | }
37 |
38 | void func2() override {
39 | std::cout << "B::func2()" << std::endl;
40 | }
41 | };
42 |
43 |
44 | int main() {
45 |
46 | B override; // 不要直接修改main函数中的代码
47 | override.func1(); // B::func1()
48 | override.func2(); // B::func2()
49 |
50 | A *a = &override;
51 | a->func1(); // B::func1()
52 | a->func2(); // A::func2()
53 |
54 | D2X_WAIT
55 |
56 | return 0;
57 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/02-final-and-override-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/02-final-and-override-1.cpp
4 | //
5 | // Exercise: cpp11 | 02 - final and override
6 | //
7 | // Tips: Correct the usage errors of final in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/final
11 | // - https://en.cppreference.com/w/cpp/language/override
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker final-and-override-1
16 | //
17 |
18 | #include
19 |
20 | #include
21 | #include
22 |
23 | struct A {
24 | virtual int func1() {
25 | return 1;
26 | }
27 | int func2() { return 2; }
28 | };
29 |
30 | struct B : A {
31 |
32 | int func1() {
33 | return 3;
34 | }
35 |
36 | int func2() {
37 | return 4;
38 | }
39 | };
40 |
41 | struct C : B {
42 |
43 | };
44 |
45 | int main() {
46 |
47 | B final; // Do not directly modify the code in the main function
48 | d2x_assert_eq(final.func1(), 3); // B::func1()
49 | d2x_assert_eq(final.func2(), 4); // B::func2()
50 |
51 | A *a = &final;
52 | d2x_assert_eq(a->func1(), 3); // B::func1()
53 | d2x_assert_eq(a->func2(), 2); // A::func2()
54 |
55 | D2X_WAIT
56 |
57 | return 0;
58 | }
--------------------------------------------------------------------------------
/dslings/cpp11/12-nullptr-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/12-nullptr-2.cpp
4 | //
5 | // Exercise/练习: cpp11 | 12 - nullptr | 指针字面量 - 模板编程
6 | //
7 | // Tips/提示: 使用 nullptr 在模板编程中提供更好的类型安全性
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/12-nullptr.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 | #include
22 |
23 | // 模板函数示例
24 | template
25 | void processPointer(T* ptr) { }
26 |
27 | // 模板函数,返回传入值的副本
28 | template
29 | T clone(const T& t) {
30 | return t;
31 | }
32 |
33 | int main() {
34 |
35 | // 1. nullptr 在模板中的类型推导
36 | auto ptr1 = nullptr; // ptr1 的类型是 std::nullptr_t
37 | static_assert(std::is_same::value, "类型推导错误");
38 |
39 | // 2. nullptr 在模板函数中的优势 - 类型推导安全
40 | // 观察编译器报错, 尝试显式指定模板参数解决报错
41 | processPointer(clone(0));
42 | processPointer(clone(NULL));
43 |
44 | processPointer(clone(nullptr));
45 | processPointer(clone(nullptr));
46 |
47 | D2X_WAIT
48 |
49 | return 0;
50 | }
51 |
--------------------------------------------------------------------------------
/dslings/cpp11/13-long-long-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/13-long-long-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 13 - long long | 64位整数类型 - 基础用法
6 | //
7 | // Tips/提示: 使用 long long 类型和对应的字面量后缀
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/types
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/13-long-long.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker long-long
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. 基本声明和初始化
27 | D2X_YOUR_ANSWER val1 = 1;
28 | D2X_YOUR_ANSWER val2 = -1;
29 |
30 | // 2. 整数表示范围
31 | unsigned int uVal1 = 18446744073709551615;
32 | d2x_assert_eq(uVal1, 18446744073709551615ULL);
33 |
34 | // 3. 类型推导和字面量
35 | // 修复下面的声明,让 auto 正确推导类型
36 | auto longlong = 1234567890;
37 | auto ulonglong = 9876543210;
38 |
39 | bool is_longlong = std::is_same::value;
40 | bool is_ulonglong = std::is_same::value;
41 |
42 | d2x_assert(is_longlong == true);
43 | d2x_assert(is_ulonglong == true);
44 |
45 | D2X_WAIT
46 |
47 | return 0;
48 | }
49 |
--------------------------------------------------------------------------------
/dslings/cpp11/13-long-long-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/13-long-long-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 13 - long long | 64位整数类型 - 大数应用和边界值
6 | //
7 | // Tips/提示: 使用 long long 处理大整数和边界值计算
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/types
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/13-long-long.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker long-long
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. 获取整数类型的边界值
27 | auto maxInt = std::numeric_limits::max();
28 | auto maxLL = std::numeric_limits::max();
29 | auto minLL = std::numeric_limits::min();
30 | auto maxULL = std::numeric_limits::max();
31 |
32 | d2x_assert_eq(maxInt, 2147483647);
33 | d2x_assert_eq(maxLL, 9223372036854775807LL);
34 | d2x_assert_eq(minLL, -9223372036854775807LL - 1);
35 | d2x_assert_eq(maxULL, 18446744073709551615ULL);
36 |
37 | // 2. 大整数应用 - 表示世界人口
38 | int currentPopulation = 7800000000;
39 | d2x_assert_eq(currentPopulation, 7800000000ULL);
40 |
41 | D2X_WAIT
42 |
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/09-list-initialization-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-0.cpp
4 | //
5 | // Exercise: cpp11 | 09 - list initialization | Narrowing checks
6 | //
7 | // Tips: Fix compiler errors based on compiler output, understand narrowing checks in list initialization
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker list-initialization
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | int main() {
23 |
24 | int a1 = 1.1;
25 | d2x_assert_eq(a1, 1);
26 |
27 | int a2 = 1.1;
28 | int a3 = { 1.1 };
29 | d2x_assert_eq(a2, 1);
30 | d2x_assert_eq(a3, 1);
31 |
32 | double b1 { 1.1 };
33 | constexpr double c1 { 2.2 };
34 |
35 | int b2 { b1 };
36 | int c2 { c1 };
37 |
38 | d2x_assert_eq(b2, 1);
39 | d2x_assert_eq(c2, 2);
40 |
41 | int arr1[] = { 1, 2.2, 3 };
42 | d2x_assert_eq(arr1[1], 2);
43 |
44 | int arr2[4] { 1, b1, c1 };
45 | d2x_assert_eq(arr2[1], 1);
46 | d2x_assert_eq(arr2[2], 2);
47 | d2x_assert_eq(arr2[3], 0);
48 |
49 | D2X_WAIT
50 |
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/02-final-and-override-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/02-final-and-override-0.cpp
4 | //
5 | // Exercise: cpp11 | 02 - final and override
6 | //
7 | // Tips: Correct the usage errors of override in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/final
11 | // - https://en.cppreference.com/w/cpp/language/override
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker final-and-override
16 | //
17 |
18 | #include
19 |
20 | #include
21 | #include
22 |
23 | struct A {
24 | virtual void func1() {
25 | std::cout << "A::func1()" << std::endl;
26 | }
27 |
28 | void func2() {
29 | std::cout << "A::func2()" << std::endl;
30 | }
31 | };
32 |
33 | struct B : A {
34 | void func1() {
35 | std::cout << "B::func1()" << std::endl;
36 | }
37 |
38 | void func2() override {
39 | std::cout << "B::func2()" << std::endl;
40 | }
41 | };
42 |
43 |
44 | int main() {
45 |
46 | B override; // Do not directly modify the code in the main function
47 | override.func1(); // B::func1()
48 | override.func2(); // B::func2()
49 |
50 | A *a = &override;
51 | a->func1(); // B::func1()
52 | a->func2(); // A::func2()
53 |
54 | D2X_WAIT
55 |
56 | return 0;
57 | }
--------------------------------------------------------------------------------
/tools/update_upstream.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # 脚本名称:update_upstream.sh
4 |
5 | set -euo pipefail
6 |
7 | UPSTREAM_URL="https://github.com/Sunrisepeak/mcpp-standard.git"
8 | BRANCH_NAME="main"
9 |
10 | echo "[1] - 检查是否已添加 upstream 远程..."
11 | if ! git remote get-url upstream &> /dev/null; then
12 | echo "添加 upstream: $UPSTREAM_URL"
13 | git remote add upstream "$UPSTREAM_URL"
14 | else
15 | echo "✅ upstream 已存在"
16 | fi
17 |
18 | echo "[2] - 获取 upstream 更新..."
19 | git fetch upstream
20 |
21 | echo "[3] - 切换到 $BRANCH_NAME 分支..."
22 | git checkout $BRANCH_NAME
23 |
24 |
25 | echo "[4] - 检测到本地改动,自动备份中..."
26 |
27 | TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
28 | BACKUP_BRANCH="solutions-$TIMESTAMP"
29 |
30 | echo "🔀 创建备份分支: $BACKUP_BRANCH"
31 | git checkout -b "$BACKUP_BRANCH"
32 |
33 | # 判断是否有工作区改动
34 | HAS_UNSTAGED_CHANGES=false
35 | if ! git diff --quiet || ! git diff --cached --quiet; then
36 | HAS_UNSTAGED_CHANGES=true
37 | fi
38 |
39 | if $HAS_UNSTAGED_CHANGES; then
40 | echo "📦 提交工作目录的未提交改动"
41 | git add .
42 | git commit -m "🧶 自动备份:本地改动 - $TIMESTAMP"
43 | fi
44 |
45 | echo "✅ 本地改动已保存到 $BACKUP_BRANCH 分支"
46 |
47 | # 回到 main 分支准备 rebase
48 | git checkout $BRANCH_NAME
49 |
50 | echo "[5] - 使用 rebase 同步 upstream/$BRANCH_NAME 到本地 $BRANCH_NAME..."
51 | if ! git rebase upstream/$BRANCH_NAME; then
52 | echo "❌ 出现冲突,请手动解决后完成 rebase"
53 | exit 1
54 | fi
55 |
56 | echo "✅ 更新完成!"
--------------------------------------------------------------------------------
/dslings/cpp11/09-list-initialization-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-2.cpp
4 | //
5 | // Exercise/练习: cpp11 | 09 - list initialization | 容器列表初始化
6 | //
7 | // Tips/提示: 实现一个支持列表初始化的构造函数, 计算容器大小, 了解initializer_list并通过运行时检查
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | // - https://en.cppreference.com/w/cpp/utility/initializer_list.html
13 | //
14 | // Auto-Checker/自动检测命令:
15 | //
16 | // d2x checker list-initialization
17 | //
18 |
19 | #include
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | class MyVector {
26 | int mSize;
27 |
28 | public:
29 | D2X_YOUR_ANSWER
30 |
31 | int size() const {
32 | return mSize;
33 | }
34 | };
35 |
36 | int main() {
37 |
38 | std::vector vec1 = { 1, 2, 3 };
39 | d2x_assert_eq(vec1.size(), 3);
40 | std::vector vec2 { 1, 2, 3, 4, 5 };
41 | d2x_assert_eq(vec2.size(), 5);
42 |
43 | MyVector myVec1 = { 1, 2, 3 };
44 | d2x_assert_eq(myVec1.size(), 3);
45 | MyVector myVec2 { 1, 2, 3, 4, 5 };
46 | d2x_assert_eq(myVec2.size(), 5);
47 |
48 | D2X_WAIT
49 |
50 | return 0;
51 | }
--------------------------------------------------------------------------------
/dslings/cpp11/12-nullptr-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/12-nullptr-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 12 - nullptr | 指针字面量 - 函数重载
6 | //
7 | // Tips/提示: 使用 nullptr 解决函数重载中的歧义问题
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/12-nullptr.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 |
22 | bool process_int_called = false;
23 | bool process_ptr_called = false;
24 | bool display_int_called = false;
25 | bool display_ptr_called = false;
26 |
27 | void process(int* ptr) {
28 | process_ptr_called = true;
29 | }
30 |
31 | void process(int value) {
32 | process_int_called = true;
33 | }
34 |
35 | void display(int* ptr) {
36 | display_ptr_called = true;
37 | }
38 |
39 | void display(int value) {
40 | display_int_called = true;
41 | }
42 |
43 | int main() {
44 |
45 | display(0);
46 | process(nullptr);
47 | display(NULL);
48 | process(NULL);
49 |
50 | d2x_assert(process_int_called);
51 | d2x_assert(display_int_called);
52 | d2x_assert(display_ptr_called);
53 | d2x_assert(display_ptr_called);
54 |
55 | D2X_WAIT
56 |
57 | return 0;
58 | }
59 |
--------------------------------------------------------------------------------
/dslings/cpp11/14-type-alias-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/14-type-alias-2.cpp
4 | //
5 | // Exercise/练习: cpp11 | 14 - type alias | 别名模板基础
6 | //
7 | // Tips/提示: 使用别名模板为模板类型创建别名
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 | #include
24 | #include
25 |
26 | // 1. 基本别名模板
27 | template
28 | D2X_YOUR_ANSWER Vec = std::vector;
29 |
30 | // 2. 固定长度的别名模板
31 | template
32 | using Vec3 = std::array;
33 |
34 | // 3. 带默认参数的别名模板
35 | template
36 | using Heap = std::priority_queue, Compare>;
37 |
38 | int main() {
39 |
40 | Vec numbers = {1, 2, 3};
41 | Vec3 v3 = {1.0f, 2.0f, 3.0f};
42 | Heap minHeap;
43 |
44 | d2x_assert_eq(numbers[0], 1);
45 | d2x_assert_eq(numbers[1], 2);
46 | d2x_assert_eq(numbers[2], 3);
47 |
48 | d2x_assert_eq(v3[0], 1.0f);
49 | d2x_assert_eq(v3[1], 2.0f);
50 | d2x_assert_eq(v3[2], 3.0f);
51 |
52 | D2X_WAIT
53 |
54 | return 0;
55 | }
56 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/14-type-alias-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/14-type-alias-0.cpp
4 | //
5 | // Exercise: cpp11 | 14 - type alias | Basic Type Aliases
6 | //
7 | // Tips: Use the using keyword to define type aliases, replacing traditional typedef
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // Discussion Forum: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker Command:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. Basic type alias definition
27 | D2X_YOUR_ANSWER Integer = int;
28 | D2X_YOUR_ANSWER = int;
29 |
30 | bool ok = std::is_same::value; d2x_assert(ok);
31 | ok = std::is_same::value; d2x_assert(ok);
32 |
33 | // 2. Using type aliases
34 | Integer a = 42;
35 | Real b = 3.14;
36 |
37 | // 3. Verifying type aliases
38 | d2x_assert_eq(a, 42);
39 | d2x_assert_eq(b, 3.14);
40 |
41 | // 4. Type aliases are essentially the same
42 | int c = 100;
43 | Integer d = c; // Can assign because they are essentially both int
44 |
45 | d2x_assert_eq(c, d);
46 |
47 | D2X_WAIT
48 |
49 | return 0;
50 | }
--------------------------------------------------------------------------------
/dslings/cpp11/01-default-and-delete-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 01 - default and delete | 不可拷贝对像
6 | //
7 | // Tips/提示: 根据编译器提示使用`= default`和`= delete`修复错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker default-and-delete-1
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | // 实现std::unique_ptr不可以拷贝, 但可以移动的属性
23 | struct UniquePtr {
24 | void *dataPtr;
25 | UniquePtr() = default;
26 | };
27 |
28 | int main() { // 不要直接修改main函数中的代码
29 |
30 | // std::unique_ptr a(new int(1));
31 | UniquePtr a;
32 |
33 | // 对像不可拷贝/复制
34 | // std::unique_ptr b = a; // error
35 | d2x_assert(std::is_copy_constructible::value == false);
36 | // a = b; // error
37 | d2x_assert(std::is_copy_assignable::value == false);
38 |
39 | // 对像可移动
40 | // std::unique_ptr c = std::move(a); // ok
41 | d2x_assert(std::is_move_constructible::value == true);
42 | // a = std::move(c); // ok
43 | d2x_assert(std::is_move_assignable::value == true);
44 |
45 | D2X_WAIT
46 |
47 | return 0;
48 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/12-nullptr-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/12-nullptr-1.cpp
4 | //
5 | // Exercise: cpp11 | 12 - nullptr | Pointer Literal - Function Overloading
6 | //
7 | // Tips: Use nullptr to resolve ambiguity in function overloading
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/cpp11/12-nullptr.md
12 | //
13 | // Exercise discussion: http://forum.d2learn.org/post/359
14 | //
15 | // Auto-Checker command:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 |
22 | bool process_int_called = false;
23 | bool process_ptr_called = false;
24 | bool display_int_called = false;
25 | bool display_ptr_called = false;
26 |
27 | void process(int* ptr) {
28 | process_ptr_called = true;
29 | }
30 |
31 | void process(int value) {
32 | process_int_called = true;
33 | }
34 |
35 | void display(int* ptr) {
36 | display_ptr_called = true;
37 | }
38 |
39 | void display(int value) {
40 | display_int_called = true;
41 | }
42 |
43 | int main() {
44 |
45 | display(0);
46 | process(nullptr);
47 | display(NULL);
48 | process(NULL);
49 |
50 | d2x_assert(process_int_called);
51 | d2x_assert(display_int_called);
52 | d2x_assert(display_ptr_called);
53 | d2x_assert(display_ptr_called);
54 |
55 | D2X_WAIT
56 |
57 | return 0;
58 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/09-list-initialization-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/09-list-initialization-2.cpp
4 | //
5 | // Exercise: cpp11 | 09 - list initialization | Container list initialization
6 | //
7 | // Tips: Implement a constructor that supports list initialization, calculate container size, understand initializer_list and pass runtime checks
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/list_initialization.html
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/09-list-initialization.md
12 | // - https://en.cppreference.com/w/cpp/utility/initializer_list.html
13 | //
14 | // Auto-Checker command:
15 | //
16 | // d2x checker list-initialization
17 | //
18 |
19 | #include
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | class MyVector {
26 | int mSize;
27 |
28 | public:
29 | D2X_YOUR_ANSWER
30 |
31 | int size() const {
32 | return mSize;
33 | }
34 | };
35 |
36 | int main() {
37 |
38 | std::vector vec1 = { 1, 2, 3 };
39 | d2x_assert_eq(vec1.size(), 3);
40 | std::vector vec2 { 1, 2, 3, 4, 5 };
41 | d2x_assert_eq(vec2.size(), 5);
42 |
43 | MyVector myVec1 = { 1, 2, 3 };
44 | d2x_assert_eq(myVec1.size(), 3);
45 | MyVector myVec2 { 1, 2, 3, 4, 5 };
46 | d2x_assert_eq(myVec2.size(), 5);
47 |
48 | D2X_WAIT
49 |
50 | return 0;
51 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/13-long-long-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/13-long-long-1.cpp
4 | //
5 | // Exercise: cpp11 | 13 - long long | 64-bit Integer Type - Large Number Applications and Boundary Values
6 | //
7 | // Tips: Use long long to handle large integers and boundary value calculations
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/types
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/src/cpp11/13-long-long.md
12 | //
13 | // Exercise discussion: http://forum.d2learn.org
14 | //
15 | // Auto-Checker command:
16 | //
17 | // d2x checker long-long
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. Get boundary values of integer types
27 | auto maxInt = std::numeric_limits::max();
28 | auto maxLL = std::numeric_limits::max();
29 | auto minLL = std::numeric_limits::min();
30 | auto maxULL = std::numeric_limits::max();
31 |
32 | d2x_assert_eq(maxInt, 2147483647);
33 | d2x_assert_eq(maxLL, 9223372036854775807LL);
34 | d2x_assert_eq(minLL, -9223372036854775807LL - 1);
35 | d2x_assert_eq(maxULL, 18446744073709551615ULL);
36 |
37 | // 2. Large integer application - representing world population
38 | int currentPopulation = 7800000000;
39 | d2x_assert_eq(currentPopulation, 7800000000ULL);
40 |
41 | D2X_WAIT
42 |
43 | return 0;
44 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/13-long-long-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/13-long-long-0.cpp
4 | //
5 | // Exercise: cpp11 | 13 - long long | 64-bit Integer Type - Basic Usage
6 | //
7 | // Tips: Use long long type and corresponding literal suffixes
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/types
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/src/cpp11/13-long-long.md
12 | //
13 | // Exercise discussion: http://forum.d2learn.org
14 | //
15 | // Auto-Checker command:
16 | //
17 | // d2x checker long-long
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. Basic declaration and initialization
27 | D2X_YOUR_ANSWER val1 = 1;
28 | D2X_YOUR_ANSWER val2 = -1;
29 |
30 | // 2. Integer representation range
31 | unsigned int uVal1 = 18446744073709551615;
32 | d2x_assert_eq(uVal1, 18446744073709551615ULL);
33 |
34 | // 3. Type deduction and literals
35 | // Fix the declarations below to let auto correctly deduce the types
36 | auto longlong = 1234567890;
37 | auto ulonglong = 9876543210;
38 |
39 | bool is_longlong = std::is_same::value;
40 | bool is_ulonglong = std::is_same::value;
41 |
42 | d2x_assert(is_longlong == true);
43 | d2x_assert(is_ulonglong == true);
44 |
45 | D2X_WAIT
46 |
47 | return 0;
48 | }
49 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/14-type-alias-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/14-type-alias-2.cpp
4 | //
5 | // Exercise: cpp11 | 14 - type alias | Alias Templates Basics
6 | //
7 | // Tips: Use alias templates to create aliases for template types
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // Discussion Forum: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker Command:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 | #include
24 | #include
25 |
26 | // 1. Basic alias template
27 | template
28 | D2X_YOUR_ANSWER Vec = std::vector;
29 |
30 | // 2. Fixed-length alias template
31 | template
32 | using Vec3 = std::array;
33 |
34 | // 3. Alias template with default parameters
35 | template
36 | using Heap = std::priority_queue, Compare>;
37 |
38 | int main() {
39 |
40 | Vec numbers = {1, 2, 3};
41 | Vec3 v3 = {1.0f, 2.0f, 3.0f};
42 | Heap minHeap;
43 |
44 | d2x_assert_eq(numbers[0], 1);
45 | d2x_assert_eq(numbers[1], 2);
46 | d2x_assert_eq(numbers[2], 3);
47 |
48 | d2x_assert_eq(v3[0], 1.0f);
49 | d2x_assert_eq(v3[1], 2.0f);
50 | d2x_assert_eq(v3[2], 3.0f);
51 |
52 | D2X_WAIT
53 |
54 | return 0;
55 | }
--------------------------------------------------------------------------------
/dslings/cpp11/14-type-alias-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/14-type-alias-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 14 - type alias | 复杂类型和函数指针别名
6 | //
7 | // Tips/提示: 使用using关键字为复杂类型和函数指针定义别名
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 | #include
24 |
25 | static int func_called = 0;
26 |
27 | // 函数声明
28 | void example_func(int a, int b) {
29 | func_called = a + b;
30 | }
31 |
32 | int main() {
33 |
34 | // 1. 函数指针别名
35 | D2X_YOUR_ANSWER FuncPtr = void(*)(int, int);
36 |
37 | FuncPtr func = example_func;
38 |
39 | func(1, 2);
40 | d2x_assert_eq(func_called, 3);
41 |
42 | // 2. 容器类型别名
43 | // 使用using定义vector的别名
44 | D2X_YOUR_ANSWER = std::vector;
45 |
46 | // 使用容器类型别名
47 | StringVector strings = {"hello", "world"};
48 |
49 | // 3. 嵌套类型别名
50 | struct Container {
51 | ValueType = int
52 | };
53 |
54 | Container::ValueType value = 100;
55 |
56 | // 4. 验证类型别名
57 | d2x_assert(strings[0] == "hello");
58 | d2x_assert(strings[1] == "world");
59 | d2x_assert_eq(value, 100);
60 |
61 | D2X_WAIT
62 |
63 | return 0;
64 | }
65 |
--------------------------------------------------------------------------------
/dslings/cpp11/12-nullptr-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/12-nullptr-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 12 - nullptr | 指针字面量 - 基础用法
6 | //
7 | // Tips/提示: 使用 nullptr 替代传统的 NULL 和 0 来初始化指针
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/12-nullptr.md
12 | //
13 | // 练习交流讨论: http://forum.d2learn.org
14 | //
15 | // Auto-Checker/自动检测命令:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. 初始化指针
27 | int* ptr1 = nullpt; // 推荐用法
28 | int* ptr2 = NULL; // 修复这里,添加正确类型
29 | int* ptr3 = 0; // 不推荐的传统用法
30 |
31 | d2x_assert(ptr1 == nullptr);
32 | d2x_assert(ptr2 == nullptr);
33 | d2x_assert(ptr3 == nullptr);
34 |
35 | // 2. nullptr的类型
36 | bool ok = std::is_same::value;
37 | d2x_assert(ok);
38 |
39 | // 3. 使用 nullptr 进行指针比较
40 | int value = 42;
41 | int* ptr4 = &value;
42 |
43 | if (ptr4 != nullptr) {
44 | *ptr4 = D2X_YOUR_ANSWER;
45 | d2x_assert_eq(*ptr4, 2233);
46 | }
47 |
48 | // 4. 不同类型的指针都可以使用 nullptr
49 | double* dptr = nullptr;
50 | char* cptr = nullptr
51 | void* vptr = nullptr;
52 |
53 | d2x_assert(dptr == nullptr);
54 | d2x_assert(cptr == nullptr);
55 | d2x_assert(vptr == nullptr);
56 |
57 | D2X_WAIT
58 |
59 | return 0;
60 | }
61 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/01-default-and-delete-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/01-default-and-delete-1.cpp
4 | //
5 | // Exercise: cpp11 | 01 - default and delete | Non-copyable objects
6 | //
7 | // Tips: Use `= default` and `= delete` to fix errors based on compiler hints
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/function#Function_definition
11 | // - https://en.cppreference.com/w/cpp/language/function#Deleted_functions
12 | //
13 | // Auto-Checker command:
14 | //
15 | // d2x checker default-and-delete-1
16 | //
17 |
18 | #include
19 |
20 | #include
21 |
22 | // Implement std::unique_ptr property: not copyable but movable
23 | struct UniquePtr {
24 | void *dataPtr;
25 | UniquePtr() = default;
26 | };
27 |
28 | int main() { // Do not directly modify the code in the main function
29 |
30 | // std::unique_ptr a(new int(1));
31 | UniquePtr a;
32 |
33 | // Object cannot be copied/duplicated
34 | // std::unique_ptr b = a; // error
35 | d2x_assert(std::is_copy_constructible::value == false);
36 | // a = b; // error
37 | d2x_assert(std::is_copy_assignable::value == false);
38 |
39 | // Object can be moved
40 | // std::unique_ptr c = std::move(a); // ok
41 | d2x_assert(std::is_move_constructible::value == true);
42 | // a = std::move(c); // ok
43 | d2x_assert(std::is_move_assignable::value == true);
44 |
45 | D2X_WAIT
46 |
47 | return 0;
48 | }
--------------------------------------------------------------------------------
/CLA.md:
--------------------------------------------------------------------------------
1 | # Contributor License Agreement (CLA)
2 |
3 | Thank you for your interest in contributing to this project!
4 |
5 | By submitting content (including code, documentation, translations, or any other material), you agree to the following terms:
6 |
7 | 1. You affirm that the contribution is your original work, or that you have received appropriate rights and permissions to submit it.
8 | 2. You retain the copyright to your contribution.
9 | 3. You grant the project maintainers a worldwide, perpetual, irrevocable, non-exclusive, and royalty-free license to use, reproduce, modify, sublicense, publish, and distribute your contribution.
10 | 4. This license applies to all forms of contributions, including but not limited to source code and documentation.
11 | 5. You specifically authorize the project maintainers to use your contributions for commercial purposes, including but not limited to commercial publishing, training materials, and licensing.
12 | 6. If you are contributing on behalf of an organization or employer, you confirm that you have the authority to do so.
13 | 7. By submitting a pull request, issue, or any form of contribution, you indicate that you have read and agree to the terms of this CLA.
14 |
15 | ---
16 |
17 | # 贡献者许可协议(CLA)
18 |
19 | 感谢你对本项目的贡献!
20 |
21 | 通过提交内容(包括代码、文档、翻译或其他材料),你同意以下条款:
22 |
23 | 1. 你确认该贡献为你原创,或你已获得适当的权利和授权进行提交;
24 | 2. 你保留对该贡献的著作权;
25 | 3. 你授予本项目维护者一个全球范围、永久、不可撤销、非排他、免费的许可权,用于使用、复制、修改、再许可、发布和分发你的贡献;
26 | 4. 此许可适用于所有类型的贡献,包括但不限于源代码和文档;
27 | 5. 你特别授权本项目维护者将你的贡献用于商业用途,包括但不限于商业出版、培训材料和商业授权;
28 | 6. 如果你是代表组织或雇主提交内容,你确认你有权这样做;
29 | 7. 通过提交 Pull Request、Issue 或其他形式的贡献,即表示你已阅读并同意本 CLA 的全部条款。
--------------------------------------------------------------------------------
/dslings/en/cpp11/12-nullptr-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/12-nullptr-2.cpp
4 | //
5 | // Exercise: cpp11 | 12 - nullptr | Pointer Literal - Template Programming
6 | //
7 | // Tips: Use nullptr for better type safety in template programming
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/cpp11/12-nullptr.md
12 | //
13 | // Exercise discussion: http://forum.d2learn.org/post/360
14 | //
15 | // Auto-Checker command:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 | #include
22 |
23 | // Template function example
24 | template
25 | void processPointer(T* ptr) { }
26 |
27 | // Template function that returns a copy of the input value
28 | template
29 | T clone(const T& t) {
30 | return t;
31 | }
32 |
33 | int main() {
34 |
35 | // 1. Type deduction of nullptr in templates
36 | auto ptr1 = nullptr; // ptr1's type is std::nullptr_t
37 | static_assert(std::is_same::value, "Type deduction error");
38 |
39 | // 2. Advantages of nullptr in template functions - type deduction safety
40 | // Observe compiler errors, try to explicitly specify template parameters to resolve errors
41 | processPointer(clone(0));
42 | processPointer(clone(NULL));
43 |
44 | processPointer(clone(nullptr));
45 | processPointer(clone(nullptr));
46 |
47 | D2X_WAIT
48 |
49 | return 0;
50 | }
--------------------------------------------------------------------------------
/dslings/cpp11/00-auto-and-decltype-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-2.cpp
4 | //
5 | // Exercise/练习: cpp11 | 00 - auto and decltype | 复杂类型推导
6 | //
7 | // Tips/提示: 使用 auto 和 decltype 修复代码中的错误
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // 练习交流讨论: http://forum.d2learn.org/post/357
15 | //
16 | // Auto-Checker/自动检测命令:
17 | //
18 | // d2x checker auto-and-decltype-2
19 | //
20 |
21 | #include
22 |
23 | #include
24 | #include
25 | #include
26 |
27 | int add_func(int a, int b) {
28 | return a + b;
29 | }
30 |
31 | int main() {
32 |
33 | // 2. 复杂类型
34 |
35 | std::vector v = {1, 2, 3};
36 |
37 | std::vector::iterator v1 = v.begin();
38 | for (; v1 != v.end(); ++v1) {
39 | std::cout << *v1 << " ";
40 | }
41 | std::cout << std::endl;
42 |
43 | D2X_YOUR_ANSWER v2 = v.begin();
44 | for (; v2 != v.end(); ++v2) {
45 | std::cout << *v2 << " ";
46 | }
47 | std::cout << std::endl;
48 |
49 | auto minus_func = [](int a, int b) { return a - b; };
50 |
51 | std::vector> funcVec = {
52 | add_func,
53 | minus_func
54 | };
55 |
56 | d2x_assert_eq(funcVec[0](1, 2), 3);
57 | d2x_assert_eq(funcVec[1](1, 2), -1);
58 |
59 | D2X_WAIT
60 |
61 | return 0;
62 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/00-auto-and-decltype-2.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-2.cpp
4 | //
5 | // Exercise: cpp11 | 00 - auto and decltype | Complex Type Deduction
6 | //
7 | // Tips: Use auto and decltype to fix errors in the code
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // Forum: https://forum.d2learn.org/category/20
15 | //
16 | // Auto-Checker command:
17 | //
18 | // d2x checker auto-and-decltype-2
19 | //
20 |
21 | #include
22 |
23 | #include
24 | #include
25 | #include
26 |
27 | int add_func(int a, int b) {
28 | return a + b;
29 | }
30 |
31 | int main() {
32 |
33 | // 2. Complex types
34 |
35 | std::vector v = {1, 2, 3};
36 |
37 | std::vector::iterator v1 = v.begin();
38 | for (; v1 != v.end(); ++v1) {
39 | std::cout << *v1 << " ";
40 | }
41 | std::cout << std::endl;
42 |
43 | D2X_YOUR_ANSWER v2 = v.begin();
44 | for (; v2 != v.end(); ++v2) {
45 | std::cout << *v2 << " ";
46 | }
47 | std::cout << std::endl;
48 |
49 | auto minus_func = [](int a, int b) { return a - b; };
50 |
51 | std::vector> funcVec = {
52 | add_func,
53 | minus_func
54 | };
55 |
56 | d2x_assert_eq(funcVec[0](1, 2), 3);
57 | d2x_assert_eq(funcVec[1](1, 2), -1);
58 |
59 | D2X_WAIT
60 |
61 | return 0;
62 | }
63 |
--------------------------------------------------------------------------------
/dslings/cpp11/08-literal-type-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/08-literal-type-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 08 - literal type | 什么是字面值类型
6 | //
7 | // Tips/提示: 根据编译器的输出, 修复编译器报错, 了解字面值类型的概念
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/named_req/LiteralType.html
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker literal-type-0
15 | //
16 |
17 | #include
18 |
19 | #include
20 | #include
21 | #include
22 |
23 | constexpr char compile_time_compute(char c, int a) {
24 | return a + c;
25 | }
26 |
27 | constexpr std::array to_array(const char *str /*, len = 3 */) {
28 | return {str[0] - '0', str[1] - '0', str[2] - '0'};
29 | }
30 |
31 | constexpr std::string to_string(const std::array &arr) {
32 | return std::string(1, '0' + arr[0]) +
33 | std::string(1, '0' + arr[1]) +
34 | std::string(1, '0' + arr[2]);
35 | }
36 |
37 | int main() {
38 |
39 | // 1.能参与编译期计算的字面值类型
40 | constexpr char c = 'A';
41 | constexpr int a = 1;
42 | constexpr std::array arr = {1, 2, 3};
43 | constexpr std::string str = "123";
44 |
45 | constexpr auto result = compile_time_compute(c, a);
46 | std::cout << result << std::endl;
47 |
48 | // 2."复杂" 聚合/指针/引用等 字面值类型
49 | constexpr auto arr_to_str = to_string(arr);
50 | std::cout << arr_to_str.data() << std::endl;
51 |
52 | constexpr auto str_to_arr = to_array(str);
53 | constexpr int sum = str_to_arr[0] + str_to_arr[1] + str_to_arr[2];
54 |
55 | std::cout << "1 + 2 + 3 = " << sum << std::endl;
56 |
57 | D2X_WAIT
58 |
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/dslings/cpp11/04-rvalue-references.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/04-rvalue-references.cpp
4 | //
5 | // Exercise/练习: cpp11 | 04 - rvalue references
6 | //
7 | // Tips/提示: 使用正确的方式来延长临时对象的生命周期, 让其能被再次修改
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/reference
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker rvalue-references
15 | //
16 |
17 | #include
18 |
19 | #include
20 | #include
21 |
22 | struct Object;
23 | static Object * object_address = nullptr;
24 |
25 | struct Object {
26 | int data = 0;
27 | Object() {
28 | std::cout << "Object():" << this << std::endl;
29 | object_address = this;
30 | }
31 | Object(const Object&) { std::cout << "Object(const Object&):" << this << std::endl; }
32 | Object(Object&&) { std::cout << "Object(Object&&):" << this << std::endl; }
33 | ~Object() { std::cout << "~Object():" << this << std::endl; }
34 | };
35 |
36 | int main() { // 关闭编译器优化
37 | {
38 | std::cout << "----> 临时对像 - 右值1" << std::endl;
39 | Object();
40 | std::cout << "----> 临时对像 - 右值2" << std::endl;
41 | Object obj = Object();
42 |
43 | std::cout << "--------代码可修改区域-开始--------" << std::endl;
44 |
45 |
46 | const Object &objRef = Object(); // 延长临时对象的生命周期
47 |
48 |
49 | std::cout << "--------代码可修改区域-结束--------" << std::endl;
50 |
51 | objRef.data = 1; // 修改被延长生命周期的临时对象的值(不要直接改动这行代码)
52 | std::cout << "objRef.data = " << objRef.data << " - " << &objRef << std::endl;
53 | d2x_assert((&objRef == object_address));
54 | }
55 |
56 | D2X_WAIT
57 |
58 | return 0;
59 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/12-nullptr-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/12-nullptr-0.cpp
4 | //
5 | // Exercise: cpp11 | 12 - nullptr | Pointer Literal - Basic Usage
6 | //
7 | // Tips: Use nullptr to replace traditional NULL and 0 for pointer initialization
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/nullptr
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/en/cpp11/12-nullptr.md
12 | //
13 | // Exercise discussion: http://forum.d2learn.org/post/358
14 | //
15 | // Auto-Checker command:
16 | //
17 | // d2x checker nullptr
18 | //
19 |
20 | #include
21 |
22 | #include
23 |
24 | int main() {
25 |
26 | // 1. Initialize pointers
27 | int* ptr1 = nullptr; // Recommended usage
28 | int* ptr2 = NULL; // Fix here, add correct type
29 | int* ptr3 = 0; // Not recommended traditional usage
30 |
31 | d2x_assert(ptr1 == nullptr);
32 | d2x_assert(ptr2 == nullptr);
33 | d2x_assert(ptr3 == nullptr);
34 |
35 | // 2. Type of nullptr
36 | bool ok = std::is_same::value;
37 | d2x_assert(ok);
38 |
39 | // 3. Use nullptr for pointer comparison
40 | int value = 42;
41 | int* ptr4 = &value;
42 |
43 | if (ptr4 != nullptr) {
44 | *ptr4 = D2X_YOUR_ANSWER;
45 | d2x_assert_eq(*ptr4, 2233);
46 | }
47 |
48 | // 4. Different types of pointers can all use nullptr
49 | double* dptr = nullptr;
50 | char* cptr = nullptr
51 | void* vptr = nullptr;
52 |
53 | d2x_assert(dptr == nullptr);
54 | d2x_assert(cptr == nullptr);
55 | d2x_assert(vptr == nullptr);
56 |
57 | D2X_WAIT
58 |
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/14-type-alias-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/en/cpp11/14-type-alias-1.cpp
4 | //
5 | // Exercise: cpp11 | 14 - type alias | Complex Types and Function Pointer Aliases
6 | //
7 | // Tips: Use the using keyword to define aliases for complex types and function pointers
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/type_alias
11 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/14-type-alias.md
12 | //
13 | // Discussion Forum: http://forum.d2learn.org/category/20
14 | //
15 | // Auto-Checker Command:
16 | //
17 | // d2x checker type-alias
18 | //
19 |
20 | #include
21 |
22 | #include
23 | #include
24 |
25 | static int func_called = 0;
26 |
27 | // Function declaration
28 | void example_func(int a, int b) {
29 | func_called = a + b;
30 | }
31 |
32 | int main() {
33 |
34 | // 1. Function pointer alias
35 | D2X_YOUR_ANSWER FuncPtr = void(*)(int, int);
36 |
37 | FuncPtr func = example_func;
38 |
39 | func(1, 2);
40 | d2x_assert_eq(func_called, 3);
41 |
42 | // 2. Container type alias
43 | // Use using to define an alias for vector
44 | D2X_YOUR_ANSWER = std::vector;
45 |
46 | // Using container type alias
47 | StringVector strings = {"hello", "world"};
48 |
49 | // 3. Nested type alias
50 | struct Container {
51 | ValueType = int
52 | };
53 |
54 | Container::ValueType value = 100;
55 |
56 | // 4. Verifying type aliases
57 | d2x_assert(strings[0] == "hello");
58 | d2x_assert(strings[1] == "world");
59 | d2x_assert_eq(value, 100);
60 |
61 | D2X_WAIT
62 |
63 | return 0;
64 | }
--------------------------------------------------------------------------------
/tools/update_upstream.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | REM ===== 配置参数 =====
3 | set "UPSTREAM_URL=https://github.com/Sunrisepeak/mcpp-standard.git"
4 | set "BRANCH_NAME=main"
5 | set "HAS_CHANGES="
6 | set "HAS_COMMITS="
7 | set "BACKUP_REQUIRED="
8 | set "TIMESTAMP="
9 | set "BACKUP_BRANCH="
10 | set "COMMITS_AHEAD="
11 |
12 | echo [1] - 检查是否已添加 upstream 远程...
13 | git remote | findstr /b /c:"upstream" >nul
14 | if errorlevel 1 (
15 | echo ➕ 添加 upstream: %UPSTREAM_URL%
16 | git remote add upstream %UPSTREAM_URL%
17 | ) else (
18 | echo ✅ upstream 已存在
19 | )
20 |
21 | echo [2] - 获取 upstream 更新...
22 | git fetch upstream
23 | if errorlevel 1 goto error
24 |
25 | echo [3] - 切换到 %BRANCH_NAME% 分支...
26 | git checkout %BRANCH_NAME%
27 | if errorlevel 1 goto error
28 |
29 | echo [4] - 检测到本地更改或提交,正在创建备份...
30 |
31 | REM 注: 不能放到()内,() 代码块中使用 %变量%, 运行时不会更新
32 | for /f %%i in ('powershell -Command "Get-Date -Format yyyyMMdd-HHmmss"') do set TIMESTAMP=%%i
33 | set BACKUP_BRANCH=solutions-%TIMESTAMP%
34 |
35 | echo 🔀 创建备份分支: %BACKUP_BRANCH%
36 | git checkout -b %BACKUP_BRANCH%
37 | if errorlevel 1 goto error
38 |
39 | REM ===== 检查是否有未提交更改 =====
40 | git diff --quiet
41 | if errorlevel 1 set HAS_CHANGES=1
42 |
43 | git diff --cached --quiet
44 | if errorlevel 1 set HAS_CHANGES=1
45 |
46 | if defined HAS_CHANGES (
47 | echo 📦 提交本地未提交更改...
48 | git add .
49 | git commit -m "🧶 自动备份:本地改动"
50 | if errorlevel 1 goto error
51 | )
52 |
53 | echo ✅ 本地改动已保存到 %BACKUP_BRANCH%
54 |
55 | echo 🔁 回到 %BRANCH_NAME% 分支...
56 | git checkout %BRANCH_NAME%
57 |
58 | echo [5] - 使用 rebase 同步 upstream/%BRANCH_NAME% 到本地 %BRANCH_NAME%...
59 | git rebase upstream/%BRANCH_NAME%
60 | if errorlevel 1 (
61 | goto error
62 | )
63 |
64 | echo ✅ 更新完成!
65 | goto end
66 |
67 | :error
68 | echo ❌ 出现冲突,请手动解决后完成 rebase
69 | exit /b 1
70 |
71 | :end
--------------------------------------------------------------------------------
/dslings/cpp11/06-scoped-enums-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/06-scoped-enums-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 06 - scoped enums | 范围枚举类型基本用法
6 | //
7 | // Tips/提示: 通过编译器的错误提示修复代码, 并理解传统枚举类型的潜在问题
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/enum
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker scoped-enums-1
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | enum class Color {
22 | RED,
23 | GREEN,
24 | BLUE,
25 | ORANGE // 橙色
26 | };
27 |
28 | enum Fruit {
29 | Apple,
30 | Banana,
31 | ORANGE // 橙子
32 | };
33 |
34 | int main() {
35 |
36 | // 1.作用域限定: 使用范围枚举类型, 解决ORANGE类型冲突问题
37 | Color color = Color::ORANGE;
38 | Fruit fruit = Fruit::ORANGE;
39 |
40 | d2x_assert(color == Color::ORANGE);
41 | d2x_assert(fruit == Fruit::ORANGE);
42 |
43 | // 2.类型安全: 防止不同类型的枚举值之间的比较
44 | if (color == Fruit::ORANGE) { // 使用Color类型修复编译错误
45 | d2x_assert(color == Color::ORANGE);
46 | }
47 |
48 | // 3.类型检查: 默认情况下, 范围枚举类型的值是不可隐式转换
49 | int colorValue = color; // 需要显式转换static_cast(color)
50 |
51 | // 4.可自定义底层类型, 控制内存布局
52 | enum class Color8Bit : short {
53 | RED,
54 | GREEN,
55 | BLUE,
56 | ORANGE // 橙色
57 | };
58 |
59 | d2x_assert_eq(sizeof(Color), sizeof(int)); // 默认类型是int
60 | d2x_assert_eq(sizeof(Color8Bit), sizeof(int8_t)); // 可自定义类型int8_t
61 |
62 | // 5.自定义起始值: 默认情况下, 范围枚举类型的值从0开始, 往下递增
63 | enum class ErrorCode : int {
64 | OK = 0,
65 | ERROR_1,
66 | ERROR_2 = -2,
67 | ERROR_3
68 | };
69 |
70 | d2x_assert_eq(static_cast(ErrorCode::ERROR_3), 3);
71 |
72 | D2X_WAIT
73 |
74 | return 0;
75 | }
--------------------------------------------------------------------------------
/dslings/en/cpp11/08-literal-type-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/08-literal-type-0.cpp
4 | //
5 | // Exercise: cpp11 | 08 - literal type | What are literal types
6 | //
7 | // Tips: Fix compiler errors based on compiler output, understand the concept of literal types
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/named_req/LiteralType.html
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker literal-type-0
15 | //
16 |
17 | #include
18 |
19 | #include
20 | #include
21 | #include
22 |
23 | constexpr char compile_time_compute(char c, int a) {
24 | return a + c;
25 | }
26 |
27 | constexpr std::array to_array(const char *str /*, len = 3 */) {
28 | return {str[0] - '0', str[1] - '0', str[2] - '0'};
29 | }
30 |
31 | constexpr std::string to_string(const std::array &arr) {
32 | return std::string(1, '0' + arr[0]) +
33 | std::string(1, '0' + arr[1]) +
34 | std::string(1, '0' + arr[2]);
35 | }
36 |
37 | int main() {
38 |
39 | // 1. Literal types that can participate in compile-time computation
40 | constexpr char c = 'A';
41 | constexpr int a = 1;
42 | constexpr std::array arr = {1, 2, 3};
43 | constexpr std::string str = "123";
44 |
45 | constexpr auto result = compile_time_compute(c, a);
46 | std::cout << result << std::endl;
47 |
48 | // 2. "Complex" aggregate/pointer/reference literal types
49 | constexpr auto arr_to_str = to_string(arr);
50 | std::cout << arr_to_str.data() << std::endl;
51 |
52 | constexpr auto str_to_arr = to_array(str);
53 | constexpr int sum = str_to_arr[0] + str_to_arr[1] + str_to_arr[2];
54 |
55 | std::cout << "1 + 2 + 3 = " << sum << std::endl;
56 |
57 | D2X_WAIT
58 |
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/book/en/src/base/chapter_0.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 🌎 [中文] | [English]
4 |
5 |
6 | [中文]: ../../base/chapter_0.html
7 | [English]: ./chapter_0.html
8 |
9 | # Preface
10 |
11 | mcpp-standard is an open-source tutorial project focused on **Modern C++ Core Language Features** with an emphasis on hands-on coding practice. The project structure follows the [Book + Video + Code + X] model, providing users with online e-books, corresponding instructional videos, accompanying practice code, as well as discussion forums and regular learning activities.
12 |
13 | - [Book: Online E-book](https://sunrisepeak.github.io/mcpp-standard/en)
14 | - [Video: Instructional Videos](https://youtube.com/playlist?list=PL7uow6t1QjF0ooMLkLSS96swpSuBZvoRE&si=1xHOGVIYpbzZAosI)
15 | - [Code: Practice Code](https://github.com/Sunrisepeak/mcpp-standard/tree/main/dslings/en)
16 | - [X: mcpp Forum](https://forum.d2learn.org/category/20)
17 |
18 | ## Language Support
19 |
20 | | 中文 | English | Repo |
21 | | --- | --- | --- |
22 | | [中文](../../base/chapter_0.html) | [English](./) | [Github](https://github.com/Sunrisepeak/mcpp-standard) |
23 |
24 | ## Activities | [ 📣 MSCP - mcpp Project Learning and Contributor Cultivation Program ](https://moga.d2learn.org/activity/mscp/intro.html)
25 |
26 | > MSCP is a "Earth Online" style role-playing game developed based on the mcpp-standard open-source project. In the game, you'll play as a "programming beginner" embarking on a challenging and exciting journey to learn Modern C++ and uncover its underlying truths...
27 |
28 | - `Price:` Free
29 | - `Developer:` [Sunrisepeak](https://github.com/Sunrisepeak)
30 | - `Publisher:` [MOGA](https://moga.d2learn.org)
31 | - `Release Date:` October 2025
32 | - `Game Duration:` 100H - 200H
33 | - `Tags:` Souls-like, The Sims, 🌍Online, Programmer, C++, Open Source, Feynman Learning Method
34 | - [-> Game Details](https://moga.d2learn.org/activity/mscp/intro.html)
35 |
--------------------------------------------------------------------------------
/dslings/en/cpp11/04-rvalue-references.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/04-rvalue-references.cpp
4 | //
5 | // Exercise: cpp11 | 04 - rvalue references
6 | //
7 | // Tips: Use the correct way to extend the lifetime of temporary objects so they can be modified again
8 | //
9 | // Docs:
10 | // - https://en.cppreference.com/w/cpp/language/reference
11 | //
12 | // Auto-Checker command:
13 | //
14 | // d2x checker rvalue-references
15 | //
16 |
17 | #include
18 |
19 | #include
20 | #include
21 |
22 | struct Object;
23 | static Object * object_address = nullptr;
24 |
25 | struct Object {
26 | int data = 0;
27 | Object() {
28 | std::cout << "Object():" << this << std::endl;
29 | object_address = this;
30 | }
31 | Object(const Object&) { std::cout << "Object(const Object&):" << this << std::endl; }
32 | Object(Object&&) { std::cout << "Object(Object&&):" << this << std::endl; }
33 | ~Object() { std::cout << "~Object():" << this << std::endl; }
34 | };
35 |
36 | int main() { // Disable compiler optimization
37 | {
38 | std::cout << "----> Temporary object - rvalue 1" << std::endl;
39 | Object();
40 | std::cout << "----> Temporary object - rvalue 2" << std::endl;
41 | Object obj = Object();
42 |
43 | std::cout << "--------Code modifiable area - Start--------" << std::endl;
44 |
45 |
46 | const Object &objRef = Object(); // Extend temporary object lifetime
47 |
48 |
49 | std::cout << "--------Code modifiable area - End--------" << std::endl;
50 |
51 | objRef.data = 1; // Modify the value of the extended lifetime temporary object (do not directly modify this line)
52 | std::cout << "objRef.data = " << objRef.data << " - " << &objRef << std::endl;
53 | d2x_assert((&objRef == object_address));
54 | }
55 |
56 | D2X_WAIT
57 |
58 | return 0;
59 | }
--------------------------------------------------------------------------------
/dslings/cpp11/00-auto-and-decltype-4.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/00-auto-and-decltype-4.cpp
4 | //
5 | // Exercise/练习: cpp11 | 00 - auto and decltype | 类/结构体成员类型推导
6 | //
7 | // Tips/提示: 在D2X_YOUR_ANSWER处填写正确的推导类型
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/auto
11 | // - https://en.cppreference.com/w/cpp/language/decltype
12 | // - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/00-auto-and-decltype.md
13 | //
14 | // 练习交流讨论: http://forum.d2learn.org/post/357
15 | //
16 | // Auto-Checker/自动检测命令:
17 | //
18 | // d2x checker auto-and-decltype-4
19 | //
20 |
21 | #include
22 |
23 | #include
24 |
25 |
26 | // 4. 类/结构体成员类型推导
27 |
28 | struct Object {
29 | const int a;
30 | double b;
31 | Object() : a(1), b(2.0) { }
32 | };
33 |
34 | int main() {
35 | const Object obj;
36 |
37 | bool type_check = false;
38 |
39 | // obj的类型推导 和 (obj) 的类型推导
40 | type_check = std::is_same::value;
41 | d2x_assert(type_check); type_check = false; // dont change this line
42 | type_check = std::is_same::value;
43 | d2x_assert(type_check); type_check = false; // dont change this line
44 |
45 | // obj.a的类型推导 和 (obj.a) 的类型推导
46 | type_check = std::is_same::value;
47 | d2x_assert(type_check); type_check = false; // dont change this line
48 | type_check = std::is_same::value;
49 | d2x_assert(type_check); type_check = false; // dont change this line
50 |
51 | // obj.b的类型推导 和 (obj.b) 的类型推导
52 | type_check = std::is_same::value;
53 | d2x_assert(type_check); type_check = false; // dont change this line
54 | type_check = std::is_same::value;
55 | d2x_assert(type_check); type_check = false; // dont change this line
56 |
57 | D2X_WAIT
58 |
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/dslings/cpp11/07-constexpr-1.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/07-constexpr-1.cpp
4 | //
5 | // Exercise/练习: cpp11 | 07 - constexpr | 编译期计算应用示例
6 | //
7 | // Tips/提示: 根据编译器的输出, 修复编译器报错
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/constexpr
11 | //
12 | // Auto-Checker/自动检测命令:
13 | //
14 | // d2x checker constexpr
15 | //
16 |
17 | #include
18 |
19 | #include
20 |
21 | template
22 | struct Sum {
23 | static constexpr int value = Sum::value + N;
24 | };
25 |
26 | template <>
27 | struct Sum<1> { static constexpr int value = 1; };
28 |
29 | constexpr int factorial(int n) {
30 | return n <= 1 ? 1 : n * factorial(n - 1);
31 | }
32 |
33 | double pow(double base, int exp) {
34 | return exp == 0 ? 1.0 : base * pow(base, exp - 1);
35 | }
36 |
37 | constexpr double mysin(double x) {
38 | //constexpr double PI = 3.14159265358979323846;
39 | //constexpr double radius = x * PI / 180.0;
40 | #define radius(x) (x * 3.14159265358979323846 / 180.0)
41 | // (-1)^n * radius(x)^2n+1 / factorial(2n + 1);
42 | return radius(x)
43 | - pow(radius(x), 3) / factorial(3)
44 | + pow(radius(x), 5) / factorial(5);
45 | }
46 |
47 | int main() {
48 |
49 | // 1. 编译期-函数计算
50 | constexpr int fact_10 = factorial(10);
51 | std::cout << "1 * 2 * .. * 10 = " << fact_10 << std::endl;
52 |
53 | // 2. 编译期-模板参数计算
54 | constexpr int sum_4 = Sum<4>::value;
55 | std::cout << "1 + 2 + 3 + 4 = " << sum_4 << std::endl;
56 |
57 | // 3. 编译期计算示例:
58 | // value是多少时? value! + (1 + 2 + .. + value) > 10000
59 | constexpr int value = 5;
60 | int f = factorial(value);
61 | constexpr int s = Sum::value;
62 | constexpr int ans = f + s;
63 |
64 | static_assert(ans > 10000);
65 |
66 | // 4. 编译期计算sin值(自动打表) - 时间复杂度O(1)
67 | constexpr double sin30 = mysin(30.0);
68 | std::cout << "mysin(30): " << sin30 << " " << std::endl;
69 |
70 | D2X_WAIT
71 |
72 | return 0;
73 | }
--------------------------------------------------------------------------------
/.github/workflows/online-ebook.yml:
--------------------------------------------------------------------------------
1 | # Sample workflow for building and deploying a mdBook site to GitHub Pages
2 | #
3 | # To get started with mdBook see: https://rust-lang.github.io/mdBook/index.html
4 | #
5 | name: Deploy Online EBook site to Pages
6 |
7 | on:
8 | # Runs on pushes targeting the default branch
9 | push:
10 | branches: ["main"]
11 | paths:
12 | - "book/src/**"
13 |
14 | # Allows you to run this workflow manually from the Actions tab
15 | workflow_dispatch:
16 |
17 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
18 | permissions:
19 | contents: read
20 | pages: write
21 | id-token: write
22 |
23 | # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
24 | # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
25 | concurrency:
26 | group: "pages"
27 | cancel-in-progress: false
28 |
29 | jobs:
30 | # Build job
31 | build:
32 | runs-on: ubuntu-latest
33 |
34 | defaults:
35 | run:
36 | working-directory: book
37 |
38 | env:
39 | MDBOOK_VERSION: 0.4.43
40 |
41 | steps:
42 | - uses: actions/checkout@v4
43 | - name: Install mdBook by Xlings
44 | run: |
45 | curl -fsSL https://d2learn.org/xlings-install.sh | bash
46 | xlings install mdbook@${MDBOOK_VERSION} --global -y
47 | - name: Setup Pages
48 | id: pages
49 | uses: actions/configure-pages@v4
50 | - name: Build with mdBook
51 | run: |
52 | export PATH=/home/xlings/.xlings_data/bin:$PATH
53 | ./build.sh
54 | - name: Upload artifact
55 | uses: actions/upload-pages-artifact@v3
56 | with:
57 | path: ./book/book
58 |
59 | # Deployment job
60 | deploy:
61 | environment:
62 | name: github-pages
63 | url: ${{ steps.deployment.outputs.page_url }}
64 | runs-on: ubuntu-latest
65 | needs: build
66 | steps:
67 | - name: Deploy to GitHub Pages
68 | id: deployment
69 | uses: actions/deploy-pages@v4
70 |
--------------------------------------------------------------------------------
/dslings/cpp11/11-inherited-constructors-0.cpp:
--------------------------------------------------------------------------------
1 | // mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2 | // license: Apache-2.0
3 | // file: dslings/cpp11/11-inherited-constructors-0.cpp
4 | //
5 | // Exercise/练习: cpp11 | 11 - inherited constructors | 继承构造函数
6 | //
7 | // Tips/提示: 根据编译器的输出和报错信息, 添加符合要求的构造函数, 并了解继承构造函数的基本用法
8 | //
9 | // Docs/文档:
10 | // - https://en.cppreference.com/w/cpp/language/using_declaration.html#Inheriting_constructors
11 | // - https://sunrisepeak.github.io/mcpp-standard/cpp11/11-inherited-constructors.html
12 | //
13 | // Auto-Checker/自动检测命令:
14 | //
15 | // d2x checker inherited-constructors
16 | //
17 |
18 | #include
19 |
20 | #include