DEV Community

Cover image for What’s New in C++20: A Practical Guide for Everyday C++ Developers
Official Beep8
Official Beep8

Posted on

What’s New in C++20: A Practical Guide for Everyday C++ Developers

What’s New in C++20: A Practical Guide for Everyday C++ Developers

If you’re a C++ developer who has stuck with C++11, C++14, or C++17, you might be wondering what’s in it for you in C++20. The answer is: a lot. From powerful new language features to quality-of-life improvements, C++20 makes modern C++ development more expressive, readable, and efficient.

In this article, I’ll walk you through the most practical and impactful additions in C++20, especially for developers already comfortable with "classic" C++.


✅ 1. auto with Lambdas (Template Lambdas)

You can now write lambdas that accept generic parameters using auto, without needing to use std::function or manually template them.

auto adder = [](auto a, auto b) {
    return a + b;
};

std::cout << adder(1, 2) << "\n";      // 3
std::cout << adder(1.5, 2.5) << "\n";  // 4.0
Enter fullscreen mode Exit fullscreen mode

This makes generic lambda code easier than ever.


✅ 2. consteval and constinit

  • consteval forces a function to be evaluated at compile-time.
  • constinit ensures a variable is initialized at compile time, but not necessarily constant.
consteval int square(int x) {
    return x * x;
}

constinit int cached = square(10);  // OK at compile-time
Enter fullscreen mode Exit fullscreen mode

These keywords give you compile-time guarantees with more precision than constexpr alone.


✅ 3. Ranges Library

The <ranges> library is one of the biggest additions in C++20. It provides a pipeline-like syntax for working with collections.

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

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    for (int i : v | std::ranges::views::filter([](int x) { return x % 2 == 0; })
                   | std::ranges::views::transform([](int x) { return x * x; })) {
        std::cout << i << " ";
    }
    // Output: 4 16
}
Enter fullscreen mode Exit fullscreen mode

Think of it as LINQ for C++ — elegant, functional, and composable.


✅ 4. std::span

std::span is a non-owning view over a sequence of data (like arrays or vectors). It’s great for function parameters.

#include <span>

void print(std::span<int> data) {
    for (int x : data) std::cout << x << " ";
}

int main() {
    std::vector<int> v = {1, 2, 3};
    print(v);  // No copy needed
}
Enter fullscreen mode Exit fullscreen mode

No more passing raw pointers and sizes separately!


✅ 5. [[likely]] and [[unlikely]]

You can now hint the compiler about the expected branch.

if ([[likely]] x == 0) {
    // Fast path
} else {
    // Rarely used path
}
Enter fullscreen mode Exit fullscreen mode

While not mandatory, this helps the optimizer generate more efficient branch code.


✅ 6. std::format

Finally, type-safe string formatting arrives!

#include <format>
std::string message = std::format("Hello, {}!", "World");
std::cout << message;  // Hello, World!
Enter fullscreen mode Exit fullscreen mode

This is a type-safe, fast, and readable alternative to sprintf or manual string concatenation.


✅ 7. Coroutines (For Advanced Users)

If you’ve ever used async or generator-style programming in other languages like Python or C#, C++20 now has native coroutine support.

#include <coroutine>
#include <iostream>

struct HelloTask {
    struct promise_type {
        HelloTask get_return_object() { return {}; }
        std::suspend_never initial_suspend() { return {}; }
        std::suspend_never final_suspend() noexcept { return {}; }
        void return_void() {}
        void unhandled_exception() {}
    };
};

HelloTask say_hello() {
    std::cout << "Hello from coroutine!\n";
}

int main() {
    say_hello();
}
Enter fullscreen mode Exit fullscreen mode

Coroutines are low-level, but libraries like cppcoro or Boost.Coroutine build user-friendly abstractions on top.


Final Thoughts

If you haven’t explored C++20 yet, it’s worth diving in. Many of these features will improve your code’s clarity, performance, and safety without needing to drastically change your programming style.

Want to go deeper? Consider exploring:

  • concepts (compile-time constraints for templates)
  • modules (new way to structure code instead of headers)
  • three-way comparison (<=>) aka spaceship operator

✍️ Summary

C++20 brings modern programming ergonomics to C++:

✅ Cleaner lambdas

✅ Powerful compile-time tools

✅ Ranges and spans for safer, cleaner code

✅ Type-safe string formatting

✅ Native coroutine support

Let me know in the comments — what’s your favorite C++20 feature? Have you started using any of these in production?


Follow me for more practical C++ insights. Happy coding!

Top comments (0)