C++14, C++17, and C++20 are the successive updates to the C++ language standard, each adding new features and improving the language. Here are some notable changes in each of these standards:
C++14:
Generic lambdas: allows lambda functions to use auto parameters, which simplifies code and enables template metaprogramming. Return type deduction: allows functions to omit the return type in certain cases, where it can be deduced from the expression returned. std::make_unique: a new utility function for creating unique_ptr objects, which simplifies the code and improves memory safety.
Example of generic lambda in C++14:
auto add = [](auto a, auto b) { return a + b; };
int sum = add(1, 2); // sum is 3
double total = add(3.5, 2.7); // total is 6.2
C++17:
Structured bindings: allows the decomposition of a tuple or struct into named variables, which simplifies code and improves readability. constexpr if: allows conditionals to be evaluated at compile-time, enabling more efficient code and better type checking. Fold expressions: allows operations to be applied to multiple arguments in a variadic template, which simplifies code and enables new kinds of metaprogramming.
Example of structured bindings in C++17:
std::tuple<int, double> tpl = {42, 3.14};
auto [a, b] = tpl;
std::cout << a << ", " << b << std::endl; // prints "42, 3.14"
C++20:
Concepts: a new language feature that allows the specification of requirements on template arguments, which improves code readability and reduces errors. Ranges: a new library feature that provides a unified way of working with containers and other iterable objects, which simplifies code and enables better performance. std::span: a new library type for representing contiguous arrays of objects, which simplifies code and improves performance.
Example of concepts in C++20:
template<typename T>
concept Integral = std::is_integral_v<T>;
template<Integral T>
T add(T a, T b) { return a + b; }
int sum = add(1, 2); // sum is 3
double total = add(3.5, 2.7); // error: concept not satisfied
In summary, each new version of the C++ standard brings new features and improvements to the language, making it more expressive, safer, and easier to use. It is important for C++ developers to stay up-to-date with the latest standards and to make use of the new features where appropriate.