Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why wasn't the idea of nested functions, implemented in older c++ standard?

was the idea of nested functions considered to be useless during the time of developing older c++ standard, because its usage is basically covered by another concept like object-oriented programming; or it wasn't implemented just as a matter of simplification?

like image 369
Pooria Avatar asked Aug 03 '10 19:08

Pooria


People also ask

Why C does not have nested functions?

Nested function is not supported by C because we cannot define a function within another function in C. We can declare a function inside a function, but it's not a nested function.

Is nested function possible in C?

C does not enable nested functions because we cannot define a function within another function in C. It is possible to declare a function within a function, but this is not a nested function.

Why are nested subprograms not allowed?

This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the containing module.

Is it bad practice to use nested functions?

no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.


2 Answers

Nested functions - to be useful - need the stack frame of the containing function as context. Look at this:

class Foo()
{
   void Tripulate()
   {
       int i=0; 

       void Dip()
       {
           // ...
       }

       int x = 12;
       for(i=1; i<=3; ++i)
       {
          int z= 33;
          Dip();
          // ...
       }
   }
}

Which values should Dip() get access to?

None? you have just duplicated the functionality of (anonymous) namespaces, more or less.
Only to i, because it's the only one defined before the function?
Only to i and x, because they are in the sam scope as Dip()? Does the compiler have to make sure the constructor of x did already run, or is that your job?
What about z?

If Dip gets any access to both the local values of tripulate and the stack frame, so the internal prototype would be

   void Dip(Foo * this, __auto_struct_Dip * stackContext)
   {
       // ...
   }

You have basically replicated the functionality of structs / classes and member functions, but on two incompatible and non-exchangable paths. That's a lot of complexity for a questionable gain.

I've wished for local functions a few times, simply because this would better indicate the scope where this is needed. But with all the questions... there are more useful things to throw more complexity onto C++.

[edit] With C++0x, lambdas can do that, allowing to explicitly state what they capture.

like image 85
peterchen Avatar answered Sep 27 '22 20:09

peterchen


The idea was raised (a number of times) during standardization. Steve Clamage wrote a post in comp.std.c++ in 1996 responding to a question about adding them to C++. He summarized his points as:

In the end, it seems to me that nested functions do not solve any programming problem for which C++ does not already have a solution at least as good.

A proposal to add nested functions to C++ should show an important class of programming problems solved by nested functions which are inconvenient to solve otherwise. (Perhaps there are such problems, and I simply haven't seen them.)

A later (1998) post by Andrew Koenig indirectly states that the committee did discuss it, but nothing seems to have materialized from it.

The obvious way to support nested functions requires hardware support, and still adds a bit of overhead. As pointed out in a post by Fergus Henderson, it's also possible to support them via "trampoline" code, but this method adds some compiler complexity (even if they're never used).

As an aside: all three are members of the C++ standard committee (or at least were at the time). If memory serves, at that time Steve was either convener of the ISO committee or chairperson of the US committee.

like image 38
Jerry Coffin Avatar answered Sep 27 '22 21:09

Jerry Coffin