Phi*_*ipp 5 c c99 nested-function
我创建了一个ac程序,它使用了gnu扩展中的嵌套函数,现在我想让它们符合ansi c标准.
什么是转换嵌套函数的最佳方法,嵌套函数访问一些外部变量到不同的东西.
#define lambda(return_type, function_body) \
({ \
return_type __fn__ function_body \
__fn__; \
})
Run Code Online (Sandbox Code Playgroud)
示例用法
size_t var1;
size_t var2;
lambda(void, (...) {
// some code
lambda(void, (...) {
// ...
// do something with var1/var2
// ..
}
// ...
// do something with var1/var2
}
Run Code Online (Sandbox Code Playgroud)
我考虑过将变量移到全局范围,所以从每个"lambda"中都可以知道它们可能是最简单的解决方案,但我不想要考虑全局范围,我不确定,如果这是最干净的方式.
正如一些评论者所说 - 这是一个具体的例子
/* fill itt*/
int n_method = 0;
void *add_method = lambda(void, (ir_entity *method) {
int itable_offset = n_method++;
const char *method_name = get_entity_name(method);
ir_entity *implementation = get_method_entity(klass, method_name);
if (implementation == NULL) {
walk_up_callback(klass, lambda(bool, (ir_type *st) {
implementation = get_method_entity(st, method_name);
if (implementation != NULL) {
insert_itable_method(implementation, itable_offset, interface, init);
}
return implementation == NULL;
}), NULL);
} else {
insert_itable_method(implementation, itable_offset, interface, init);
}
});
walk_up_callback(interface, NULL, lambda(void, (ir_type *klass) {
if (oo_get_class_is_interface(klass)) {
walk_table_methods_callback(add_method, klass);
}
}));
walk_table_methods_callback(add_method, interface);
Run Code Online (Sandbox Code Playgroud)
它是编译器的一部分,它为有效的接口查找创建了一些itables
您可以使用回调来迭代容器。如果您的数据结构允许,您可以尝试通过迭代器编写遍历代码,这将允许您将现在的单独回调编写为循环体。
例如,如果您有一个二叉树,则带有回调的递归遍历看起来或多或少像这样:
typedef struct node_t node_t;
struct node_t {
const char *id;
node_t *left, *right;
};
void traverse(const node_t *node, void (*func)(const node_t *n))
{
if (node) {
traverse(node->left, func);
func(node);
traverse(node->right, func);
}
}
Run Code Online (Sandbox Code Playgroud)
它的使用方式如下:
traverse(head, lambda(void, (const node_t *n){ puts(n->id); }));
Run Code Online (Sandbox Code Playgroud)
正如您所指出的,在标准 C 中,该函数必须是全局函数,但有以下限制:您无法轻松且类型安全地访问未存储在节点本身中的数据。
为了以符合标准且更直观的方式遍历树,您可以将遍历重写为迭代代码并将状态存储在迭代器中:
typedef struct node_iter_t node_iter_t;
struct node_iter_t {
node_t *next;
node_t *node;
node_t *stack[32];
int nstack;
};
int next_node(node_iter_t *it)
{
it->node = it->next;
while (it->nstack || it->node) {
while (it->node) {
it->stack[it->nstack++] = it->node;
it->node = it->node->left;
}
it->node = it->stack[--it->nstack];
it->next = it->node->right;
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
迭代器代码比递归遍历更冗长,但客户端代码是一个简单的循环,可以访问函数中的其他局部变量:
node_iter_t it = {head};
int i = 0;
while (next_node(&it)) {
printf("%d: %s\n", i++, it.node->id);
}
Run Code Online (Sandbox Code Playgroud)
当然,您的容器可能不适合这样的重写。
| 归档时间: |
|
| 查看次数: |
158 次 |
| 最近记录: |