gpt4 book ai didi

c++ - 是否可以在 lambda 中捕获可变数量的参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:08 27 4
gpt4 key购买 nike

考虑以下一组示例。

  1. 函数 takeOnlyVoidFunction 接受一个零参数的函数并简单地执行它。
  2. takeVariableArguments 函数接受可变数量的参数并使用这些参数执行函数。
  3. 函数 captureVariableArgs 试图将第二个函数转换为第一个函数可接受的 lambda 形式,但它没有编译。

如何使函数 captureVariableArgs 编译并展示将具有可变数量参数的函数转换为不带参数的闭包的正确行为?

#include <stdio.h>
#include <functional>

void takeOnlyVoidFunction(std::function<void()> task) {
task();
}

template<typename _Callable, typename... _Args>
void takeVariableArguments(_Callable&& __f, _Args&&... __args) {
__f(__args...);
}

// How can I make this function compile?
template<typename _Callable, typename... _Args>
void captureVariableArgs(_Callable&& __f, _Args&&... __args) {
takeOnlyVoidFunction([=]() { __f(__args...);});
}

void normalFunction(int a, int b) {
printf("I am a normal function which takes params (%d,%d)\n", a, b);
}

int main() {
int a = 7;
int b = 8;
takeVariableArguments(normalFunction, a, b);
takeOnlyVoidFunction([=](){ normalFunction(a,b);});
captureVariableArgs(normalFunction, a, b);
}

我正在运行 gcc 4.9.2。这是我看到的编译器错误。

g++ -std=c++11    Test.cc   -o Test
Test.cc: In instantiation of ‘captureVariableArgs(_Callable&&, _Args&& ...)::<lambda()> [with _Callable = void (&)(int, int); _Args = {int&, int&}]’:
Test.cc:16:38: required from ‘struct captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’
Test.cc:16:50: required from ‘void captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]’
Test.cc:28:45: required from here
Test.cc:16:34: error: variable ‘__f’ has function type
takeOnlyVoidFunction([=]() { __f(__args...);});
^
Test.cc:16:34: error: variable ‘__f’ has function type
Test.cc: In instantiation of ‘struct captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’:
Test.cc:16:50: required from ‘void captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]’
Test.cc:28:45: required from here
Test.cc:16:34: error: field ‘captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>::<__f capture>’ invalidly declared function type
In file included from Test.cc:2:0:
/usr/include/c++/4.9/functional:2418:7: error: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {}]’, declared using local type ‘captureVariableArgs(_Callable&&, _Args&& ...) [with _Callable = void (&)(int, int); _Args = {int&, int&}]::<lambda()>’, is used but never defined [-fpermissive]
function<_Res(_ArgTypes...)>::
^

更新:一个更简单的例子来说明这个问题。

#include <stdio.h>

// How can I make this function compile?
template<typename _Callable>
void captureVariableArgs(_Callable&& __f) {
takeOnlyVoidFunction( [=]{ __f(); } );
}

void normalFunction() {
printf("I am a normal function\n");
}

int main(){
captureVariableArgs(normalFunction);
}

最佳答案

作为 GCC 的另一个潜在解决方法,您可以使用 std::bind 而不是使用 lambda:

template <typename F, typename... Args>
auto captureVariable(F&& f, Args&&... args)
{
return std::bind(std::forward<F>(f), std::forward<Args>(args)...);
}

这适用于 GCC 4.9.3。

关于c++ - 是否可以在 lambda 中捕获可变数量的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com