有什么用于局部函数声明

有什么用于局部函数声明

本文介绍了有什么用于局部函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像我这样的大多数C ++程序员在某个时候犯了以下错误:

Most C++ programmers like me have made the following mistake at some point:

class C { /*...*/ };

int main() {
  C c();     // declares a function c taking no arguments returning a C,
             // not, as intended by most, an object c of type C initialized
             // using the default constructor.
  c.foo();   // compiler complains here.

  //...
}

错误很明显,一旦你知道它我想知道是否有任何明智的使用这种局部函数声明,除非你能做到 - 特别是因为没有办法定义这样的局部函数在同一块中;你必须在其他地方定义它。

Now while the error is pretty obvious once you know it I was wondering if there is any sensible use for this kind of local function declaration except that you can do it -- especially since there is no way to define such a local function in the same block; you have to define it elsewhere.

我认为Java风格的本地类是一个相当不错的功能,我经常使用,特别是匿名排序。即使是本地C ++类(可以有内联定义的成员函数)也有一些用处。但是这个局部函数声明没有定义东西似乎对我很尴尬。是非C的遗产还是有一些更深的用例,我不知道?

I think that Java-style local classes are a pretty nice feature which I tend to use often, especially the anonymous sort. Even local C++ classes (which can have inline-defined member functions) have some use. But this local function declaration without definition thing seems very awkward to me. Is it just a C-legacy or is there some deeper use case which I am not aware of?

编辑 C c()不是函数指针声明。

Edit for the non-believers: C c() is not a function pointer declaration.

p>

This program

int main()
{
  void g();
  cout << "Hello ";
  g();
  return 0;
}

void g()
{
  cout << "world." << endl;
}

输出 Hello world。此程式

void fun()
{
  cout << "world." << endl;
}

int main()
{
  void g();
  g = fun;
  cout << "Hello ";
  g();
  return 0;
}

gcc抱怨:

error: cannot convert 'void ()()' to 'void ()()' in assignment

$时将'void() b
$ b

comeau:

comeau:

error: expression must be a modifiable lvalue


推荐答案

我想要在C中的局部函数声明,当我想把它们作为参数传递给其他函数。我用其他语言做这一切。原因是封装了数据结构的实现。

I've wanted local function declarations in C when I wanted to pass them as arguments to some other function. I do this all the time in other languages. The reason is to encapsulate the implementation of data structures.

例如。我定义一些数据结构,例如树或图形,我不想公开其内部实现的细节,例如。因为我可能想改变或改变它。所以我公开其元素的访问器和mutator函数,以及遍历这些元素的遍历函数。遍历函数的参数是对元素进行操作的函数;遍历函数的任务是对每个元素执行参数函数,并可能以某种方式聚合结果。现在当我调用遍历函数时,参数函数通常是一些依赖于局部状态的专用操作,因此应该定义为局部函数。不得不将函数,包含本地状态的变量,外部推送到全局变量,或者推送到专门创建的内部类来保存它们。但这是一个问题,我有C和Java,而不是C ++。

E.g. I define some data structure, e.g. a tree or a graph, and I don't want to expose the details of its internal implementation, e.g. because I may want to change or vary it. So I expose accessor and mutator functions for its elements, and a traversal function to iterate over the elements. The traversal function has as its argument a function that operates on an element; the traversal function's task is to execute the argument function on each element and possibly aggregate the results in some way. Now when I call the traversal function, the argument function is usually some specialized operation that depends on local state and therefore should be defined as a local function. Having to push the function, with the variables that contain the local state, outside to be global, or into an inner class created specifically to hold them, is butt ugly. But this is a problem I have with C and Java, not with C++.

这篇关于有什么用于局部函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:06