我如何使用宏和函数指针在 C 语言中实现类似于 C++ 中的 lambda 函数的东西?

How could I implement in C language using macros and function pointers something similar to lambda functions in C++?

我想在 C 中实现类似于 C++ 中的 lambda 函数的东西(使用宏和函数指针)

我认为我面临的最大问题是在另一个函数中定义一个函数,而这在 C 中是不可能的。我认为更好的主意是将 lambda 函数(通过宏传递)视为一个词汇块。

我开始了一些代码:

#define func_name(line) func##line

#define line __LINE__

#define lambda(body, ret_type, ...)   ret_type  func_name(line)(__VA_ARGS__)  \
                                 {                     \
                                   body;               \
                                 }                     \


//#include <stdio.h>

lambda(  printf("Hello from Lambda\n") ,  void, int a, float b)



int main(void) {
  //printf("Hello World\n");
  return 0;
}

我使用带有“-E”选项的 gcc 编译器来查看预处理器输出:

  void funcline(int a, float b) { printf("Hello from Lambda\n"); }

  int main(void) {

  return 0;
  }

这是可能的,但 lambda 必须是无状态的(没有捕获)。您可以使用预处理器移动函数定义,但无法在另一个函数中使用局部变量,也无法在函数中引入某种状态。

这也使得代码很难调试。由于所有使用这些伪 lambda 的函数都必须包含在宏中,因此在预处理期间会删除其中的所有换行符。无法在此类函数内放置断点,也无法逐行前进。


这是一个用法示例。实施在答案的最后。语法的解释就在例子之后。

Run on gcc.godbolt.org

#include <stdio.h>
#include <stdlib.h>

ENABLE_LAMBDAS(

void example1()
{
    int arr[] = {4,1,3,2,5};

    FUNC(int)(compare)(const void *a, const void *b)
    (
        return *(int*)a - *(int*)b;
    )
    qsort(arr, 5, sizeof(int), compare);

    for (int i = 0; i < 5; i++ )
        printf("%d ", arr[i]);
    putchar('\n');
}

void example2()
{
    int arr[] = {4,1,3,2,5};

    qsort L_(arr, 5, sizeof(int), LAMBDA(int)(const void *a, const void *b)
    (
        return *(int*)a - *(int*)b;
    ));

    for (int i = 0; i < 5; i++ )
        printf("%d ", arr[i]);
    putchar('\n');
}

int main()
{
    example1();
    example2();
}

) // ENABLE_LAMBDAS

注意 ENABLE_LAMBDAS 宏包裹了整个代码段。

本例使用两种方式定义functions/lambdas:

  • FUNC(return_type)(name)(params)(body)只是定义了一个函数。函数定义被移动到 ENABLE_LAMBDAS 的开头,因此它可以在其他函数内部使用。
  • LAMBDA(return_type)(params)(body) 定义了一个伪 lambda。它的函数定义在 ENABLE_LAMBDAS 的开头生成,具有自动选择的唯一名称。 LAMBDA... 扩展为该名称。

如果在括号内使用FUNCLAMBDA,则括号前面必须有L_宏。不幸的是,这是预处理器的限制。

生成的函数总是static


实施:

// Creates a lambda.
// Usage:
//     LAMBDA(return_type)(params)(body)
// Example:
//     ptr = LAMBDA(int)(int x, int y)(return x + y;);
#define LAMBDA LAM_LAMBDA

// Defines a function.
// Usage:
//     FUNC(return_type)(name)(params)(body)
// Example:
//     FUNC(int)(foo)(int x, int y)(return x + y;)
//     some_func(foo);
#define FUNC LAM_FUNC

// Any time a `LAMBDA` or `FUNC` appears inside of parentheses,
//   those parentheses must be preceeded by this macro.
// For example, this is wrong:
//     foo(LAMBDA(int)()(return 42;));
// While this works:
//     foo L_(LAMBDA(int)()(return 42;));
#define L_ LAM_NEST

// `LAMBDA` and `FUNC` only work inside `ENABLE_LAMBDAS(...)`.
// `ENABLE_LAMBDAS(...)` expands to `...`, preceeded by function definitions for all the lambdas.
#define ENABLE_LAMBDAS LAM_ENABLE_LAMBDAS

// Lambda names are composed of this prefix and a numeric ID.
#ifndef LAM_PREFIX
#define LAM_PREFIX LambdaFunc_
#endif

