Topic: My try at the reverse function
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/01/27 Raw View
Martijn Lievaart wrote:
>
> [ comp.std.c++ added as I added a question on what the C++ standard allows ]
>
> Paul Lutus wrote in message <78imbu$rdj$1@remarQ.com>...
....
> >Do you mean that you can't call main() recursively in ANSI C++?
> >
>
> Ahh, someone found it! And I thought it would be obvious. I still would like
> to know if C allows this or forbids it.
I don't have a copy of the C89 standard, so what I'm about to say may be
specific to the draft C9X standard.
C doesn't specifically allow it, but neither does it forbid calling
main(). It does, however contain two places where it is implied that
you're permitted to call main():
Section 5.1.2.2.3 "Program termination" in the draft C9X standard says
that "... a return from the initial call to the main function is". This
seems to imply that there can be more than one call to main(). Section
7.19.3 "Files" says that "If the main function returns to its original
caller, ...", which also seems to imply that main() can have more than
one caller.
> Actually I'm not absolutely sure the C++ standard forbids it, but a C++
> compiler has to treat main specially so it is *not* an ordinary C++
> function. Can any C++ guru answer this?
Section 3.6.1 "main function" in the C++ Standard says quite explicitly
that "The function main shall not be used (3.2) within a program."
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/01/27 Raw View
In article <78ju6b$jcq@news3.euro.net>, nobody@orion.nl says...
[ ... ]
> >Do you mean that you can't call main() recursively in ANSI C++?
> >
>
> Ahh, someone found it! And I thought it would be obvious. I still would like
> to know if C allows this or forbids it.
C allows main to be recursive. I don't recall a specific statement to
this effect related to main, but it simply says that main is a
function, and says that any function can be called recursively, either
directly or indirectly.
> Actually I'm not absolutely sure the C++ standard forbids it, but a C++
> compiler has to treat main specially so it is *not* an ordinary C++
> function. Can any C++ guru answer this?
Section 3.6.1/3 says "The function main shall not be used within a
program." IOW, you can't call main recursively in 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fvali@biotrack.com
Date: 1999/01/27 Raw View
In article <78ju6b$jcq@news3.euro.net>,
"Martijn Lievaart" <nobody@orion.nl> wrote:
<snip>
> Actually I'm not absolutely sure the C++ standard forbids it, but a C++
> compiler has to treat main specially so it is *not* an ordinary C++
> function...
>
> Martijn
Yes, main cannot call itself according to section 3.6.1 of the Standard.
Essentially if you define a function of type
int main() {... }
or, but not also
int main( int, char* [] ) {...}
in global scope, it is ill-formed to 'use' main in any expression
if it refers to either one of the two functions defined above -
you can't define both at global scope.
But you can do useless stuff like redeclare its name, but you cannot
declare an overload in the global namespace. i.e.
int main(); //well-formed
int main( int ); // ill-formed, even if you don't define it
You can also have:
struct main
{
};
enum main { junk };
Now according to my reading of the standard
the following examples are well-formed since the
standard says that
you can call entities in other namespaces main.
namespace
{
int main( int ) { return 1; } // This overload is well-formed.
}
void f()
{
int i = main(3); // does not 'use' int ::main() so we're fine
}
And this is well-formed too:
namespace A
{
int main(char*) { return 1; }
}
using namespace A; // well-formed
// But this should be ill-formed
using A::main; // ill-formed
fun, huh?
-hope that was helpful
-fais
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/01/28 Raw View
In article <36AE5F5F.57A95432@wizard.net>, James Kuyper
<kuyper@wizard.net> writes
>I don't have a copy of the C89 standard, so what I'm about to say may be
>specific to the draft C9X standard.
>C doesn't specifically allow it, but neither does it forbid calling
>main(). It does, however contain two places where it is implied that
>you're permitted to call main():
I think this is a case of what is not forbidden is allowed.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/01/30 Raw View
Steve Clamage wrote:
> The restrictions on "using" mean that you cannot take the
> address of main or call it as a function in C++.
...
>
> It is easy to work around the restrictions on main. The limitation
> is merely syntactical, not fundamental. Given a main function that
> you would like to call, just rename it to, for example, my_main,
> and do this:
>
> int my_main(int, char**); // the original main
> int main(int argc, char** argv)
> {
> return my_main(argc, argv);
> }
I generally try to make every function and variable a member of
some class; I generally abhor free functions and variables. (This
approach is consistent with the Java and Eiffel object models.)
To this end, I usually code my ::main() function like this:
int main(int argc, char **argv)
{
Program pgm;
return pgm.main(argc, argv);
}
Class 'Program' then deals with all the details that a main function
would normally deal with. It has the added advantage of being
a member of a class, so things like command line options and such
can be members of the class (instead of global variables).
This also makes it possible to instantiate multiple Program objects,
which can come in handy in multi-conversational programs.
And since Program::main() is not a special function, it can call
itself recursively if necessary.
-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Martijn Lievaart" <nobody@orion.nl>
Date: 1999/01/26 Raw View
[ comp.std.c++ added as I added a question on what the C++ standard allows ]
Paul Lutus wrote in message <78imbu$rdj$1@remarQ.com>...
><< There is still another point you've missed I think, but that could also
>be a
>C++ issue that is allowed in C. Anyone for a puzzle? >>
>
>Do you mean that you can't call main() recursively in ANSI C++?
>
Ahh, someone found it! And I thought it would be obvious. I still would like
to know if C allows this or forbids it.
Actually I'm not absolutely sure the C++ standard forbids it, but a C++
compiler has to treat main specially so it is *not* an ordinary C++
function. Can any C++ guru answer this?
Martijn
--
My reply-to address is intentionally set to /dev/null
reply to mlievaart at orion in nl
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stanley@West.sun.com (Stanley Friesen [Contractor])
Date: 1999/01/26 Raw View
In article <78ju6b$jcq@news3.euro.net>,
Martijn Lievaart <nobody@orion.nl> wrote:
>
>Paul Lutus wrote in message <78imbu$rdj$1@remarQ.com>...
>>Do you mean that you can't call main() recursively in ANSI C++?>>
>
>Ahh, someone found it! And I thought it would be obvious. I still would like
>to know if C allows this or forbids it.
>
>Actually I'm not absolutely sure the C++ standard forbids it,
It does. In fact it does more, it forbids any explicit call to main at all.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/01/27 Raw View
"Martijn Lievaart" <nobody@orion.nl> writes:
>[ comp.std.c++ added as I added a question on what the C++ standard allows ]
>Paul Lutus wrote in message <78imbu$rdj$1@remarQ.com>...
>>
>>Do you mean that you can't call main() recursively in ANSI C++?
>Ahh, someone found it! And I thought it would be obvious. I still would like
>to know if C allows this or forbids it.
>Actually I'm not absolutely sure the C++ standard forbids it, but a C++
>compiler has to treat main specially so it is *not* an ordinary C++
>function. Can any C++ guru answer this?
In C, "main" is just a function. The only things special about it
are that it is the program entry point, and has a predefined form.
You can take its address or call it, in particular.
In C++, "main" has many more restrictions. Quoting from the standard,
section 3.6.1, "Main function":
--------------------------------
An implementation shall not predefine the main function. This function
shall not be overloaded. It shall have a return type of type int, but
otherwise its type is implementation-defined. All implementations shall
allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
...
The function main shall not be used (3.2) within a program. The linkage
(3.5) of main is implementation-defined. A program that declares main
to be inline or static is ill-formed. ...
--------------------------------
Other provisions of the standard allow equivalent forms, so that
int main(int count, char** strings) { /* ... */ }
must be allowed as well, for example.
Notice also the absolute requirement that main have a return
type of int.
Section 3.2 defines what it means for something to be "used":
-------------------------------
An expression is potentially evaluated unless either it is the operand
of the sizeof operator (5.3.3), or it is the operand of the typeid
operator ...
An object or non-overloaded function is used if its name appears in
a potentially-evaluated expression.
-------------------------------
The restrictions on "using" mean that you cannot take the
address of main or call it as a function in C++.
The reasons for the restriction are to allow implementations to
recognize main uniquely as the program entry point, and be free
to do special program initialization or cleanup as part of main.
That is, a compiler might want to emit some special extra code
in main. It can safely do so, because no valid program can
call main internally.
It is easy to work around the restrictions on main. The limitation
is merely syntactical, not fundamental. Given a main function that
you would like to call, just rename it to, for example, my_main,
and do this:
int my_main(int, char**); // the original main
int main(int argc, char** argv)
{
return my_main(argc, argv);
}
This trick is sometimes used when you have a main program written
in C and you want to include C++ functions in the program. For
maximum portability, the main program should be compiled by
the C++ compiler. If you don't want to modify main to make it
valid C++, you can leave it in C but change its name. Then you
write the C++ main program as a wrapper around it, as above.
Since in this new example main would be a C function, you need
to declare it as a C function in the C++ code:
extern "C" int my_main(int, char**); // the original C main
int main(int argc, char** argv)
{
return my_main(argc, argv);
}
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/01/27 Raw View
In article <78ju6b$jcq@news3.euro.net>, Martijn Lievaart
<nobody@orion.nl> writes
>Ahh, someone found it! And I thought it would be obvious. I still would like
>to know if C allows this or forbids it.
>
>Actually I'm not absolutely sure the C++ standard forbids it, but a C++
>compiler has to treat main specially so it is *not* an ordinary C++
>function. Can any C++ guru answer this?
In C main is like any other function, it can be called recursively and
you can take its address.
In C++ you are prohibited from either calling main() or taking its
address. If you need to import a C program that uses this mechanism you
have to rename all occurences of main() as something like mymain and
then write main() to forward to mymain()
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]