遅延代入

BoostML - 【lambda】 delayed assignment to a member

#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>

int main()
{
    int value = 1;

    boost::function<void()> assign = boost::lambda::var(value) = 3;

    std::cout << value << std::endl; // 1

    assign();

    std::cout << value << std::endl; // 3
}

メンバ変数の場合も同様。

#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>

struct hoge {
    int member;
};

int main()
{
    hoge h;
    h.member = 1;

    boost::function<void()> assign = boost::lambda::var(h.member) = 3;

    std::cout << h.member << std::endl; // 1

    assign();

    std::cout << h.member << std::endl; // 3
}