From -396235300921460803
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,a69618ea97f65037
X-Google-Attributes: gidf78e5,public
From: Mathew Hendry <scampi@dev.null>
Subject: Re: nested functions
Date: 1998/04/19
Message-ID: <19980419.36D398.DD58@ag169.du.pipex.com>#1/1
X-Deja-AN: 346369707
Content-Transfer-Encoding: quoted-printable
Approved: stephen.clamage@sun.com (comp.std.c++)
Mime-Version: 1.0
X-MIME-Autoconverted: from 8bit to quoted-printable by earth.sun.com id IAA22072
Originator: clamage@taumet
Content-Type: text/plain; charset=US-ASCII
Organization: /dev/null
Newsgroups: comp.std.c++


Lo=EFc Tr=E9gan <loic.tregan@isdnet.net> wrote:
: Are nested functions a good thing ?

I think they would have a rather limited place in C++.

A very simple way of emulating them is to provide a unique namespace
for your function; this is roughly what would happen under the surface
if they were provided explicitly.

Or perhaps you are talking about local functions, rather than nested
functions. That is, where an inner function has access to the outer
function's variables and functions. (Forgive me if I'm using the wrong
terminology here.)

Local functions are rather more tricky to implement, but the functor
style can be used, with the variables to be used in the inner function
provided as members of the functor class; if the functor class is
nested, you can provide multiple levels of function nesting, too. A
functor can be wrapped up to appear as a normal function, so this sort
of implementation can made entirely invisible to users:

int func( int i ) {
  class func_functor {
  public:
    func_functor()          { /* ... */ }
    int operator()( int i ) { /* ... */ }
  private:
    // ...
  };
  return func_functor()( i );
};

In fact, this is a cleaner way to implement nested functions, too, as
the encapsulation is more clearly visible, configurable, and
enforcible. (If you use the simpler namespace method, there's nothing
to prevent another function from using the same namespace, and not all
that much to indicate that it shouldn't.)

: (of course, i have my opinion...)

Well, what is it? :)

: if YES, why they are nt in the draft ?

Many people feel that the language is complicated enough, if not too
complicated, already. There are already reasonably simple ways to
achieve this sort of thing, and in a much more flexible manner.

: if NO, why are they allowed in C and not C++ ?

They're not allowed in C either. Perhaps you're thinking of Pascal.

--
       Mathew Hendry (actually at dial.pipex.com, not dev.null)
                                                                    --




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