// Implementation details:

// Returns nothing.
#define LAM_NULL(...)
// Identity macro.
#define LAM_IDENTITY(...) __VA_ARGS__
// Concats two arguments.
#define LAM_CAT(x, y) LAM_CAT_(x, y)
#define LAM_CAT_(x, y) x##y
// Given `(x)y`, returns `x`.
#define LAM_PAR(...) LAM_PAR_ __VA_ARGS__ )
#define LAM_PAR_(...) __VA_ARGS__ LAM_NULL(
// Given `(x)y`, returns `y`.
#define LAM_NO_PAR(...) LAM_NULL __VA_ARGS__
// Expands `...` and concats it with `_END`.
#define LAM_END(...) LAM_END_(__VA_ARGS__)
#define LAM_END_(...) __VA_ARGS__##_END

// A generic macro to define functions and lambdas.
// Usage: `LAM_DEFINE(wrap, ret)(name)(params)(body)`.
// In the encloding code, expands to `wrap(name)`.
#define LAM_DEFINE(wrap, ...) )(l,wrap,(__VA_ARGS__),LAM_DEFINE_0
#define LAM_DEFINE_0(name) name,LAM_DEFINE_1
#define LAM_DEFINE_1(...) (__VA_ARGS__),LAM_DEFINE_2
#define LAM_DEFINE_2(...) __VA_ARGS__)(c,

// Creates a lambda.
// Usage: `LAM_LAMBDA(ret)(params)(body)`.
#define LAM_LAMBDA(...) LAM_DEFINE(LAM_IDENTITY, __VA_ARGS__)(LAM_CAT(LAM_PREFIX, __COUNTER__))
// Defines a function.
// Usage: `LAM_FUNC(ret)(name)(params)(body)`.
#define LAM_FUNC(...) LAM_DEFINE(LAM_NULL, __VA_ARGS__)

// `LAM_LAMBDA` and `LAM_FUNC` only work inside of this macro.
#define LAM_ENABLE_LAMBDAS(...) \
    LAM_END( LAM_GEN_LAMBDAS_A (c,__VA_ARGS__) ) \
    LAM_END( LAM_GEN_CODE_A (c,__VA_ARGS__) )

// Processes lambdas and functions in the following parentheses.
#define LAM_NEST(...) )(open,)(c,__VA_ARGS__)(close,)(c,

// A loop. Returns the original code, with lambdas replaced with corresponding function names.
#define LAM_GEN_CODE_A(...) LAM_GEN_CODE_BODY(__VA_ARGS__) LAM_GEN_CODE_B
#define LAM_GEN_CODE_B(...) LAM_GEN_CODE_BODY(__VA_ARGS__) LAM_GEN_CODE_A
#define LAM_GEN_CODE_A_END
#define LAM_GEN_CODE_B_END
#define LAM_GEN_CODE_BODY(type, ...) LAM_CAT(LAM_GEN_CODE_BODY_, type)(__VA_ARGS__)
#define LAM_GEN_CODE_BODY_c(...) __VA_ARGS__
#define LAM_GEN_CODE_BODY_l(wrap, ret, name, ...) wrap(name)
#define LAM_GEN_CODE_BODY_open() (
#define LAM_GEN_CODE_BODY_close() )

// A loop. Generates lambda definitions, discarding all other code.
#define LAM_GEN_LAMBDAS_A(...) LAM_GEN_LAMBDAS_BODY(__VA_ARGS__) LAM_GEN_LAMBDAS_B
#define LAM_GEN_LAMBDAS_B(...) LAM_GEN_LAMBDAS_BODY(__VA_ARGS__) LAM_GEN_LAMBDAS_A
#define LAM_GEN_LAMBDAS_A_END
#define LAM_GEN_LAMBDAS_B_END
#define LAM_GEN_LAMBDAS_BODY(type, ...) LAM_CAT(LAM_GEN_LAMBDAS_BODY_, type)(__VA_ARGS__)
#define LAM_GEN_LAMBDAS_BODY_c(...)
#define LAM_GEN_LAMBDAS_BODY_l(wrap, ret, name, par, ...) static LAM_IDENTITY ret name par { __VA_ARGS__ }
#define LAM_GEN_LAMBDAS_BODY_open()
#define LAM_GEN_LAMBDAS_BODY_close()