Info libri per c++ 20

Pubblicità
Se lo conosci già, eviterei di comprare un testo... piuttosto guarderei il reference ufficiale online.
Rischi di prendere un libro del quale conosci l'80% delle cose; fosse anche il 70%, avresti comunque buona parte del testo li per nulla.

Puoi sfruttare ChatGPT per queste cose secondo me.

Between C++13 (which is often referred to as C++14) and C++20, the language has seen several significant changes and additions. Here's an overview of the major features introduced in each version:

C++14 (2014)​

  1. Binary Literals:
    • You can now use binary literals for integral values using the prefix 0b or 0B.
    • Example: int bin = 0b1010; // 10 in binary
  2. Digit Separators:
    • The single quote (') can be used to separate digits in numeric literals for better readability.
    • Example: int large_number = 1'000'000;
  3. Generic Lambdas:
    • Lambdas can now have auto parameters, making them generic.
    • Example: auto lambda = [](auto x, auto y) { return x + y; };
  4. Return Type Deduction for Functions:
    • You can omit the return type, and it will be automatically deduced.
    • Example: auto add(int a, int b) { return a + b; }
  5. Relaxed constexpr Restrictions:
    • constexpr functions can now have more complex logic, including multiple statements and loops.
    • Example: constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); }
  6. std::make_unique:
    • Provides a safer way to create unique pointers.
    • Example: auto ptr = std::make_unique<int>(10);
  7. Extended constexpr:
    • More complex expressions and functions are allowed in constexpr contexts.
  8. Variable Templates:
    • Allows the definition of variable templates, which are templates that represent variables.
    • Example: template<typename T> constexpr T pi = T(3.1415926535897932385);

C++17 (2017)​

  1. Structured Bindings:
    • Allows you to unpack tuples and other types with multiple return values.
    • Example: auto [a, b] = std::pair<int, int>{1, 2};
  2. If and Switch with Initializers:
    • You can now include an initializer in the if and switch statements.
    • Example: if (auto x = foo(); x > 0) { /* ... */ }
  3. Inline Variables:
    • Variables can be declared inline to avoid multiple definitions.
    • Example: inline int x = 10;
  4. std::optional:
    • Represents an optional value that might or might not be present.
    • Example: std::optional<int> opt = 5;
  5. std::variant:
    • A type-safe union, allowing a variable to hold one of several types.
    • Example: std::variant<int, double> v = 42;
  6. std::any:
    • A type-safe container for single values of any type.
    • Example: std::any a = 10;
  7. Fold Expressions:
    • Simplifies the syntax for variadic templates.
    • Example: template<typename... Args> void print(Args... args) { (std::cout << ... << args) << '\n'; }
  8. constexpr if:
    • Allows compile-time conditionals within templated code.
    • Example: if constexpr (condition) { /* code */ }
  9. std::filesystem:
    • Provides facilities for performing operations on file systems and their components.
    • Example: std::filesystem::path p = "/tmp";
  10. Parallel Algorithms:
    • Standard algorithms can now be executed in parallel.
    • Example: std::vector<int> v = /* ... */; std::sort(std::execution::par, v.begin(), v.end());
  11. std::string_view:
    • Provides a non-owning view into a string.
    • Example: std::string_view sv = "Hello, World!";

C++20 (2020)​

  1. Concepts:
    • Provides a way to specify constraints on template parameters.
    • Example: template<typename T> requires std::integral<T> T gcd(T a, T b) { /* ... */ }
  2. Ranges:
    • Introduces a new way to work with sequences of elements.
    • Example: std::vector<int> v = {1, 2, 3, 4}; auto even = v | std::views::filter([](int n) { return n % 2 == 0; });
  3. Coroutines:
    • Allows the definition of functions that can suspend execution to be resumed later.
    • Example: std::future<int> async_func() { co_return 42; }
  4. Modules:
    • Provides a way to modularize code and improve compilation times.
    • Example: import module_name;
  5. Three-Way Comparison (Spaceship Operator):
    • Introduces a new comparison operator (<=>) to simplify the definition of ordering.
    • Example: auto result = a <=> b;
  6. Calendar and Time Zone Library:
    • Enhances support for handling dates, times, and time zones.
    • Example: std::chrono::year_month_day date = std::chrono::year{2020}/12/25;
  7. std::span:
    • A non-owning view into a contiguous sequence of elements.
    • Example: std::array<int, 3> arr = {1, 2, 3}; std::span<int> s(arr);
  8. Constexpr Improvements:
    • Many standard library functions are now constexpr, allowing more compile-time computation.
    • Example: constexpr int max = std::max(10, 20);
  9. Enhanced Lambdas:
    • Lambdas can now capture *this and use template parameters.
    • Example: auto lambda = [*this](auto x) { return x + value; };
  10. Expanded Immediate Functions (consteval):
    • Introduces consteval for functions that must produce a compile-time constant.
    • Example: consteval int square(int x) { return x * x; }
  11. Concepts and Requires Clauses:
    • Constraints can be explicitly stated for templates.
    • Example: template<typename T> requires std::integral<T> T add(T a, T b) { return a + b; }
  12. Template Syntax Improvements:
    • Simplifies and extends template syntax, including abbreviated function templates.
    • Example: auto add(T a, T b) -> decltype(a + b);
  13. std::format:
    • Provides a type-safe and efficient way to format strings.
    • Example: std::string s = std::format("Hello, {}!", "World");
Each version of C++ introduced features to make the language more expressive, safer, and more efficient. It's beneficial to be familiar with these new features to write modern, idiomatic C++ code.

Ho scoperto che siamo a C++26.... ho un libro "The C++ Programming Language", versione 11.
 
Se lo conosci già, eviterei di comprare un testo... piuttosto guarderei il reference ufficiale online.
Rischi di prendere un libro del quale conosci l'80% delle cose; fosse anche il 70%, avresti comunque buona parte del testo li per nulla.

Puoi sfruttare ChatGPT per queste cose secondo me.


Ho scoperto che siamo a C++26.... ho un libro "The C++ Programming Language", versione 11.
Si lo so , potrei anche verificare il sito ufficiale ed aggiornarmi oppure chatgpt ma sono un nostalgico dei libri mi piace avere un supporto cartaceo..
 
Pubblicità
Pubblicità

Discussioni Simili

Indietro
Top