Advanced C++ Features

Section 10: Advanced C++ Features

Lesson 1: Lambda Expressions and C++11 Features

1.1 Introduction to Lambda Expressions

Lambda expressions, introduced in C++11, provide a concise way to create anonymous functions. They are particularly useful for writing short, inline functions.

Example (Lambda Expression in C++): 

#include <iostream>


int main() {

    // Lambda function that adds two numbers

    auto add = [](int a, int b) { return a + b; };


    // Using the lambda function

    int result = add(3, 4);

    std::cout << "Result: " << result << std::endl;


    return 0;

}


1.2 Overview of C++11 Features

C++11 introduced several features to enhance the language, including:

Auto keyword: Automatic type inference.

Range-based for loops: Simplifies iteration over elements in a range.

nullptr: Null pointer representation.

Smart pointers: std::unique_ptr, std::shared_ptr, and std::weak_ptr for memory management.

Example (C++11 Features): 

#include <iostream>

#include <vector>


int main() {

    // Auto keyword for automatic type inference

    auto number = 42;


    // Range-based for loop

    std::vector<int> numbers = {1, 2, 3, 4, 5};

    for (const auto& n : numbers) {

        std::cout << n << " ";

    }


    // nullptr for null pointer

    int* ptr = nullptr;


    // Smart pointers for memory management

    std::unique_ptr<int> uniquePtr(new int(42));

    std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);


    return 0;

}


Lesson 2: Concurrency in Modern C++

2.1 Asynchronous Programming with std::async and std::thread

Modern C++ provides facilities for asynchronous programming using std::async and std::thread. std::async allows running functions asynchronously and obtaining a std::future to retrieve the result.

Example (Asynchronous Programming with std::async): 

#include <iostream>

#include <future>


// Function to perform a time-consuming task

int timeConsumingTask() {

    // Simulate a time-consuming task

    std::this_thread::sleep_for(std::chrono::seconds(2));

    return 42;

}


int main() {

    // Asynchronously run the time-consuming task

    std::future<int> result = std::async(std::launch::async, timeConsumingTask);


    // Do other work while waiting for the result

    std::cout << "Doing some other work..." << std::endl;


    // Get the result when needed

    int finalResult = result.get();

    std::cout << "Result: " << finalResult << std::endl;


    return 0;

}

Example (Concurrency with std::thread):

#include <iostream>

#include <thread>


// Function to be executed by a thread

void threadFunction() {

    std::cout << "Thread is running!" << std::endl;

}


int main() {

    // Create a thread and execute the thread function

    std::thread myThread(threadFunction);


    // Do other work in the main thread

    std::cout << "Main thread is doing some work..." << std::endl;


    // Wait for the thread to finish

    myThread.join();


    return 0;

}

Understanding lambda expressions, C++11 features, and modern concurrency tools like std::async and std::thread allows developers to write more expressive and efficient C++ code. Practice using these features to enhance your C++ programming skills