C++ 11

C++11, also known as C++0x during its development phase, is the name given to the revision of the C++ programming language standard that was ratified in 2011. This release brought significant improvements and new features to the language, enhancing its capabilities and making it more modern, efficient, and safer.

Below are some of the key features introduced in C++11:

Uniform Initialization

C++11 introduced uniform initialization syntax, which allows you to initialize variables and containers using braces {}. It works consistently for built-in types, user-defined types, and standard library containers.

   int number = 42;
   std::string message = "Hello";
   std::vector<int> numbers = {1, 2, 3, 4};

Auto keyword

The auto keyword enables automatic type inference, making it easier to declare variables without explicitly specifying their type.

   auto x = 5;         // x will be deduced as int
   auto name = "John"; // name will be deduced as const char*

Range-based for loop

C++11 introduced the range-based for loop, which simplifies iterating over elements in a container.

   std::vector<int> numbers = {1, 2, 3, 4};
   for (int num : numbers) {
       // Do something with 'num'
   }

Lambda Expressions

Lambdas allow you to create anonymous functions on-the-fly. They are especially useful in combination with standard algorithms and in scenarios where you need a short-lived function.

   auto add = [](int a, int b) { return a + b; };
   int result = add(2, 3); // result will be 5

Smart Pointers

C++11 introduced three types of smart pointers: std::unique_ptr, std::shared_ptr, and std::weak_ptr. They manage memory automatically, reducing the risk of memory leaks and making it easier to handle shared ownership of objects.

   std::unique_ptr<int> ptr = std::make_unique<int>(42);
   std::shared_ptr<int> sharedPtr = std::make_shared<int>(10);

Move Semantics

Move semantics enable efficient transfer of resources (like memory) from one object to another, avoiding unnecessary deep copies. This is particularly useful for improving performance in situations where deep copying is expensive.

   // Move constructor
   MyObject(MyObject&& other) noexcept;

   // Move assignment operator
   MyObject& operator=(MyObject&& other) noexcept;

Rvalue References

Rvalue references are used in conjunction with move semantics and allow you to distinguish between objects that can be safely moved from and those that cannot.

   // lvalue reference (cannot be moved from)
   T& operator=(const T& other);

   // rvalue reference (can be moved from)
   T& operator=(T&& other);

Null Pointer Constant

C++11 introduced nullptr, a new keyword representing the null pointer. It resolves some ambiguities related to using 0 as a null pointer and provides better type safety.

   int* ptr = nullptr;

Standard Threads

The <thread> header in C++11 provides a standard way to work with threads, allowing developers to create and manage concurrent execution.

   #include <thread>
   void threadFunction() {
       // Do some work in the thread
   }

   int main() {
       std::thread t(threadFunction);
       t.join(); // Wait for the thread to finish
       return 0;
   }

Atomic Operations

C++11 introduced atomic operations and types (std::atomic) to safely perform operations in multi-threaded environments without data races.

   #include <atomic>
   std::atomic<int> counter = 0;
   counter.fetch_add(1); // Atomic increment

These are some of the most significant features introduced in C++11. The release of C++11 was a major milestone in the evolution of the C++ language, and it has since paved the way for subsequent versions like C++14, C++17, and C++20, each bringing its own set of improvements and new features.