如何保存一个函数指针以便以后在c++中使用,类似的闭包有一个保存的状态

uinbv5nw  于 2022-11-27  发布在  其他
关注(0)|答案(2)|浏览(86)

我是一个c新手,所以我不知道如何写这个,但基本上我想要一个函数,接受一些参数,并返回一个函数指针,不需要任何参数,并可以执行供以后使用。
我知道c
没有闭包,但是可以用lambda表达式得到一些相同的效果。我只是不确定它是否能做我想让它做的事情。再说一次,我对c了解不多。我一直在看教程和阅读关于lambda在c中如何工作的帖子,但是我不知道如何让这段代码工作。
下面是我在typescript中尝试的一些示例代码

let myVariable;

const myClosure = (param1: number, param2: number, param3, string, ) => {
    return () => {
        // Do something with params
        console.log(param1, param2, param3);
    }
}

function whereInitalized() {
    myVariable = myClosure(1,2,"name");

}

function whereExecuted() {
    myVariable(); // prints the params
}

whereInitalized();
whereExecuted();

这是我在C++中想要的,但它是错误的

// Not correct syntax or type
// Having trouble getting typing for this variable;
std::function<void(param1: T, param2: P, param3: V)> (*myVariable)() = myClosure;

std::function<void()> myClosure(param1: T, param2: P, param3: V) {
    return []() { // Returns a function that does not take a parameter
        param1.someMethod();
        param2->Call(blah, blah);
        // ... More work
        
    };
}

void functionWhereInitalized() {
    myVariable = myClosure(param1, param2, param3);
}

void functionWhereExecuted() {
    myVariable();
}

下面是我在C++中所做的,可以工作,但不能接受参数

std::function<void()> myVariable = myClosure;

std::function<void()> myClosure() {
    return [num = 99]() mutable {
        // Test code to see it gets called
        num++; 
        std::cout << num << "  --  " << "\n";
    };
}

void functionWhereInitalized() {
    myVariable = myClosure();
}

void functionWhereExecuted() {
    myVariable();
}

感谢您提前回复!

kg7wmglp

kg7wmglp1#

在回答技术问题之前,我同意Sam Varshavchik的意见:
你说你“不太懂C++"。不幸的是,你学到C的第一件事是它不是即时的满足。学习它需要时间,很长一段时间。你正在描述C库中的一个基本模板,但要达到这一点,有必要学习和学习核心C基础知识,大约一两年。然后再讨论模板之类的高级主题。任何试图缩短这一过程的尝试最终都会以失败告终。C太复杂了,不可能通过在Stackoverflow上一次问一个问题来学习。
现在来谈谈技术问题:您可以使用lambda capture 1实现这里描述的简单闭包:

#include <iostream>
#include <string_view>

auto make_closure(std::ostream& out, std::string_view message, int repeat=1)
{
    return [&out,repeat,message]() mutable { while (repeat --> 0) out << message; };
}

int main(){
    auto say_hello = make_closure(std::cout, "Hello\n", 2);
    say_hello();
}

Live demo
1)的
捕获是一个逗号分隔的零个或多个捕获的列表,可以选择以capture-default开头。捕获列表定义了可从lambda函数体中访问的外部变量。唯一的捕获默认值是
&(通过引用隐式捕获使用的自动变量)和=(通过复制隐式捕获所使用的自动变量).当前对象(*this)可以隐式捕获。如果隐式捕获,则始终通过引用捕获,即使撷取预设值是=。当撷取预设值是=时,*this的隐含撷取已被取代。(C++20起)

k5hmc34c

k5hmc34c2#

std::function<void(param1: T, param2: P, param3: V)> (*myVariable)() = myClosure;

我不太清楚这里发生了什么事我猜你一定想做这种事

std::function<void()> yourClosure(T1 const& p1, T2 const& p2, T3 const& p3)
{
  return [p1, p2, p3]() { /* do the thing */};
}

但如果您只是想存储函数以备将来使用,您实际上可以

auto const function = [a,b,c](){ /* meaningful code*/ };

some_types... a, b, c; // whatever variables you have
auto const function_with_params = [](auto const& a, auto const& b, auto const&c){ /* skrnyr dgdyr */};
auto const function_with_bound_params = std::bind(function_with_params, a, b, c);

lambda版本和绑定版本都应该可以强制转换为std::function<void()>

相关问题