[comp.std.c++] Local functions

rwallace@vax1.tcd.ie (08/06/90)

Is there any chance of introducing local functions into C++? i.e. declaring a
function within a block in another function so it can only be called from
within that block and can access that block's local variables. This would be
useful in the following situation which I've run into a few times: say you've
got a large function with a section of code, repeated several times, which
performs operations on variables local to the function. You want to save code
space and increase maintainability by factoring out this section of code into
another function. Problem: the small function can't access the big function's
local variables unless you

a) pass pointers to those local variables to the small function as parameters
   (this loses most of the benefits of factoring out the code of the small
   function)

b) make the big function's local variables local only to the module not to
   the function, so the small function can access them (with the obvious
   problem of reduced locality of data)

... what you really want to do is declare the small function as local to the
big function. I know handling the stack allocation for local functions is a
severe pain for the compiler writer. Is there any loss of run-time efficiency?
(If there is an unavoidable loss that can't be fixed by a clever optimizer,
forget I mentioned the subject).

"To summarize the summary of the summary: people are a problem"
Russell Wallace, Trinity College, Dublin
rwallace@vax1.tcd.ie

mcgrath@paris.Berkeley.EDU (Roland McGrath) (08/06/90)

GCC version 2 (not yet released, and don't ask when) supports nested functions
in C.  I imagine the support is (or will be) in C++ as well.
--
	Roland McGrath
	Free Software Foundation, Inc.
roland@ai.mit.edu, uunet!ai.mit.edu!roland