[comp.lang.c++] Local classes, are they worth it????

pfaust@hpcll05.HP.COM (Paul Faust) (02/21/90)

Local classes with the combination of the +d option give the illusion of
Pascal style nested functions.  In cfront, if a local class defines member
functions (with their bodies), the +d option will cause the function to 
dumped out prior to the definition of the function currently being processed.
Static members are properly hoisted out.  However, locals that are
defined prior to the class are not accessible and cfront generates an error.

For example, in the following program:

    1	int x_file;
    2	
    3	class foo_file {
    4	private:
    5		int y_file;
    6	public:
    7		foo_file() {}
    8		void do_it_file () { x_file = 1;}
    9	};
   10	
   11	main () {
   12		int x_local = 0;
   13	
   14		class foo_local {
   15		  int y_local;
   16		public:
   17		  foo_local() {}
   18		  void do_it_local () { x_local = 1; x_file = 2;}
   19		};
   20	
   21		foo_file foo_int_file;
   22		foo_int_file.do_it_file();
   23	
   24		foo_local foo_int_local;
   25		foo_int_local.do_it_local();
   26	}

On line 18, cfront reports an error message that x_local is undefined.  I 
fail to see the difference between that and the access in the file scoped
class accessing the file scoped variable.

To implement this, there are two options:

   1.  All the locals are pulled out as well and appropriately mangled.
   2.  In a compiler solution, a static link ala Pascal is used for the 
       member functions.

Is the feature worth it, and as a side issue, are member function
bodies defined within a class body, the real culprit of this problem, worth
the complexity.  The user already has an option to specify inline on any 
function.  The body definition doesn't really buy you anything and will get 
you into the previously mention difficulty.

What do people think???

pcg@aber-cs.UUCP (Piercarlo Grandi) (02/23/90)

In article <58170004@hpcll05.HP.COM> pfaust@hpcll05.HP.COM (Paul Faust) writes:

  Local classes with the combination of the +d option give the illusion of
  Pascal style nested functions.  In cfront, if a local class defines member
  functions (with their bodies), the +d option will cause the function to 
  dumped out prior to the definition of the function currently being processed.
  Static members are properly hoisted out.  However, locals that are
  defined prior to the class are not accessible and cfront generates an error.

Reading Lippman on this subject makes it clear that local classes have been
officially burdened with enough restrictions to the effect that they don't
really implement a nested scope. This is appropriate -- you don;t really
want to have nested scopes in a language supposedly compatibl with C...
-- 
Piercarlo "Peter" Grandi           | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk
Dept of CS, UCW Aberystwyth        | UUCP: ...!mcvax!ukc!aber-cs!pcg
Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk