lambda defs in C (and C++ inlines)

Bjarne Stroustrup bs at alice.UucP
Sun Feb 9 07:20:57 AEST 1986


> From allegra!mit-eddie!think!harvard!seismo!hao!nbires!isis!dmiruke Wed Dec 31 19:00:00 1969
> From: dmiruke at isis.UUCP (Dataram Miruke)
> Subject: Re: lambda defs in C
> Organization: University of Denver Math and Computer Science
>
>> I often find myself creating highly localized register variables in an
>> attempt to synthesize common sub expression optimization:
>> 
>> 	{
>> 		register  char _q = *((x.y)->z);
>> 
>> 		if (_q == 'a' || _q == 'b' || _q == 'c') { ...
>> 
>> I also want to return values from arbitrary expressions like a for loop.
>> Lisp has something called a lambda definition which is something like
>> a local temporary function definition. Sort of like:
>> 
>> 	if ( lambda int f(register char _q = *((x.y)->z)) {
>> 			return(_q == 'a' || _q == 'b' || _q == 'c');
>> 		}	/* end of lambda def */ ) { ...
>> 
>> or
>> 
>> 	x = lambda int f() {
>> 		for (...) {
>> 			...
>> 			return(some expression);
>> 			...
>> 		}	/* for */
>> 	}	/* lambda */
>> 
>> So, academically, if we were to add a simlar feature to a new dialect of C,
>> or to construct a pre-preprocessor for ansii C (a la C++), what problems
>> do you see?
>> 
>> K. Richard Magill
>	Don't the inline functions in C++ provide a similar feature?
>
							- Datta Miruke
Yes, the following is legal C++

	inline int f()
	{
 		for (...) {
 			...
 			return(some expression);
			...
 		}

	}

However, the current implementation cannot handle loops or switches
in an inline, so you get a ``sorry, not implemented'' message. Sorry,
we will have to wait a bit longer. The non-looping example can be
handled quite nicely using an inline. Inline functions differ from lambdas
in that they must be defined (outside any function) before they can be used.

	- Bjarne Stroustrup (AT&T Bell Labs, Murray Hill).

PS Calling the current C++ implementation a pre-processor for C is as accurate
	as calling C a pre-processor for the assembler. The C++ front-end does
	a complete typecheck and performs non-trivial code transformations (like
	inline expansion). It relies on the underlying C compiler for the part of
	the code generation process (only). ANY message from the C compiler is
	considered a C++ compiler bug.



More information about the Comp.lang.c mailing list