From 1166075687540109757
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,fb1a92fa405c930c
X-Google-Attributes: gidf78e5,public
From: David R Tribble <dtribble@technologist.com>
Subject: Re: nested functions
Date: 1999/06/15
Message-ID: <37659D89.565ACF7D@technologist.com>#1/1
X-Deja-AN: 489736434
Content-Transfer-Encoding: 7bit
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> <FD38vK.CC4@research.att.com> <7jrpss$ogk$1@nnrp1.deja.com> <FqA83.38464$wk2.553149@newscene.newscene.com>
X-Original-Date: Mon, 14 Jun 1999 19:25:45 -0500
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 929428041 27835 128.250.29.16 (15 Jun 1999 06:27:21 GMT)
Organization: Member of the Vast Right-Wing Conspiracy
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAN2XyKeEDnX0m9pzZAQFm+AF/Y2I6Hmaovc/3utIiXiNqBOmVUahQ3BWV 3f5KN8Cn88MYxEpJR064j1rmkD5cj4+G =q4Hd
Mime-Version: 1.0
NNTP-Posting-Date: 15 Jun 1999 06:27:21 GMT
Newsgroups: comp.std.c++

John Potter wrote:
> 
> gbush@my-deja.com wrote:
>: I think there should be no differences in pointers to the local
>: function or to the global function. They should have exactly the
>: same signatures, so that any function that recieves function pointer 
>: could call equally successfully local and global function through 
>: that pointer. Since local functions are visible only in the scope 
>: where they are defined such a call could be made only from the same 
>: scope.
> 
> I don't understand your use of scope.  Consider:
[reformatted for clarity]:
> 
> void f1 (void (*g)())
> {
>     void f2 ()
>     {
>         g();
>     }
>
>     f2();
> }
>
> void f3 ()
> {
>     void f4 ()
>     {
>         printf("Hi\n");
>     }
>
>     f1(f4);
> }
> 
> This must work.  You may test it with egcs which supports nested
> functions for C.

I disagree; I don't think it makes much sense to call or pass a
pointer to a local function to functions outside its scope (i.e,
outside its enclosing function).  It's analogous to passing pointers
to local structures to other functions outside the function containing
the structure declaration.

On the other hand, it seems entirely reasonable to pass pointers
to local functions to other functions within the same scope of the
outer function.  Consider this example, which "must" work:

    void outer(int n)
    {
        long    count;

        long fact(long i)
        {
            if (i > 0  and  i < 15)
                return (fact(i-1) * i);
            else
                std::printf("count=%ld\n", count);
                                    // Uses outer::count
            return 1;
        }

        long call(long n, long (*fp)(long i))
        {
            return (*fp)(n);        // Call via func ptr
        }

        // begin outer()
        for (count = 0;  count < 20;  count++)
            std::printf("%ld: %ld\n", count, call(count*2, &fact));
                                    // Uses &func
    }

I'm not passing &fact to anything outside the scope of outer(),
but I am passing &fact to functions within the same scope as
fact().  This seems like a reasonable thing to do with nested
functions, and a reasonable restriction to levy on pointers to
nested functions.

If using the existing syntax for function pointers doesn't
quite solve the compiler writer's problem (since pointers to
nested functions might require different invocation code even
with the restriction I mentioned), then perhaps we could use
something like:

    long two(int n, long ({}::* fp)(long i))
                          ^^^^^
                          Means pointer to nested function

> BTW are functions nested within member functions member functions?

Good question.  If so, should they be declared somewhere in the class
declaration?

P.S.  I personally don't see any need for nested functions.  Still,
they make for an interesting discussion in language design.

-- David R. Tribble, dtribble@technologist.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              ]



