From 7887319864371535104
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,fb1a92fa405c930c
X-Google-Attributes: gidf78e5,public
From: clamage@eng.sun.com (Steve Clamage)
Subject: Re: nested functions
Date: 1999/06/12
Message-ID: <7jrv0r$dfq$1@engnews1.eng.sun.com>#1/1
X-Deja-AN: 488627241
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <7jd4c5$nup$1@nnrp1.deja.com> <7jjjr6$r02$1@nnrp1.deja.com> <7jma5c$i85@abyss.West.Sun.COM> <7jmdcv$a56$1@engnews1.eng.sun.com> <7jrqf5$op5$1@nnrp1.deja.com>
X-Original-Date: 11 Jun 1999 21:27:55 GMT
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 929151925 178 128.250.29.16 (12 Jun 1999 01:45:25 GMT)
Organization: Sun Microsystems Inc., Mountain View, CA
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAN2G7leEDnX0m9pzZAQEFAAF7BOe0HnLhW/uMpcZsKMaGaQBfSBB0MVNc JWulpDm8VJ+g+tcQ+Xs6CVG/TwZgQdeX =n1GJ
NNTP-Posting-Date: 12 Jun 1999 01:45:25 GMT
Newsgroups: comp.std.c++

gbush@my-deja.com writes:

>In article <7jmdcv$a56$1@engnews1.eng.sun.com>,
>  clamage@eng.sun.com (Steve Clamage) wrote:
>> Consider function pointers:
>>
>> extern int f(int);
>> extern int(fp*)(int);
>>
>> void g1()
>> {
>>     int h(int) { ... } // supposing C++ had local functions
>>     fp = h;

>This must be prohibited. It doesn't make sense to assign local function
>pointer to the global variable. Local functions cannot be extern.

I don't understand that reasoning.  Consider this code:

    extern int (*pf)(int);
    extern int* pi;

    static int f(int i) { ... }

    void foo()
    {
	static int k = 1;
	pf = f;
	pi = &k;
    }

Function f is not visible outide this translation unit, yet
can be called from other translation units via pf. Varible
k is not visible outside function foo, yet can be accessed
even from other translation units via pi.

>> If the calling sequences for g1::h and f are not the same,

>There should not be such thing as g1::h. Local functions should be
>visible only in the scope where they are defined.

What does visibility have to do with it?  See above.

>> the code can't work. Either you need a different kind of
>> function pointer for local functions, or you extend the
>> calling sequence of global functions to match that of local
>> functions.

>You don't need a different kind of pointer for local functions. You
>just don't call them from out of scope.

Then you eliminate the only use for local functions in C++.

C++ has enough different scope control mechanisms you don't need
local functions the way you need them in, say, Pascal. The
primary reason to have local functions in C++ would be to
implement closures. If you just want to share state among
cooperating functions, use a class. The code will be simpler
and easier to maintain.

>> Given the amount of complexity, and that you can get many of
>> the effects of local functions with function objects or with
>> classes in general, adding local functions didn't seem like
>> a big win for C++.
>There is no "amount of complexity", it works great in Pascal and is
>very usefull.

You are arguing against yourself.  Pascal allows you to pass a
local function to an outside function, yet you say that must be
prohibited in C++. If it works great, why prohibit it?

Pascal gives you true closures, and does so by using "fat"
function pointers. We can do that in C++, but it breaks C
compatibility if function pointers are uniform. That brings
us back to where this thread started.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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              ]



