Lambda: More Capturing

More Capture Syntax

  • Lambdas are constantly improved as C++ evolves

  • Find below a number of examples

Capturing Explicitly, By Copy

Important

A matter of taste, merely: don’t cature all variables, by just saying [], or [&] (see below)

  • ⟶ sign of lazyness, akin to using global variables

  • ⟶ be explicit

#include <gtest/gtest.h>

TEST(lambda_capure_suite, explicit_by_copy)
{
    int x = 1, y = 2;

    auto fun = [x, y](){
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 3);
}

Capturing Explicitly, By Reference

#include <gtest/gtest.h>

TEST(lambda_capure_suite, explicit_by_reference)
{
    int x = 1, y = 2;

    auto fun = [&x, &y](){
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 4);
}

Mixed Explicit Capture

Dubios, for reasons mentioned above

#include <gtest/gtest.h>

TEST(lambda_capure_suite, explicit_mixed)
{
    int x = 1, y = 2;

    auto fun = [&x, y](){   // x by reference, y by copy
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 4);

    y = 3;
    ASSERT_EQ(fun(), 4);
}

Define Variables In Capture

#include <gtest/gtest.h>

TEST(lambda_capure_suite, initialized_capture)
{
    auto fun = [x=1, y=2](){
        return x + y;
    };

    ASSERT_EQ(fun(), 3);
}

Capturing All By Reference

#include <gtest/gtest.h>

TEST(lambda_capure_suite, all_by_reference)
{
    int x = 1, y = 2;

    auto fun = [&](){   // <-- all by reference
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 4);
}

Capturing All By Copy

#include <gtest/gtest.h>

TEST(lambda_capure_suite, all_by_copy)
{
    int x = 1, y = 2;

    auto fun = [=](){   // <-- all by copy
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 3);
}

Capturing All By Copy, Except Some By Reference

#include <gtest/gtest.h>

TEST(lambda_capure_suite, all_by_copy_except)
{
    int x = 1, y = 2;

    auto fun = [=, &y](){   // <-- all by copy, except y by reference
        return x + y;
    };

    ASSERT_EQ(fun(), 3);

    x = 2;
    ASSERT_EQ(fun(), 3);

    y = 3;
    ASSERT_EQ(fun(), 4);
}

Capturing this

#include <gtest/gtest.h>

class Integer
{
public:
    Integer(int i) : _i{i} {}

    void add_to_yourself_and_guarantee_programmers_job_security(int addend)
    {
        auto f = [this, addend](){
            _i += addend;     // <--- access _i as-if lambda were a class method
        };
        f();
    }

    operator int() const { return _i; }

private:
    int _i;
};

TEST(lambda_capure_suite, this_capture)
{
    Integer i{42};
    i.add_to_yourself_and_guarantee_programmers_job_security(7);
    ASSERT_EQ(i, 49);
}