Skip to main content

Lesson 7 of 7

Practice & Quiz - Modern C++11-17

Test your mastery of auto deduction, lambda captures, rvalue references, std::move, optional/variant, and thread basics with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Event Dispatcher with Lambdas

Practical Exercise - Event Dispatcher with Lambdas
#include <iostream>
#include <functional>
#include <unordered_map>
#include <vector>
#include <string>
#include <string_view>
#include <optional>

// Event bus - stores lambdas by event name
class EventBus {
    std::unordered_map<std::string,
        std::vector<std::function<void(const std::string&)>>> handlers_;
    int fire_count_ = 0;

public:
    // Subscribe - returns handler ID for unsubscription
    int on(std::string event, std::function<void(const std::string&)> fn) {
        auto& vec = handlers_[std::move(event)];
        vec.push_back(std::move(fn));
        return (int)vec.size() - 1;
    }

    // Fire - invoke all handlers for event
    void emit(std::string_view event, const std::string& payload) {
        ++fire_count_;
        auto it = handlers_.find(std::string(event));
        if (it == handlers_.end()) return;
        for (const auto& fn : it->second) fn(payload);
    }

    // optional - returns nothing if event has no handlers
    std::optional<int> handler_count(std::string_view event) const {
        auto it = handlers_.find(std::string(event));
        if (it == handlers_.end()) return std::nullopt;
        return (int)it->second.size();
    }

    int total_fires() const { return fire_count_; }
};

int main() {
    EventBus bus;
    int user_login_count = 0;

    // Lambda captures by reference - modifies outer var
    bus.on("user.login", [&user_login_count](const std::string& user) {
        ++user_login_count;
        std::cout << "Auth: " << user << " logged in\n";
    });
    bus.on("user.login", [](const std::string& user) {
        std::cout << "Log: login event for " << user << "\n";
    });
    bus.on("user.logout", [](const std::string& user) {
        std::cout << "Log: logout event for " << user << "\n";
    });

    bus.emit("user.login",  "Alice");
    bus.emit("user.login",  "Bob");
    bus.emit("user.logout", "Alice");

    std::cout << "Login handlers: " << bus.handler_count("user.login").value_or(0) << "\n";
    std::cout << "Total fires:    " << bus.total_fires() << "\n";
    std::cout << "Login count:    " << user_login_count << "\n";

    // optional for non-existent event
    if (!bus.handler_count("payment.failed")) {
        std::cout << "No handlers for payment.failed\n";
    }
    return 0;
}

Module Quiz

Module Quiz
Question 1 of 5

`auto x = get_ref()` where `get_ref()` returns `int&`. What is the type of x?

Tip

Tip

Practice Practice Quiz Modern C1117 in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

auto/decltype, Generic Lambdas, Move Semantics, std::optional/variant & Thread Concurrency

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Modern C1117 from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Modern C1117 is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Topics in This Module

0/7 done