Update: Using C++26 with contracts enabled

This commit is contained in:
David Gil de Gómez Pérez
2026-08-12 13:18:20 +03:00
parent 3cce26e72d
commit ea0e601850
18 changed files with 153 additions and 84 deletions
+69 -27
View File
@@ -5,21 +5,33 @@ A hands-on C++ course with **56 Google Test exercises** organized into **14 modu
## Requirements
- **CMake** 3.20+
- **C++ compiler** with C++23 support for later modules
- Apple Clang 15+, GCC 13+, or MSVC 19.34+ recommended
- **C++26-capable compiler**
- **GCC 16+** recommended (required for `pre`/`post` contracts)
- Apple Clang can build most exercises but **does not support contracts yet**
- **CLion** (recommended) or any CMake-aware IDE
- Internet access on first configure (Google Test is fetched automatically)
## Quick Start (CLion)
1. Open this folder in CLion (`File → Open…`).
2. CLion detects `CMakeLists.txt` and configures automatically.
3. Pick an exercise target (e.g. `01_fundamentals_hello_world`) in the run configuration dropdown.
4. Implement the TODOs in the matching `src/*.cpp` file.
5. Run the test target until all assertions pass.
2. Select the **`gcc26-contracts`** CMake profile (see `CMakePresets.json`).
3. Reload CMake when prompted.
4. Pick an exercise target (e.g. `02_control_flow_loops`) in the run configuration dropdown.
5. Implement the TODOs in the matching `src/*.cpp` file.
6. Run the test target until all assertions pass.
## Quick Start (Terminal)
**Recommended — GCC 16 with contracts:**
```bash
cmake --preset gcc26-contracts
cmake --build build-gcc26
ctest --test-dir build-gcc26 --output-on-failure
```
**Default toolchain:**
```bash
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
@@ -29,8 +41,8 @@ ctest --test-dir build --output-on-failure
Run a single exercise:
```bash
cmake --build build --target 01_fundamentals_hello_world
./build/modules/01_fundamentals/01_fundamentals_hello_world
cmake --build build-gcc26 --target 02_control_flow_loops
./build-gcc26/modules/02_control_flow/02_control_flow_loops
```
## Project Structure
@@ -40,7 +52,10 @@ cppc/
├── CMakeLists.txt # Root project + Google Test
├── cmake/
│ ├── CourseExercise.cmake # add_course_exercise() helper
── CourseOptions.cmake # Warnings and standards
── CourseOptions.cmake # Warnings, standards, contracts
│ └── toolchains/gcc-16.cmake # Optional GCC 16 toolchain file
├── CMakePresets.json # CLion/CMake presets (gcc26-contracts)
├── support/ # Shared contract violation handler
└── modules/
├── 01_fundamentals/ # Types, operators, I/O
├── 02_control_flow/ # if, switch, loops
@@ -75,22 +90,22 @@ Each module contains:
## Course Map
| Module | Topic | Exercises | C++ Standard |
|--------|-------|-----------|--------------|
| 01 | Fundamentals | 4 | C++17 |
| 02 | Control flow | 4 | C++17 |
| 03 | Functions | 4 | C++17 |
| 04 | Arrays & strings | 4 | C++17 |
| 05 | Pointers & references | 4 | C++17 |
| 06 | OOP basics | 4 | C++17 |
| 07 | STL containers | 4 | C++17 |
| 08 | STL algorithms | 4 | C++17 |
| 09 | C++11 | 4 | C++11 |
| 10 | C++14 | 4 | C++14 |
| 11 | C++17 | 4 | C++17 |
| 12 | C++20 | 4 | C++20 |
| 13 | C++23 | 4 | C++23 |
| 14 | C++26 frontier | 4 | C++23+ |
| Module | Topic | Exercises | Build standard |
|--------|-------|-----------|----------------|
| 01 | Fundamentals | 4 | C++26 |
| 02 | Control flow | 4 | C++26 |
| 03 | Functions | 4 | C++26 |
| 04 | Arrays & strings | 4 | C++26 |
| 05 | Pointers & references | 4 | C++26 |
| 06 | OOP basics | 4 | C++26 |
| 07 | STL containers | 4 | C++26 |
| 08 | STL algorithms | 4 | C++26 |
| 09 | C++11 features | 4 | C++26 |
| 10 | C++14 features | 4 | C++26 |
| 11 | C++17 features | 4 | C++26 |
| 12 | C++20 features | 4 | C++26 |
| 13 | C++23 features | 4 | C++26 |
| 14 | C++26 frontier | 4 | C++26 |
## CMake Options
@@ -98,11 +113,38 @@ Each module contains:
|--------|---------|-------------|
| `COURSE_BUILD_TESTS` | ON | Build Google Test exercise targets |
| `COURSE_WARNINGS_AS_ERRORS` | OFF | Treat warnings as errors |
| `COURSE_ENABLE_CONTRACTS` | ON | Enable `-fcontracts` on GCC 16+ targets |
Example:
```bash
cmake -S . -B build -DCOURSE_WARNINGS_AS_ERRORS=ON
cmake --preset gcc26-contracts -DCOURSE_WARNINGS_AS_ERRORS=ON
```
## C++26 Contracts
Exercises compile as **C++26**. On **GCC 16+**, the build enables:
- `-fcontracts`
- `-fcontract-semantic=enforce`
- A shared `handle_contract_violation` handler in `support/contract_handler.cpp`
Use `#include <contracts>` and syntax like:
```cpp
auto factorial(int n) -> long long
pre(n >= 0)
{
// ...
}
```
**Apple Clang does not support contracts yet.** Use the `gcc26-contracts` preset in CLion or:
```bash
cmake -S . -B build-gcc26 \
-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g++-16 \
-DCOURSE_ENABLE_CONTRACTS=ON
```
## CLion Tips
@@ -114,6 +156,6 @@ cmake -S . -B build -DCOURSE_WARNINGS_AS_ERRORS=ON
## Notes on C++26
C++26 is still in active standardization. Module 14 focuses on techniques and library directions that remain relevant—deep `constexpr`, monadic `expected`, ranges pipelines, and `std::formatter`—using C++23 as the practical baseline.
All exercises now build with **C++26**. Module READMEs still describe the language feature each exercise teaches (C++11, C++17, etc.), but the toolchain baseline is unified at C++26 for a consistent modern environment.
Happy learning!