From -3953332275080120273
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/17
Message-ID: <376815E7.CF1EE30D@technologist.com>#1/1
X-Deja-AN: 490586594
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> <37659D89.565ACF7D@technologist.com> <7k5ncv$n8u$1@nnrp1.deja.com> <376690B6.6231FB13@technologist.com> <376761DB.603FE6F1@physik.tu-muenchen.de>
X-Original-Date: Wed, 16 Jun 1999 16:23:51 -0500
X-Accept-Language: en
X-Authentication-Warning: backdraft.briar.org: smap set sender to <news@beasys.com> using -f
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 929607428 22533 128.250.29.16 (17 Jun 1999 08:17:08 GMT)
Organization: Member of the Vast Right-Wing Conspiracy
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAN2iu7OEDnX0m9pzZAQG5sQGAj1PF/zV5G63FHYuMzNKHuNttAZ9CkmNn YZRyTd106GH1qf/v+2JWc8gB4pfLHkY/ =TlIU
Mime-Version: 1.0
NNTP-Posting-Date: 17 Jun 1999 08:17:08 GMT
Newsgroups: comp.std.c++

Christopher Eltschka wrote:
> 
> David R Tribble wrote:
> 
> [...]
> 
> > >> P.S.  I personally don't see any need for nested functions.
> > >
> > > I do. (It's true no single language feature is have-it-or-die,
> > > though.)
> >
> > Please provide a convincing example; no one's done that yet.
> 
> One from real life:
> 
> extern "C"
>  void fortran_integration_routine_(double(*)(double&),
>                                    int const& start, int& end,
>                                    double const& prec,
>                                    double& result);
> 
> double calc_something(double param1, double param2)
> {
>   // I wish I had this
>   double foo(double& pd)
>   {
>     return param1 + pd*param2;
>       // of course, the real function was more complex
>   }
>   double result;
>   fortran_integration_routine_(&foo, 1, 3, 1e-10, result);
>   return result;
> }
> 
> Of course, the real function was much more complex (and the
> integral was two-dimensional, so the integration was called
> recursively).

Ignoring for the moment the problems of integration with FORTRAN
subroutines, why not use a functor that stores the 'pd' parm as part
of its state?:

    double calc_something(double param1, double param2)
    {
        struct Functor
        {
            double      p1;
            double      p2;

                        Functor(double a, double b):
                            p1(a), p2(b)
                        { }

            double      operator ()(double pd)
                        { return p1 + pd*p2; }
        };

        result = integration_routine(
                     Functor(param1, param2), 1, 3, 1e-10);
        return result;
    }

Hooking this into FORTRAN adds another dimension to the problem,
of course.  But I don't think we should resort to adding nested
functions to C++ just to solve that particular problem.

-- 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              ]



