Home > Mobile >  Move (or copy) capture variadic template arguments into lambda
Move (or copy) capture variadic template arguments into lambda

Time:10-07

I am attempting to figure out how to move (or just copy if a move is not available) variadic parameters into a lambda within a templated function.

I am testing this with a move-only class (see below) because this would be the "worst-case" that needs to work with my template.

class MoveOnlyTest {
public:
    MoveOnlyTest(int a, int b = 20, int c = 30) : _a(a), _b(b), _c(c) {
        std::cout << "MoveOnlyTest: Constructor" << std::endl;
    }
    
    ~MoveOnlyTest() {
        std::cout << "MoveOnlyTest: Destructor" << std::endl;
    }
    
    MoveOnlyTest(const MoveOnlyTest& other) = delete;

    MoveOnlyTest(MoveOnlyTest&& other) :
        _a(std::move(other._a)),
        _b(std::move(other._b)),
        _c(std::move(other._c))
    {
        std::cout << "MoveOnlyTest: Move Constructor" << std::endl;
        
        other._a = 0;
        other._b = 0;
        other._c = 0;
    }
    
    MoveOnlyTest& operator=(const MoveOnlyTest& other) = delete;

     MoveOnlyTest& operator=(MoveOnlyTest&& other) {
        if (this != &other) {
            _a = std::move(other._a);
            _b = std::move(other._b);
            _c = std::move(other._c);
            
            other._a = 0;
            other._b = 0;
            other._c = 0;
        
            std::cout << "MoveOnlyTest: Move Assignment Operator" << std::endl;
        }
        
        return *this;
    }
    
    friend std::ostream& operator<<(std::ostream& os, const MoveOnlyTest& v) {
        os << "{a=" << v._a << "}";
        return os;
    }
    
private:
    int _a;
    int _b;
    int _c;
};

And here is the test code I am attempting to get working:

void test6() {
    std::cout << "--------------------" << std::endl;
    std::cout << "       TEST 6       " << std::endl;
    std::cout << "--------------------" << std::endl;
    
    MoveOnlyTest v(1, 2, 3);
    
    test6_A(std::move(v));
}

void test6_A(MoveOnlyTest v) {
    std::cout << "test6_A()" << std::endl;
    
    test6_B(test6_C, v);
}

template <typename ... ARGSF, typename ... ARGS>
void test6_B(void(*fn)(ARGSF...), ARGS&&... args) {
    std::cout << "test6_B()" << std::endl;
    
    //What do I need to get args to be moved/copied into the lambda
    auto lambda = [fn, args = ???]() mutable {
        (*fn)( std::forward<ARGS>(args)... );
    };
    
    lambda();
}

void test6_C(MoveOnlyTest v) {
    std::cout << "test6_C()" << std::endl;
    
    std::cout << "v = " << v << std::endl;
}

I am trying to have the exact same behavior as below, only using a generic template so that I can create a lambda which captures and arguments, and calls any function with those arguments.

void test5() {
    std::cout << "--------------------" << std::endl;
    std::cout << "       TEST 5       " << std::endl;
    std::cout << "--------------------" << std::endl;
    
    MoveOnlyTest v(1, 2, 3);
    
    test5_A(std::move(v));
}

void test5_A(MoveOnlyTest v) {
    std::cout << "test5_A()" << std::endl;
    
    auto lambda = [v = std::move(v)]() mutable {
        test5_B(std::move(v));
    };
    
    lambda();
}

void test5_B(MoveOnlyTest v) {
    std::cout << "test5_B()" << std::endl;
    
    std::cout << "v = " << v << std::endl;
}

To be clear, I don't want to perfectly capture the arguments as in c lambdas how to capture variadic parameter pack from the upper scope I want to move them if possible and, if not, copy them (the reason being is that I plan to store this lambda for later execution thus the variables in the stack will no longer be around if they are just captured by reference).

CodePudding user response:

To be clear, I don't want to perfectly capture the arguments as in c lambdas how to capture variadic parameter pack from the upper scope I want to move them if possible

Just using the same form:

auto lambda = [fn, ...args = std::move(args)]() mutable {
  (*fn)(std::move(args)...);
};

In C 17, you could do:

auto lambda = [fn, args = std::tuple(std::move(args)...)]() mutable {
  std::apply([fn](auto&&... args) { (*fn)( std::move(args)...); }, 
             std::move(args));
};
  • Related