C/C++ Arena

Modern C++: C++17 and C++20 features

The modern C++ features worth knowing, from auto and structured bindings to std::optional, concepts and ranges.

Modern C++ (C++11 onward, and especially C++17 and C++20) is much friendlier than older styles:

Together with smart pointers and RAII, these let you write code that is both fast and hard to misuse.

Example

#include <iostream>
#include <map>
#include <optional>
#include <string>

std::optional<int> find_age(const std::map<std::string, int> &m, const std::string &name) {
    auto it = m.find(name);
    if (it == m.end()) return std::nullopt;
    return it->second;
}

int main() {
    std::map<std::string, int> ages{{"Ada", 36}};
    auto a = find_age(ages, "Ada");
    auto b = find_age(ages, "Bob");
    std::cout << a.value_or(-1) << " " << b.has_value() << "\n";
    return 0;
}

Output:

36 0

Practice it