C++ 17
C++17 introduces several new features and improvements that make the language more expressive, safer, and efficient.
Let’s delve into some of the key features of C++17 with examples:
Table of Contents
Structured Bindings
C++17 introduces the ability to unpack the elements of a tuple or any other user-defined type into individual variables using structured bindings.
#include <tuple>
#include <iostream>
std::tuple<int, double, std::string> getValues() {
return std::make_tuple(42, 3.14, "hello");
}
int main() {
auto [x, y, z] = getValues();
std::cout << x << ", " << y << ", " << z << std::endl;
return 0;
}
if with Initializer
In C++17, you can use the if
statement with an initializer, allowing you to declare and initialize variables directly inside the condition.
if (int num = someFunction(); num > 0) {
// num is only accessible inside this scope
// if the condition is true.
}
constexpr Improvements
C++17 extends the usability of constexpr to include more scenarios, such as if statements and loops, making it easier to write compile-time computations.
constexpr int factorial(int n) {
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
std::optional
C++17 introduces std::optional
, which is a container that either holds a value or nothing. It provides a safe way to handle optional values without using pointers or default values.
#include <optional>
#include <iostream>
std::optional<int> getValue(bool condition) {
if (condition) return 42;
else return std::nullopt;
}
int main() {
auto value = getValue(true);
if (value) {
std::cout << "Value is present: " << *value << std::endl;
} else {
std::cout << "Value is not present." << std::endl;
}
return 0;
}
std::variant
std::variant
is a type-safe union that can hold a value from a specified set of types. It provides a safer alternative to traditional unions.
#include <variant>
#include <iostream>
#include <string>
int main() {
std::variant<int, double, std::string> data;
data = 42;
std::cout << std::get<int>(data) << std::endl;
data = 3.14;
std::cout << std::get<double>(data) << std::endl;
data = "hello";
std::cout << std::get<std::string>(data) << std::endl;
return 0;
}
std::string_view:
std::string_view
is a non-owning view of a string, allowing you to work with substrings or parts of a string without copying or owning the underlying data.
#include <iostream>
#include <string_view>
void printString(std::string_view str) {
std::cout << str << std::endl;
}
int main() {
std::string_view sv = "Hello, C++17!";
printString(sv.substr(7)); // Output: C++17!
return 0;
}
std::filesystem
C++17 introduces the <filesystem>
library, providing an easy and standardized way to work with files and directories.
#include <iostream>
#include <filesystem>
int main() {
std::filesystem::path filePath = "/path/to/file.txt";
if (std::filesystem::exists(filePath)) {
std::cout << "File exists!" << std::endl;
} else {
std::cout << "File does not exist." << std::endl;
}
return 0;
}
These are just a few of the many features introduced in C++17. The update aimed to enhance the language with modern conveniences while maintaining backward compatibility with older C++ code.