Auto Keyword | The auto keyword allows the compiler to automatically deduce the type of a variable at compile-time. This leads to cleaner and more maintainable code, as it reduces the need to explicitly specify types. | auto x = 5; // x is an int auto y = 3.14; // y is a double |
---|
Range-Based For Loop | C++11 introduced a new syntax for iterating over containers, known as the range-based for loop. This feature simplifies loops by eliminating the need for explicit iterators. | for (auto& i : v) { //code} |
---|
Lambda Expressions | Lambda expressions allow you to define anonymous functions directly in your code. This is particularly useful for short, inline functions and callbacks. | auto add = [](int a, int b) {return a + b; }; |
---|
Smart Pointers | C++11 introduced std::unique_ptr and std::shared_ptr , which automate memory management and prevent memory leaks by automatically deallocating memory when it's no longer needed. | unique_ptr<int> p = make_unique<int>(10); |
---|
Move Semantics and Rvalue References | Move semantics and rvalue references (&& ) allow for more efficient memory management by enabling the transfer of resources from temporary objects, reducing unnecessary copying. | vector<int> v2 = move(v1); |
---|
constexpr | The constexpr keyword allows the evaluation of functions at compile-time, enabling better optimization and potentially leading to faster code. | constexpr int square(int x) { return x * x; } |
---|
nullptr | C++11 introduced nullptr to represent null pointers, replacing the older NULL macro and avoiding type-related ambiguities. | int* ptr = nullptr; |
---|
Variadic Templates | Variadic templates allow functions and classes to accept any number of template arguments, enabling more flexible and generalized code. | template<typename... Args> |
---|
Type Inference with decltype | The decltype keyword allows to deduce the type of an expression, providing greater flexibility when writing generic code. | decltype(x) |
---|
Uniform Initialization | C++11 introduced a uniform way to initialize variables using braces, which works for any type, including arrays, structs, and classes. | int arr[] = {1, 2, 3}; vector<int> vec{4, 5, 6}; |
---|
Strongly-Typed Enums | C++11 introduced strongly-typed enums (enum class ) to avoid name clashes and improve type safety. | enum class Color { Red, Green, Blue }; |
---|
Attributes | C++11 introduced a standardized way to specify attributes in code using [[attribute]] syntax, enabling more granular control over compiler behavior. | [[nodiscard]] int compute() { return 42; } |
---|