From 1781755339677183605
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,fb1a92fa405c930c
X-Google-Attributes: gidf78e5,public
From: James.Kanze@dresdner-bank.com
Subject: Re: nested functions
Date: 1999/06/16
Message-ID: <7k5ui3$qgm$1@nnrp1.deja.com>#1/1
X-Deja-AN: 490113685
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <7jd4c5$nup$1@nnrp1.deja.com> <7jfkcr$na6$1@engnews1.eng.sun.com>
X-Original-Date: Tue, 15 Jun 1999 16:21:37 GMT
X-Http-User-Agent: Mozilla/4.6 [en] (WinNT; I)
X-Complaints-To: news@news.unimelb.edu.au
X-Http-Proxy: 1.0 x32.deja.com:80 (Squid/1.1.22) for client 193.194.7.71
X-Trace: izvestia.its.unimelb.edu.au 929505452 14101 128.250.29.16 (16 Jun 1999 03:57:32 GMT)
Organization: Deja.com - Share what you know. Learn what you don't.
X-Article-Creation-Date: Tue Jun 15 16:21:37 1999 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAN2cgjeEDnX0m9pzZAQEcnAF/Svk3VPfhKbb2aR0lqigTbRmoXqUV1pvi UXLlPAQkq2hMCoRhjSZNuekaZ4cAK/r/ =5Vuw
NNTP-Posting-Date: 16 Jun 1999 03:57:32 GMT
Newsgroups: comp.std.c++

In article <7jfkcr$na6$1@engnews1.eng.sun.com>,
  clamage@eng.sun.com (Steve Clamage) wrote:
> gbush@my-deja.com writes:
>
> >C++ unlike other languages doesn't support nested functions. I
> >understand, bad heredity. But C++ had an opportunity to escape from
> >this predicament by allowing classes inside function body see the
local
> >non-static variables, defined in the outer scope as well as function
> >parameters. For some reason it was not done. I wonder, is there at
> >least one good reason why it is so?
>
> IMHO, no good reason.
>
> You can have a local class with member functions. In order to
> support that, the compiler has most of the mechanism needed
> to support general nested functions.

You've more experience than I with compilers.  But it would seem that
while the same mechanisms are needed at the parsing level, they are
relatively simple, and that the complexity of nested functions is the
need for passing additional context -- there is an additional run-time
complexity (in the generated code) that is not present in nested classes
as currently defined by the standard.

As for why nested functions are not supported, one of the main reasons I
have always heard was the difficulties involved in supported pointers to
the nested function.  The classical solution is for the pointer to
function to contain in fact two pointers, one to the function, and one
to the calling context.  This means either that all C/C++ pointers to
functions become heavier (you pay for what you don't use), or that
pointers to functions and pointers to nested functions are two different
types.

I believe that Fergus Henderson contests this argument, however.  On
many machines, the pointer to a nested function can be implemented as a
pointer to a trampoline, generated on the fly.  I believe that Fergus
has proven, or claims to have proven, that this solution can always be
made to work.

> I once argued that this situation makes no sense. Either nested
> functions should be supported, or local classes should not be
> allowed to have user-defined member functions. That is, the
> member functions in local classes add considerable complexity
> to the compiler for a feature that is not very useful and is
> seldom used.

Well, I've used them rather often.  In many cases, the main reason for
nesting was simply to limit namespace pollution, but not always.  And
more than once, I've transferred part of the local context to the class,
by declaring references in them which were initialized to local
variables.  (This is awkward.  You have to declare the reference as a
data member.  You have to provide a constructor to initialize it.  And
whenever you instantiate the class, you have to pass the local variable
to the constructor.)

In almost all cases, the local class derived from a non-local abstract
class.

--
James Kanze                         mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orient�e objet/
                        Beratung in objekt orientierter
Datenverarbeitung
Ziegelh�ttenweg 17a, 60598 Frankfurt, Germany  Tel. +49 (069) 63 19 86
27


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



