[comp.lang.c++] function variable

daniel@osf.org (Daniel Dardailler) (07/19/90)

I have a g++ problem:

  main()
  {
  int (*  foo) ();
  }
doesn't compile with:  `foo' undeclared (first use this function)

but:

  main()
  {
  static int (*  foo) ();
  }
and
  int (*  foo) ();
  main()
  {
  }

works fine.

Any idea? 

        Daniel Dardailler          |          OSF/Motif Team  
     Open Software Foundation      |      Email : daniel@osf.org           
       11 Cambridge Center         |      Phone : (617) 621 8840    
       CAMBRIDGE, MA 02142         |      Fax   : (617) 621 0584

krohn@czech.sw.mcc.com (Eric Krohn) (07/20/90)

In article <10983@paperboy.OSF.ORG> daniel@osf.org (Daniel Dardailler) writes:
] I have a g++ problem:
] 
]   main()
]   {
]   int (*  foo) ();
]   }
] doesn't compile with:  `foo' undeclared (first use this function)

Welcome to one of the C++ language's inherent ambiguities.  A statement of
the form
	T (* foo) ();
(where T is a typename) could be either:
1) an expression 	// Dereference foo, cast it to a T, call function or
			// overloaded operator ().
2) a declaration	// foo is pointer to function returning T.

As of AT&T's C++ 2.0, the language manual says if a statement can be parsed as
either an expression or a declaration, then it must be parsed as a declaration.

In your case, g++ parsed it (incorrectly) as an expression.
When you added the storage class specifier (static), you disambiguated the
statement so that it could only be parsed as a declaration.

If you want to force your statement to be parsed as an expression, you can say
	0, int (*foo) ();
-- 
Eric J. Krohn
krohn@sw.mcc.com