From 8034516631729664096
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,a69618ea97f65037
X-Google-Attributes: gidf78e5,public
From: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Subject: Re: nested functions
Date: 1998/04/29
Message-ID: <3546C3C8.898F123F@ix.netcom.com>#1/1
X-Deja-AN: 348660901
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
Content-Transfer-Encoding: 7bit
References: <6hac23$8lb$1@news2.isdnet.net> <353A6B2D.C2904893@concentric.net> <353F43D1.2781E494@ips.cs.tu-bs.de> <3545EA31.DE87C4E6@concentric.net>
X-Original-Date: Wed, 29 Apr 1998 02:08:08 -0400
Content-Type: text/plain; charset=us-ascii
Organization: Vast Right-Wing Conspiracy
X-NETCOM-Date: Tue Apr 28 11:09:50 PM PDT 1998
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANUbN/uEDnX0m9pzZAQEiaQF/R0MA/ZcAgyhLPr7IONa3FeAwSI72jRv8 O0XjUJGKyNnyfwlRGs6EjopOy6tQtNoT =SsVe
Mime-Version: 1.0
Newsgroups: comp.std.c++


Bradd W. Szonye wrote:
> 
> Okay, no argument there. I thought the original poster was talking
> about
> the 'toy' local functions in, say, Pascal, not the higher-order
> functions available in functional programming languages, which I'll
> agree are very powerful.

What I bemoan the lack of is precisely what you call "toy" local
functions. I know I can do fancier stuff with functors that carry data
along with them. However, every now and then, I have to write some big
function, perhaps containing a long switch statement, and I find there's
the same little tidbit of code in it over and over and over again, that
I'd really like to wrap in a function, except that it needs access to
two or three of the local variables of the outer function. For instance,
suppose I'm writing a disassembler that fetches bytes from memory, and
appends characters to a character buffer:

	int disassemble(
		const unsigned char* bin, // instruction
		char* buf,                // result buffer
		unsigned buflen);         // buffer length

Such code generally involves big switch statements, but I need to
produce output by appending to the buffer and checking against the
buffer length. This is done in dozens of places, so I'd like to do it
with a function:

	void append(char c, char*& buf, unsigned& buflen) {
	    *buf++ = c;
	    if (!--buflen) throw length_error();
	    }

If I could write it as a nested function, I could leave off the buf and
buflen parameters, which makes the resulting code more efficient in
terms of space, speed, and clarity. Since I can't do that, I end up
storing buf and buflen into globals, which is a kludge.

-- 

Ciao,
Paul
---
[ 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              ]



