添加struct里使用lambda测试

This commit is contained in:
2025-06-20 17:26:36 +08:00
parent 362dc08ba5
commit 3e3c62941d
3 changed files with 38 additions and 14 deletions

View File

@@ -7,9 +7,17 @@
#include "main.h"
#include "lambda.h"
lambda_use
typedef struct test_struct{
int a;
int b;
int (*sub)(int a, int b);
int (*add)(int a, int b);
int (*sum)(struct test_struct* self);
} test_struct;
lambda_use
@@ -21,7 +29,19 @@ void run_callback(int (*fun)(int a, int b)) {
static test_struct test_struct_var = {
.a = 1,
.b = 2,
.sub = lambda(int(int a, int b) {
return a - b;
}),
.add = lambda(int(int a, int b) {
return a + b;
}),
.sum = lambda(int(struct test_struct* self) {
return self->a + self->b;
})
};
@@ -65,6 +85,10 @@ int thread_fun(void* t)
return a * b * 10;
}));
printf("test_struct_var.sub(1,2) = %d\n", test_struct_var.sub(1, 2));
printf("test_struct_var.add(1,2) = %d\n", test_struct_var.add(1, 2));
printf("test_struct_var.sum(&test_struct_var) = %d\n", test_struct_var.sum(&test_struct_var));
return 0;
}