Article View: comp.std.c++
Article #1435Use of nested functions (Was: Proposal for default scope)
From: Greg Noel
Date: Fri, 08 Jan 1993 19:29
Date: Fri, 08 Jan 1993 19:29
43 lines
3122 bytes
3122 bytes
Fergus James HENDERSON <fjh@munta.cs.mu.OZ.AU> writes: >Many people seem to think that the purpose of nested functions is purely >as encapsulation, a sort of code organization/modularization feature. >... the real reason that they are useful is that ... their addresses can >be passed to any function that just expects a function parameter. >If a local function never has its address taken, then it does not use >the full power of nested functions. This is a valid point, but this is exactly the part that is expensive to implement. The ``Spirit of C'' is that expensive things are exposed to the programmer; C++ inherits much of this spirit. If you look at what a compiler must do to implement this, it has to create a ``structure'' (the stack frame) that contains all the local variables, the parameters, and so forth, and arrange for the nested function to be able to find that structure with a sort of implicit ``this'' pointer. In a heavily optimizing compiler that keeps many values in registers, it can be costly to have to re-sync memory every time a nested function _might_ be invoked. On top of that, if you wish a function to retain its scope during a potentially recursive invocation, it means that the implicit ``this'' pointer must be kept as a part of the pointer, which would increase the size of _every_ pointer, whether the feature is used or not. That was a real killer for PL/I function pointers and very much against the C philosophy. (BTW, I'd like to have nested functions, but I would not make pointers to them be compatible with a ``normal'' function pointer. Nested functions are handy to improve the clarity of code, but I'd want the compiler to have total knowledge over when they were invoked and allow it to optimize them as it saw fit, so that it could inline them or use special lightweight calling sequences if it wished. If a pointer syntax is needed, I'd make it similar to the C++ class pointer notation, using the function name as the class---after all, that's really what's going on....) But, as others have pointed out, it's possible for you, the programmer, to do the expensive thing that the compiler would have to do, by putting the variables you need in a structure and keeping a pointer to it where the helper function can find it. I won't claim that it's fun to do it this way, but this is essentially what the compiler would have to generate, so the overhead would be the same. -- -- Greg Noel, Unix Guru greg@qualcomm.com or greg@noel.cts.com
Technical Details
Message-ID:
<1993Jan8.192906.2342@qualcomm.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!network.ucsd.edu!qualcom.qualcomm.com!qualcom.qualcomm.com!gregReferences:
<9300817.11209@mulga.cs.mu.OZ.AU>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!network.ucsd.edu!qualcom.qualcomm.com!qualcom.qualcomm.com!greg From: greg@qualcom.qualcomm.com (Greg Noel) Subject: Use of nested functions (Was: Proposal for default scope) Message-ID: <1993Jan8.192906.2342@qualcomm.com> Sender: news@qualcomm.com Nntp-Posting-Host: qualcom.qualcomm.com Organization: Qualcomm, Inc., San Diego, CA References: <9300817.11209@mulga.cs.mu.OZ.AU> Date: Fri, 8 Jan 1993 19:29:06 GMT Lines: 43