Topic: GCC's extensions to C89 (nested funcs)
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/08/13 Raw View
>From a posting on comp.std.c, concerning nested functions in C...
Clive D.W. Feather <clive@demon.net> wrote:
> [BCPL] does *NOT* have nested functions in any useful sense.
>
> > "Richard A. O'Keefe" <ok@goanna.cs.rmit.edu.au> writes:
> > This doesn't buy you anything you can't get by moving the static variables
> > and the inner function _out_ of the procedure.
>
> Yes it does - namespace control.
>
> > That is to say, it does not have any functions that cannot
> > be moved out using trivial local rewrites and renaming.
>
> But said trivial renaming loses you namespace control, which is *a*
> reason for wanting nested functions.
I believe that nested funcs are not useful for C:
1) It would require too much baggage (in the stack frame).
2) After 20 years, people have pretty well adjusted to a nested-less
style of coding funcs.
3) Like Clive said, it's too late for C.
On the other hand, I can give a reason why nested funcs might be useful
for C++. It would make it easier to write static (private, hidden) funcs
that have access to the class members but without the need for them being
full-blown class member functions.
For example:
// foo.cpp
int Foo::perform(int a, int b) // Class member func
{
int local;
int do_it(int x) // Nested func
{
// Has access to 'a', 'b', and 'local'
// Has access to all members of Foo and '*this'
...
}
// Body of perform()
...
}
// foo.hpp
class Foo
{
public:
int perform(int a, int b);
};
No declaration of do_it() is needed in "foo.hpp", since it's fully
declared and defined within the scope of perform(). So even though do_it()
is not declared in the class header, it still acts like a private member
function. This keeps the client interface for class Foo less cluttered
(since there is less need for private funcs in the header file, which can't
be called by clients of Foo and don't need to be seen by them anyway).
But while this might be a valid reason for nested functions, I'm not really
sure it's a good idea because of the baggage required (in the stack frame
at least). Besides, C++ is already too feature-bloated.
C++, the PL/1 of the 90's.
--------------------. BEA Systems, Inc. ,-. +1-972-738-6125 Office
David R. Tribble \ ,------------------' \ +1-972-738-6111 Fax
http://www.beasys.com `-' Dallas, TX 75248 `-----------------------
david.tribble@noSPAM.beasys.com http://www.flash.net/~dtribble
-------------------------------------------------------------------------
Send junk to: postmaster@agis.net mrchig@easynet.co.uk emailer@qlink2info.com
simrem@answerme.com office@canma.dyn.ml.org markdan3@earthlink.net
clover@earthfriends.com abuse@cyberpromo.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]