Local function declarations

D

Dave Theese

Consider the following declaration *inside of a function*:

int j(int);

My compiler (VC++ 7.1) accepts this. typeid returns a type of int
__cdecl(int).

Local functions are not legal in C++. Is this an error or...

Is there some reason it would be legal to declare (but not define) a
function locally?
 
J

Jack Klein

Consider the following declaration *inside of a function*:

int j(int);

My compiler (VC++ 7.1) accepts this. typeid returns a type of int
__cdecl(int).

Local functions are not legal in C++. Is this an error or...

Is there some reason it would be legal to declare (but not define) a
function locally?

The purpose of putting a function prototype or external object
declaration inside a function or even a smaller block is that it
restricts the scope of the declaration, and therefore the use of the
object by name, to that block.

If you put a prototype or external object declaration outside of a
function, its scope extends to the end of the translation unit.

If you have a file with multiple functions, for example member
functions of a class, and only one of them needs to reference a
function or object defined elsewhere, there are two ways you can
restrict the scope of the function prototype or declaration.

The first is to put that particular function last in the file, and put
the function prototype or external declaration just above it. The
other is to put the prototype or declaration inside the function or
even smaller block just where it is needed.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top