What useful features and capabilities were added in C++14?
sobes.tech AI
Answer from AI
-
Generic Lambda Captures: Allows capturing variables by value or reference with automatic type deduction.
// C++14 auto lambda = [x = get_value()] { return x; }; -
Return Type Deduction for Normal Functions: The compiler can determine the return type of a function if it can be deduced from
return.// C++14 auto func() { return 42; // return type is int } -
Binary Literals: Allows writing numbers in binary notation using prefixes
0bor0B.// C++14 int binary_value = 0b1101; // 13 -
Digit Separators in Numeric Literals: Allows using apostrophe
'for visual separation of digits in numeric literals.// C++14 long big_number = 1'000'000; -
decltype(auto): The behavior ofdecltypeapplies to automatic type deduction. Preserves reference andconst,volatilequalifiers.// C++14 int x = 5; int& ref = x; decltype(auto) result = ref; // result has type int& -
Extended
constexprcapabilities:constexprfunctions can containif,for,whilestatements.- Can be used with functions returning
void. - Local variables are allowed.
// C++14 constexpr int factorial(int n) { int res = 1; for (int i = 1; i <= n; ++i) { res *= i; } return res; } -
Guaranteed Copy Elision (Partial): Partially guarantees elimination of unnecessary copying in certain cases (e.g., returning rvalue from a function). Fully guaranteed in C++17.
-
Attributes: New standard attributes added:
[[deprecated]]: Indicates an outdated element, issuing a warning when used.[[carries_dependency]]: Used in multithreaded programming for optimization.
-
std::make_unique: Convenient way to create objects managed bystd::unique_ptr. Prevents some potential issues (e.g., memory leaks in case of exceptions).// C++14 auto ptr = std::make_unique<int>(10); -
std::shared_timed_mutex: Added for implementing read/write locks. -
Small improvements in the standard library: Added
std::integer_sequence,std::exchange,std::quoted,std::getfor tuples by type.