Topic: functions within functions
Author: garry@ithaca.uucp (Garry Wiegand)
Date: 22 Jan 91 08:10:57 GMT Raw View
When the C standards committee was meeting, did they discuss allowing
the declaration of functions within functions? Ie, making:
void outside(void) {
static void inside(void) {}
inside();
}
count as legal C? Neither my quasi-Ansi C nor my g++ compiler will
accept it.
The name-space advantages should be obvious, and as far as I can
see the syntax and semantics are clear and unambiguous, with no
unfortunate side effects. (Except: I'm not sure if the word "static"
would have any meaning in the context.)
So I'm guessing that if they thought about it they must have decided
not to break old compilers that were strongly "moded" inside. Or
that there was some theological argument. Does anyone know?
Put it on the list for next time.
(The prohibition in C++ seems silly considering that a simple wrapper:
void outside(void) {
class wrapper {public:
static void inside(void){}
};
wrapper::inside();
}
turns it into a legal program.)
Garry Wiegand --- Ithaca Software, Alameda, California
...!uunet!ithaca!garry, garry%ithaca.uucp@uunet.uu.net
Author: dave@cs.arizona.edu (Dave P. Schaumann)
Date: 22 Jan 91 14:02:21 GMT Raw View
In article <1991Jan22.081057.8567@ithaca.uucp> garry@ithaca.uucp (Garry Wiegand) writes:
|When the C standards committee was meeting, did they discuss allowing
|the declaration of functions within functions? Ie, making:
|
| void outside(void) {
| static void inside(void) {}
| inside();
| }
|
|count as legal C? Neither my quasi-Ansi C nor my g++ compiler will
|accept it.
C has never allowed nested function definitions. I don't know for sure why,
but I would guess (given that C is intended for generating clean, fast code)
that they didn't want to get involved with closures.
|Garry Wiegand --- Ithaca Software, Alameda, California
|...!uunet!ithaca!garry, garry%ithaca.uucp@uunet.uu.net
Dave Schaumann | And then -- what then? Then, future...
dave@cs.arizona.edu | -Weather Report
Author: jimad@microsoft.UUCP (Jim ADCOCK)
Date: 29 Jan 91 01:22:16 GMT Raw View
In article <1991Jan22.081057.8567@ithaca.uucp> garry@ithaca.uucp (Garry Wiegand) writes:
|When the C standards committee was meeting, did they discuss allowing
|the declaration of functions within functions? Ie, making:
|
| void outside(void) {
| static void inside(void) {}
| inside();
| }
|
|count as legal C? Neither my quasi-Ansi C nor my g++ compiler will
|accept it.
|
|The name-space advantages should be obvious, and as far as I can
|see the syntax and semantics are clear and unambiguous, with no
|unfortunate side effects. (Except: I'm not sure if the word "static"
|would have any meaning in the context.)
|
|So I'm guessing that if they thought about it they must have decided
|not to break old compilers that were strongly "moded" inside. Or
|that there was some theological argument. Does anyone know?
|
|Put it on the list for next time.
|
|(The prohibition in C++ seems silly considering that a simple wrapper:
|
| void outside(void) {
| class wrapper {public:
| static void inside(void){}
| };
| wrapper::inside();
| }
|
|turns it into a legal program.)
I don't think it would be too hard to remove the requirement for
the wrapper class -- but, that still wouldn't give people the nested
function capabilities they'd expect from playing with other languages.
In C++, the nested class [and therefore the nested function] only is
allowed access to the type names, static variables, extern variables and
functions, and enumerations from the enclosing scope.
Even if you didn't have to write the wrapper:: prefix on inside(),
inside() still wouldn't be able to access the auto variables of outside().
So you still couldn't do the Pascal hack of declaring inner functions
with implied parameters, for example.
Seems to me the end result would come out half-Pas'cal'ed.
Author: rjohnson@shell.com (Roy Johnson)
Date: 31 Jan 91 17:27:39 GMT Raw View
In article <70282@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>In article <1991Jan22.081057.8567@ithaca.uucp> garry@ithaca.uucp (Garry Wiegand) writes:
>|When the C standards committee was meeting, did they discuss allowing
>|the declaration of functions within functions? Ie, making:
>|
>|> void outside(void) {
>|> static void inside(void) {}
>|>> inside();
>|> }
>|
>|count as legal C? Neither my quasi-Ansi C nor my g++ compiler will
>|accept it.
>|
>|The name-space advantages should be obvious, and as far as I can
>|see the syntax and semantics are clear and unambiguous, with no
>|unfortunate side effects. (Except: I'm not sure if the word "static"
>|would have any meaning in the context.)
If you're worried about the namespace, just make different code
modules, where the outside() is available to the other modules,
and inside() is only available within outside()'s module.
--
======= !{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!rjohnson =======
"If he exploded, all of Manhattan would be talking in high, squeaky voices
for months!" "Cool." -- When I Was Short
Roy Johnson, Shell Development Company