C++17
Programming Language
Software Development
Code Updates
Technology News

What are the new features in C++17?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

C++17, officially known as ISO/IEC 14882:2017, is an update to the C++ language standard, following C++14. It includes several new language features, library updates, and improvements aimed at increasing the performance, reliability, and clarity of C++ code. Let's discuss some significant features and details of C++17.

Language Features

1. Structured Bindings

Structured bindings allow unpacking tuple, pair, or struct into individual named variables.

cpp
1#include <tuple>
2std::tuple<int, double, std::string> get_data() {
3    return {1, 2.3, "hello"};
4}
5
6auto [x, y, z] = get_data();

2. if and switch with Initializers

This feature allows an initializer statement before the condition in if and switch statements, improving scope control and readability.

cpp
1#include <map>
2int main() {
3    std::map<int, std::string> m = {{1, "one"}};
4    if (auto it = m.find(1); it != m.end()) {
5        std::cout << it->second; // Prints "one"
6    }
7}

3. Inline Variables

Inline variables provide a way to define variables in header files without multiple definition errors, simplifying the management of global constants.

cpp
// header.h
inline const int max_value = 100;

Library Features

1. Filesystem Library

The filesystem library (<filesystem>) provides facilities for performing operations on file systems and their components.

cpp
1#include <filesystem>
2namespace fs = std::filesystem;
3
4int main() {
5    fs::create_directory("example");
6}

2. Parallel Algorithms

The Standard Library now includes support for parallel execution of algorithms.

cpp
1#include <vector>
2#include <algorithm>
3#include <execution>
4
5int main() {
6    std::vector<int> vec {1, 2, 3, 4};
7
8    std::for_each(std::execution::par, vec.begin(), vec.end(), [](int& x) {
9        x *= 2;
10    });
11}

New Template Features

1. std::variant, std::optional, and std::any

These types provide more flexible data structures that can be used to represent a value that may either hold a value of one type (std::optional), any type (std::any), or one of a set of types (std::variant).

cpp
1#include <variant>
2#include <optional>
3#include <any>
4
5std::variant<int, float> v = 1; // Can be either int or float
6std::optional<std::string> opt = "example"; // May or may not hold a string
7std::any a = 1.5; // Can hold any type

Language Clarifications and Fixes

  • constexpr Lambda Expressions: Lambdas can now be constexpr, enhancing the capabilities of compile-time computations.
  • Fold Expressions: Simplifies the generation of variadic template code using fold expressions for arbitrary operations.

Summary Table

FeatureDescriptionExample Usage
Structured BindingsUnpack data structures into individual named elementsauto [x, y, z] = get_tuple();
if and switch InitializersAdd initializers in conditionalsif (auto it = m.find(key); it != m.end())
Inline VariablesAllow inline global variablesinline const int max_value = 100;
Filesystem LibraryPerform operations on file systemsfs::create_directory("example");
Parallel AlgorithmsExecute algorithms in parallelstd::for_each(std::execution::par,...);
std::variantHold one of a set of typesstd::variant<int, float> v = 1;
std::optionalRepresent an optional valuestd::optional<std::string> opt = "example";
std::anyHold a value of an arbitrary typestd::any a = 1.5;
constexpr Lambda ExpressionsUse lambdas in constant expressionsauto f = []() constexpr &#123; return 1; &#125;;
Fold ExpressionsSimplify generation of variadic templatestemplate<typename... Args>(Args... args) &#123; return (args + ...); &#125;

Additional Considerations

In addition to these features, C++17 also makes several smaller feature adjustments and bug fixes to improve the overall language consistency. These updates reflect the ongoing evolution of C++ to meet the needs of modern software development while maintaining backward compatibility with older code bases.

For developers, migrating to C++17 offers opportunities for cleaner, more efficient, and maintainable code with robust library support and enhanced language facilities.


Course illustration
Course illustration

All Rights Reserved.