From 1109371501637922336
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,a69618ea97f65037
X-Google-Attributes: gidf78e5,public
From: JHB NIJHOF <nijhojhb@aston.ac.uk>
Subject: Re: nested functions
Date: 1998/05/01
Message-ID: <6i9jt9$50d$1@whatsit.aston.ac.uk>#1/1
X-Deja-AN: 349208968
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
Content-Transfer-Encoding: 8bit
References: <6hac23$8lb$1@news2.isdnet.net> <353A6B2D.C2904893@concentric.net> <353F43D1.2781E494@ips.cs.tu-bs.de> <3545EA31.DE87C4E6@concentric.net> <3546C3C8.898F123F@ix.netcom.com>
Delivered-To: comp-std-c++@news.pipex.net
X-Original-Date: 30 Apr 1998 10:31:05 GMT
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Organization: Aston University
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANUmJ8eEDnX0m9pzZAQE45wF/TTle40f8ZV0M82hJEFEfngni4/TfE7Ny mCtnNo+HM6PU64o+xVMTqvv0hxw+7gwq =0jzy
Newsgroups: comp.std.c++


Paul D. DeRocco <pderocco@ix.netcom.com> wrote:

: 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.

You can do that with a class:
Class Output {
private:
char* buf;
unsigned buflen; 
public: 
  Output(char *pointer,int length)
	: buf(pointer), buflen(length) {};
  void operator()(char c) { 
 	    *buf++ = c;
 	    if (!--buflen) throw length_error();
 	    }
}

Then you can use Output as:

Output append(somebuffer, itslength);
...
append(c);

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



