Skip to content

move_only_function does not carry mutable lambda state over multiple calls #23

@ccaughie

Description

@ccaughie

boost::compat::move_only_function appears to differ from std::move_only_function when used with a mutable lamba that modifies captured variables. Specifically, the captured value appears to revert to its initial state each time the wrapped function is called, instead of preserving the new state as one would expect.

This can be demonstrated with the following program:

#include <boost/compat/move_only_function.hpp>
#include <iostream>

template<typename Sig>
using move_only_function = boost::compat::move_only_function<Sig>;

// For comparison:
// using move_only_function = std::move_only_function<Sig>;

int main()
{
    int captured = 0;
    move_only_function<int()> func = [captured]() mutable { return ++captured; };

    int result = func();
    std::cout << "Result 1: " << result << "\n";

    result = func();
    std::cout << "Result 2: " << result << "\n";
}

Output with the Boost version:

Result 1: 1
Result 2: 1

Output with the std version:

Result 1: 1
Result 2: 2

Tested with Boost 1.90.0 using gcc 13.3.0 on Ubuntu 24.04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions