C++ Programming Labs

Master the cutting-edge features of C++20: concepts, ranges, and coroutines.

C++20 Modern Features - Module 10

The future of C++ programming is here.

Lab 28: Concepts
C++20
Coding Challenge
Your Task: Learn C++20 Concepts - a way to constrain templates with readable, reusable requirements. They replace complex SFINAE patterns.

Detailed Requirements:
1. Include concepts header:
#include <concepts>

2. Use standard concepts:
template T double_it(T value) { return value * 2; } // Only works with integral types! double_it(5); // OK // double_it(3.14); // Error!

3. Define custom concepts:
template concept Printable = requires(T t) { { std::cout << t } -> std::same_as; }; template void print(T value) { std::cout << value << std::endl; }

4. Requires clause:
template requires std::integral || std::floating_point T add(T a, T b) { return a + b; }

5. Abbreviated function templates:
void process(std::integral auto value) { cout << "Integer: " << value << endl; }

Expected Output:
Doubled: 10 Sum: 15 Printed: Hello Concepts!

Requirements Checklist

Include concepts header
Use std::integral concept
Define custom concept with requires
Use concept as template constraint
Use requires clause
Use abbreviated auto syntax
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Concept: concept Name = requires(T t) { ... };
• Constrain: template<std::integral T>
• Requires: requires std::integral<T>
• Auto: std::integral auto x
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 29: Ranges Library
C++20
Coding Challenge
Your Task: Master the C++20 Ranges library - a modern way to work with sequences using composable views and lazy evaluation.

Detailed Requirements:
1. Include ranges header:
#include <ranges>

2. Use range-based views:
namespace views = std::views; vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Filter even numbers auto evens = nums | views::filter([](int n) { return n % 2 == 0; }); // Output: 2 4 6 8 10

3. Transform view:
auto squared = nums | views::transform([](int n) { return n * n; }); // Output: 1 4 9 16 25 ...

4. Chain multiple views:
auto result = nums | views::filter([](int n) { return n % 2 == 0; }) | views::transform([](int n) { return n * n; }) | views::take(3); // Output: 4 16 36

5. Other useful views:
views::iota(1, 10) // 1,2,3,...,9 views::take(5) // First 5 elements views::drop(3) // Skip first 3 views::reverse // Reverse order

Expected Output:
Evens: 2 4 6 8 10 Squared: 1 4 9 16 25 First 3 even squares: 4 16 36 Range 1-5: 1 2 3 4 5

Requirements Checklist

Include ranges header
Use views::filter
Use views::transform
Chain views with pipe |
Use views::take or views::drop
Use views::iota for ranges
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Namespace: namespace views = std::views;
• Filter: nums | views::filter(pred)
• Transform: nums | views::transform(func)
• Chain: x | view1 | view2 | view3
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 30: Three-Way Comparison
C++20
Coding Challenge
Your Task: Learn the C++20 spaceship operator (<=>) - three-way comparison that automatically generates all comparison operators.

Detailed Requirements:
1. Include compare header:
#include <compare>

2. Basic spaceship operator:
int a = 5, b = 10; auto result = a <=> b; // result < 0 means a < b // result == 0 means a == b // result > 0 means a > b

3. Spaceship in classes:
struct Point { int x, y; auto operator<=>(const Point&) const = default; // Automatically generates: <, >, <=, >=, ==, != }; Point p1{1, 2}, p2{3, 4}; if (p1 < p2) cout << "p1 is less" << endl;

4. Custom spaceship implementation:
struct Version { int major, minor, patch; auto operator<=>(const Version& other) const { if (auto cmp = major <=> other.major; cmp != 0) return cmp; if (auto cmp = minor <=> other.minor; cmp != 0) return cmp; return patch <=> other.patch; } bool operator==(const Version&) const = default; };

5. Comparison categories:
std::strong_ordering // Total order (int, string) std::weak_ordering // Equivalence (case-insensitive) std::partial_ordering // May be unordered (float NaN)

Expected Output:
5 <=> 10: less Point{1,2} < Point{3,4}: true Version 1.2.3 < 2.0.0: true All comparisons generated!

Requirements Checklist

Include compare header
Use spaceship operator <=>
Use = default for auto-generation
Create struct with spaceship
Compare objects with < or >
Use strong_ordering or auto return
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Spaceship: auto operator<=>(const T&) const = default;
• Compare: auto r = a <=> b;
• Check: if (r < 0), if (r == 0), if (r > 0)
• Returns strong_ordering, weak_ordering, or partial_ordering
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

C++ Mastery Complete!

You've completed all 30 C++ Programming Labs! From basics to C++20, you're now a true C++ expert.