Passing a Lambda Closure into a Function - C++ STL

C++ examples for STL:Lambda

Description

Passing a Lambda Closure into a Function

Demo Code

#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <typeinfo>

using MyArray = std::array<int, 5>;

void PrintArray(const std::function<void(MyArray::value_type)>& myFunction)
{
    MyArray myArray{ 1, 2, 3, 4, 5 };//  ww w .j ava2s. c  o m

    std::for_each(myArray.begin(),
        myArray.end(),
        myFunction);
}

int main()
{
    auto myClosure = [](auto&& number) {
            std::cout << number << std::endl;
        };
    std::cout << typeid(myClosure).name() << std::endl;

    PrintArray(myClosure);

    return 0;
}

Result


Related Tutorials