C++ 20

C++20 is the latest version of the C++ programming language, bringing several new features and improvements to the language.

Concept Checking

C++20 introduces concepts, which allow you to specify requirements on template arguments, making it easier to write generic code and providing clearer error messages when a type does not meet the requirements. Here’s a simple example:

#include <iostream>
#include <concepts>

template <typename T>
concept Integral = std::is_integral_v<T>;

template <Integral T>
T add(T a, T b) {
    return a + b;
}

int main() {
    int result = add(10, 20);
    std::cout << "Result: " << result << std::endl;
    // Won't compile: add("hello", "world");
    return 0;
}

In this example, we define a concept Integral to check if a type T is integral. The add function uses this concept as a template parameter. If we try to call add with non-integral types (like strings), the compiler will generate a clear error message indicating that the concept requirements are not met.

Ranges

C++20 introduces ranges, which provide a more modern and convenient way to work with sequences of elements, replacing the traditional use of iterators. Ranges come with a new set of algorithms that work seamlessly with them. Here’s a simple example:

#include <iostream>
#include <vector>
#include <algorithm>
#include <ranges>

int main() {
    std::vector<int> numbers = {10, 20, 30, 40, 50};

    // Using ranges and algorithms to find the sum of even numbers
    auto sum_of_evens = std::ranges::accumulate(numbers | std::views::filter([](int n) { return n % 2 == 0; }), 0);

    std::cout << "Sum of even numbers: " << sum_of_evens << std::endl;

    return 0;
}

In this example, we use the filter view to create a range containing only the even numbers from the numbers vector. We then use the accumulate algorithm from the <ranges> header to find the sum of these even numbers.

Coroutines

C++20 introduces coroutines, which allow you to write asynchronous code in a more natural and readable way. Coroutines use the co_await keyword to suspend the execution of a coroutine until a particular operation completes. Here’s a simple example:

#include <iostream>
#include <coroutine>

// The coroutine function
struct HelloWorld {
    struct promise_type {
        HelloWorld get_return_object() { return {}; }
        std::suspend_never initial_suspend() { return {}; }
        std::suspend_never final_suspend() noexcept { return {}; }
        void return_void() {}
    };

    void say_hello() {
        std::cout << "Hello, ";
        co_await std::suspend_always{};
        std::cout << "world!" << std::endl;
    }
};

int main() {
    HelloWorld hw;
    hw.say_hello();
    return 0;
}

In this example, we define a coroutine function HelloWorld::say_hello(), which prints “Hello, ” and then suspends itself using co_await. The suspend_always object tells the coroutine to suspend indefinitely until it is resumed. The coroutine then prints “world!” and completes its execution.

These are just a few of the new features introduced in C++20. Other notable features include improvements in modules, improved lambda captures, new standard attributes, and more standard library additions.