函数返回条件函数而没有相关参数

时间:2013-11-18 18:27:07

标签: c function parameters

我想在C中定义一个函数 master 返回一个函数 slave ,其中 slave master的某些参数限制,它不是奴隶的参数......在C中是否可以构建这样的结构?

虽然以下代码不起作用(并且包含一个可怕的嵌套函数),但我想按照以下精神做一些事情:

typedef int(*First_elem)(void*);

First_elem Master(type1* arg1, type2* arg2){

  int new_first(void* data){
     if (test1(arg1)){
       /* ... some code ... */
     }
     return some_integer_built_from_data_arg1_and_arg2;
  }

  return new_first;
}

我可以理解我应该完全重新考虑我的算法方法,但有人知道这些事情是否可能,否则,是什么让C不能处理这样的构造?


对于想要查看完整背景的人来说,这是:

typedef int(*First_elem)(void*);
typedef int(*Next_elem)(void*, void*);
typedef void(*Print_elem)(void*);
typedef int(*Filter)(void*);

typedef struct{
  void* current;
  void* previous;
  size_t size;
  int index;
  First_elem first;
  Next_elem next;
  Print_elem print;
}Iterator;

int initialize_iterator(Iterator* it, size_t size, First_elem first, Next_elem next, Print_elem print){
  it->size = size;
  if ((it->current=malloc(size)) == NULL){
    perror("Memory allocation error\n");
    return 0;
  }
  if ((it->previous=malloc(size)) == NULL){
    perror("Memory allocation error\n");
    return 0;
  }
  it->index=0;
  it->first=first;
  it->next=next;
  it->print=print;
  return 1;
}

void print_current_element(Iterator* it){
  it->print(it->current);
}

int iterator_next(Iterator* it){
  int ans;
  if (it->index == 0){
    if ((ans=it->first(it->current)))
      it->index++;
    return ans;
  }
  /* copy of current into previous */
  int i;
  for (i=0 ; i<it->size ; i++)
    ((char *)it->previous)[i] = ((char *)it->current)[i];
  if ((ans=it->next(it->previous, it->current)))
    it->index++;
  return ans;
}

void iterator_reroll(Iterator* it){
  it->index=0;
}

int iterator_cardinality(Iterator* it){
  int i, ans, last=it->index;
  iterator_reroll(it);
  while (iterator_next(it));
  ans = it->index;
  if (last != ans){
    iterator_reroll(it);
    for (i=0 ; i<last ; i++)
      iterator_next(it);
  }
  return ans;
}

First_elem first_cartesian_two(Iterator* it1, Iterator* it2){
  int new_first(void* first){
    int i;

    iterator_reroll(it1);
    iterator_reroll(it2);
    if ((iterator_next(it1)) && (iterator_next(it2))){
      /* Copy the concat of the two firsts into first */
      for (i=0 ; i<it1->size ; i++)
    ((char *)first)[i] = ((char *)it1->current)[i];
      for (i=0 ; i<it2->size ; i++)
    ((char *)first)[i+it1->size] = ((char *)it2->current)[i];
      return 1;
    }
    return 0;
  }
  return &new_first;
}

Next_elem next_cartesian_two(Iterator* it1, Iterator* it2){
  int new_next(void* previous, void* next){
    int i;
    /* copy of previous in previous of both iterator */
    for (i=0 ; i<it1->size ; i++)
      ((char *)it1->previous)[i] = ((char *)previous)[i];
    for (i=0 ; i<it2->size ; i++)
      ((char *)it2->previous)[i] = ((char *)previous)[i+it1->size];

    /* if we can iterate over the second iterator */
    if (iterator_next(it2)){
      for (i=0 ; i<it1->size ; i++)
    ((char *)next)[i] = ((char *)it1->current)[i];
      for (i=0 ; i<it2->size ; i++)
    ((char *)next)[i+it1->size] = ((char *)it2->current)[i];
      return 1;
    }
    /* We reroll the second, and we try to iterate over the first one */
    iterator_reroll(it2);
    if ((iterator_next(it2)) && (iterator_next(it1))){
      for (i=0 ; i<it1->size ; i++)
    ((char *)next)[i] = ((char *)it1->current)[i];
      for (i=0 ; i<it2->size ; i++)
    ((char *)next)[i+it1->size] = ((char *)it2->current)[i];    
      return 1;
    }
    /* The cartesian product is now over... */
    return 0;
  }
  return &new_next;
}

Print_elem print_cartesian_two(Iterator* it1, Iterator* it2){
  void new_print(void* elem){
    int i;

    /* Split elem and copy into the atoms */
    for (i=0 ; i<it1->size ; i++)
      ((char *)it1->current)[i] = ((char *)elem)[i];
    for (i=0 ; i<it2->size ; i++)
      ((char *)it2->current)[i] = ((char *)elem)[i+it1->size];
    print_current_element(it1);
    printf(" ");
    print_current_element(it2);    
  }
  return &new_print;
}

int iterator_cartesian_product_two(Iterator* result, Iterator* it1, Iterator* it2){
  size_t size=it1->size+it2->size;
  Next_elem next=next_cartesian_two(it1, it2);
  First_elem first=first_cartesian_two(it1, it2);
  Print_elem print=print_cartesian_two(it1, it2);
  /* void* test=malloc(sizeof(char)+sizeof(int)); */
  /* int a=12; */
  /* char c='a'; */
  /* int i; */
  /* /\* Copy the concat of the two firsts into first *\/ */
  /* for (i=0 ; i<it1->size ; i++) */
  /*   ((char *)test)[i] = ((char *)(&a))[i]; */
  /* for (i=0 ; i<it2->size ; i++) */
  /*   ((char *)test)[i+it1->size] = ((char *)(&c))[i]; */
  /* printf("foo"); printf("\n"); */
  /* print(test); printf("\n"); */
  iterator_reroll(it1);
  iterator_reroll(it2);  
  return initialize_iterator(result, size, first, next, print);
}

对不起我的英文,并提前感谢您的建议/意见......

1 个答案:

答案 0 :(得分:2)

没有;这是不可能的。 C不允许嵌套函数,并且在调用函数时没有隐藏的上下文(如C ++中的this)。

您可以通过将通用迭代器混合到私有结构中来解决您的问题。 E.g。

/* public header */
struct Iterator {
    ...
};

/* private implementation */
struct foo_iterator {
    struct Iterator it;
    size_t size;
    ...
}

struct Iterator* iterator_create(size_t size, ...)
{
    struct foo_iterator *it = calloc(1, sizeof *it);

    it->size = size;
    return &it->it;
}

Iterator* iterator_next(struct Iterator *it)
{
    struct foo_iterator *it = container_of(it, struct Iterator, it);

    ...
}

有关container_of()的实施,请参阅例如: http://www.kroah.com/log/linux/container_of.html