 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Prateek R Karandikar Guest
|
Posted: Wed May 19, 2004 7:47 pm Post subject: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
The C++ Programming Language, Third Edition (C.10.2): "Constructs
ought to nest unless there is a strong reason for them not to."
What is the strong reason for functions not nesting? If someone in
effect wants to create nested functions, it can be done by defining a
local class with a function defined inside the class definition.
Instead of forcing programmers to use this roundabout technique if
nested functions are ever required, why aren't nested functions
supported directly?
It is commonly said that variables should be introduced into the
tightest scope possible. Why not extend the same to functions too?
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu May 20, 2004 3:28 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
Prateek R Karandikar wrote:
| Quote: | What is the strong reason for functions not nesting? If someone in
effect wants to create nested functions, it can be done by defining a
local class with a function defined inside the class definition.
|
Traditionally, languages which permit nested functions also permit those
functions to access the local variables of the enclosing function, while
local classes do not. So your techniques are inequivalent.
Supporting nested functions with local variable access is complicated
because of pointers to nested functions, which must somehow refer to
the variables of the correct scope(s) when invoked. Various techniques
can be employed for this, but C never had such things, and so neither
did C++.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri May 21, 2004 4:58 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
Prateek R Karandikar wrote:
| Quote: | The C++ Programming Language, Third Edition (C.10.2): "Constructs
ought to nest unless there is a strong reason for them not to."
What is the strong reason for functions not nesting? If someone in
effect wants to create nested functions, it can be done by defining a
local class with a function defined inside the class definition.
Instead of forcing programmers to use this roundabout technique if
nested functions are ever required, why aren't nested functions
supported directly?
snip |
In most languages that support nested functions, a nested function has
access to the local variables of the enclosing function invokation
that made reference to it (and so on up through any further enclosing
functions). Supposing that C++ supported this, one could write
something like:
int f(int (* pf)()) { return pf() - 1; }
int main()
{
int i = 5;
int nf() { return i++; }
assert(f(nf) == 4 && i == 6);
}
and the assertion would be true.
This requires that a pointer to a nested function holds not only the
address of the code of the nested function but also the automatic
storage for the enclosing function invokation(s). Extending function
pointers in this way would require implementors to break binary
compatibility with most C ABIs, which for most users of C++ would not
be an acceptable price.
There is a way to avoid this, which is for the implementation to
generate wrapper functions dynamically. This is how GCC supports
nested functions without breaking binary compatibility. However, such
dynamic code generation is not possible in all environments in which
C++ is and should be supported.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Fri May 21, 2004 5:14 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
Hyman Rosen wrote:
| Quote: | Prateek R Karandikar wrote:
What is the strong reason for functions not nesting? If someone in
effect wants to create nested functions, it can be done by defining a
local class with a function defined inside the class definition.
Traditionally, languages which permit nested functions also permit those
functions to access the local variables of the enclosing function, while
local classes do not. So your techniques are inequivalent.
Supporting nested functions with local variable access is complicated
because of pointers to nested functions, which must somehow refer to
the variables of the correct scope(s) when invoked. Various techniques
can be employed for this, but C never had such things, and so neither
did C++.
|
It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities apart from being able to define functions in
a smaller scope. We make most things as local as possible,
but non-member functions right now must be defined at
namespace scope -- something of an anomaly.
Callback functions are a prime example of functions that
would sometimes be tidier tucked away inside the function
that registers them.
Personally I've grown to accept the C/C++ limitation that
functions definitions cannot be nested, but the most common
reason why the idea is dismissed seems suspect to me.
-- James
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Fri May 21, 2004 6:26 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
[email]do-not-spam-benh (AT) bwsint (DOT) com[/email] (Ben Hutchings) writes:
[snip]
| Quote: | There is a way to avoid this, which is for the implementation to
generate wrapper functions dynamically. This is how GCC supports
nested functions without breaking binary compatibility. However, such
dynamic code generation is not possible in all environments in which
C++ is and should be supported.
[snip] |
Note that g++ does not support nested functions for C++; they are a
C-only extention.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri May 21, 2004 4:35 pm Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
James Dennett wrote:
| Quote: | [...]
It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities [...]
|
These are not new capabilities. Functions defined in a namespace scope
have access to namespace variables. Function defined in a class scope
have access to class variables (non-static functions to instance and
static data members, and statin functions to static data members). So why
should functions defined at a function (or deeper) scope be any different?
Victor
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
John G Harris Guest
|
Posted: Fri May 21, 2004 8:26 pm Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
In message <kcgrc.598$_o.238@fed1read05>, James Dennett
<jdennett (AT) acm (DOT) org> writes
<snip>
| Quote: | It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities apart from being able to define functions in
a smaller scope. We make most things as local as possible,
but non-member functions right now must be defined at
namespace scope -- something of an anomaly.
Callback functions are a prime example of functions that
would sometimes be tidier tucked away inside the function
that registers them.
snip |
1 Would you accept that you can't have pointers to inner functions?
2 No? Then would you accept that inner functions can't access outer
variables?
3 No? Then would you accept that inner functions can access only static
variables?
4 No? Then would you accept the job of writing the full and precise
definition of when destructors are run and when they aren't?
I think you were suggesting 2, but others go further.
John
--
John Harris
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Adam H. Peterson Guest
|
Posted: Sat May 22, 2004 7:39 pm Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
| Quote: | These are not new capabilities. Functions defined in a namespace scope
have access to namespace variables. Function defined in a class scope
have access to class variables (non-static functions to instance and
static data members, and statin functions to static data members). So
why should functions defined at a function (or deeper) scope be any
different?
|
On the other hand, classes declared at function scope don't have access
to function variables. Consider:
void f() {
int i=0;
struct C {
static void g() {
i=1; // Error
}
};
C::g();
}
int main() {
f();
}
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Dave Whipp Guest
|
Posted: Sun May 23, 2004 12:15 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (Victor Bazarov) wrote:
| Quote: | These are not new capabilities. Functions defined in a namespace scope
have access to namespace variables. Function defined in a class scope
have access to class variables (non-static functions to instance and
static data members, and static functions to static data members). So why
should functions defined at a function (or deeper) scope be any different?
|
As you point out, static functions in class can't access (non-static)
member variables, so there is precident for providing only partial
visibility to variables in the defining scope.
Another perspective is that providing the ability to access local
variables does not need to be as complex as people like to make it.
C++ doesn't prevent you from storing away a pointer to a local
variable (and then returning); so why should protect me against
stashing away a pointer to a function that accesses local variables?
Also, a pointer to a non-static local function is unlikely to be a
regular function pointer, because it would need something analagous to
a "this" pointer as an implicit argument (it couldn't actually be
"this" because "this" is, itself, a local variable).
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sun May 23, 2004 2:05 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
[email]jdennett (AT) acm (DOT) org[/email] (James Dennett) writes:
[...]
| Quote: | It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
|
yes, I can see how one can construct arguments against that answer,
but I see nothing particularly wrong with it.
| Quote: | It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities apart from being able to define functions in
a smaller scope.
|
Then, they can just use local classes.
I make the conjecture that, once you allow people to lexically nest
functions inside functions, sooner or latter, they will want to access
automatic variables from surrounding environments -- with the argument
that it is virtually useless to nest functions if you can't access
variables in surrouding neighbors.
| Quote: | We make most things as local as possible,
but non-member functions right now must be defined at
namespace scope -- something of an anomaly.
|
No more of anomaly than not being able to nest namespaces at class or
local scope.
| Quote: | Callback functions are a prime example of functions that
would sometimes be tidier tucked away inside the function
that registers them.
|
I would put them either inside local classes or just in front of the
registering function and have them with internal linkage.
| Quote: | Personally I've grown to accept the C/C++ limitation that
functions definitions cannot be nested, but the most common
reason why the idea is dismissed seems suspect to me.
|
If it were matematics, I would be seeking for the most beautiful,
effective demonstration... :-)
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Sun May 23, 2004 2:06 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
Dave Whipp wrote:
| Quote: | C++ doesn't prevent you from storing away a pointer to a local
variable (and then returning); so why should protect me against
stashing away a pointer to a function that accesses local variables?
|
The problem isn't in protecting you, it's in making sure that such
a saved pointer to function acesses the right local variables.
Consider the following program. Each saved pointer to f accesses a
different j.
void (*af[10])();
void outer(int i)
{
int j = 0;
void f() { ++j; }
af[i] = &f;
if (i < 9)
outer(i + 1);
for (int k = 0; k <= i; ++k)
af[k]();
cout << j << "n";
}
int main() { outer(0); }
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Sun May 23, 2004 6:42 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
Victor Bazarov wrote:
| Quote: | James Dennett wrote:
[...]
It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities [...]
These are not new capabilities.
|
I claim that they are. They don't exist in the language today.
| Quote: | Functions defined in a namespace scope
have access to namespace variables.
|
Yes.
| Quote: | Function defined in a class scope
have access to class variables (non-static functions to instance and
static data members, and statin functions to static data members).
|
Yes.
| Quote: | So
why should functions defined at a function (or deeper) scope be any
different?
|
Because local variable are different -- the nested function
would need some kind of pointer back to the stack frame of
its parent function, and *if* we allow the address of the
nested function to be taken then the situation is complicated;
to call the function via that pointer, we must somehow be able
to track down its context (meaning roughly the stack frame of
the function that took its address).
Of course there are various approaches: we could ban taking
the address of a nested function, but that rules out many
likely uses. We could rule out passing the address of a
nested function to another function for later use. But if
we just say that nested functions have no special access to
local variables in their containing function, there's no
need to worry about this -- the nested function behaves just
as a namespace-scope function *except* for being more tightly
scoped.
-- James
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Sun May 23, 2004 6:42 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
John G Harris wrote:
| Quote: | In message <kcgrc.598$_o.238@fed1read05>, James Dennett
[email]jdennett (AT) acm (DOT) org[/email]> writes
snip
It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities apart from being able to define functions in
a smaller scope. We make most things as local as possible,
but non-member functions right now must be defined at
namespace scope -- something of an anomaly.
Callback functions are a prime example of functions that
would sometimes be tidier tucked away inside the function
that registers them.
snip
1 Would you accept that you can't have pointers to inner functions?
2 No? Then would you accept that inner functions can't access outer
variables?
3 No? Then would you accept that inner functions can access only static
variables?
4 No? Then would you accept the job of writing the full and precise
definition of when destructors are run and when they aren't?
I think you were suggesting 2, but others go further.
John
|
Thank you for stating some of the issue clearly.
2 was where I was aiming. I find the idea of pointers to
nested functions appealing. 3 is reasonable too, and
probably no harder to implement.
Some would have nested functions as full closers, and hence
require garbage collection, but the language they want is
not C++. And that's fine. There's room on this planet (and
in many large projects) for more than one programming language.
-- James
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sun May 23, 2004 7:13 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
[email]jdennett (AT) acm (DOT) org[/email] (James Dennett) writes:
| Quote: | Hyman Rosen wrote:
Prateek R Karandikar wrote:
What is the strong reason for functions not nesting? If someone in
effect wants to create nested functions, it can be done by defining a
local class with a function defined inside the class definition.
Traditionally, languages which permit nested functions also permit
those
functions to access the local variables of the enclosing function, while
local classes do not. So your techniques are inequivalent.
Supporting nested functions with local variable access is
complicated
because of pointers to nested functions, which must somehow refer to
the variables of the correct scope(s) when invoked. Various techniques
can be employed for this, but C never had such things, and so neither
did C++.
It's very odd that every time someone asks why C++ doesn't
support nested functions, the answers are mostly along the
lines of "Well, supporting nested functions _with access to
local variables from the surrounding scope_ has implementation
issues, and cost, so it's not done."
It's odd because I suspect that many of the people asking have
no interest in this complication. They don't want new
capabilities apart from being able to define functions in
a smaller scope. We make most things as local as possible,
but non-member functions right now must be defined at
namespace scope -- something of an anomaly.
|
You could define them at namespace scope in one translation unit, and
then declare them at function scope in the (different)
translation unit where they were used. This constrains the
visibility to a smaller scope, but nobody does it.
| Quote: | Callback functions are a prime example of functions that
would sometimes be tidier tucked away inside the function
that registers them.
Personally I've grown to accept the C/C++ limitation that
functions definitions cannot be nested, but the most common
reason why the idea is dismissed seems suspect to me.
|
Different people mean different things by 'nested function'. Some
people mean full-blown closures. Most people at least expect
access to local variables of the outer function, but clearly you
don't consider that necessary.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sun May 23, 2004 7:40 am Post subject: Re: Nested functions? TC++PL: "Constructs ought to nest ..." |
|
|
[email]ahp6 (AT) email (DOT) byu.edu[/email] ("Adam H. Peterson") writes:
| Quote: | These are not new capabilities. Functions defined in a namespace
scope have access to namespace variables. Function defined in a
class scope have access to class variables (non-static functions to
instance and static data members, and statin functions to static
data members). So why should functions defined at a function (or
deeper) scope be any different?
On the other hand, classes declared at function scope don't have
access to function variables. Consider:
|
Unless those variables are static. For automatic variables, I just
tend to lambda-lift them as constructor parameters.
| Quote: | void f() {
int i=0;
struct C {
static void g() {
i=1; // Error
}
};
C::g();
}
|
void f() {
int i = 0;
struct C {
int& i;
C(int& v) : i(v) { }
void g() { i = 1; }
};
C(i)::g();
}
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|