Topic: main() in a namespace
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/07/09 Raw View
Steve Clamage wrote:
[...]
> The compiler is correct on all counts, according to 3.6.1 "Main
> function". You can declare entities called "main" in namespaces
> and classes. The main program entry point is a function "main"
> in the global namespace.
Does this include unnamed namespaces? That is, would the following
program be conforming?
namespace
{
int main() { return 0; }
}
int call_main() { return main(); }
int main() { return call_main(); }
And what about extern "C"?
namespace N
{
extern "C" int main(); // a C linkage main!
}
int main()
{
N::main();
}
This case is especially interesting since the extern "C" int main()
is the same function as a global extern "C" int main() - and I guess
that is not allowed (is it?).
[ 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: loewis@informatik.hu-berlin.de (Martin v. Loewis)
Date: 1998/07/09 Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> namespace
> {
> int main() { return 0; }
> }
>
> int call_main() { return main(); }
>
> int main() { return call_main(); }
This is correct.
> namespace N
> {
> extern "C" int main(); // a C linkage main!
> }
>
> int main()
> {
> N::main();
> }
[basic.start.main]/3 says
>> The linkage (3.5) of main is implementation=ADdefined.
If this is meant to include language linkage, it seems that
implementations have to specify whether your second example is valid
or not (i.e. if they specify "C" linkage for main, it is incorrect).
Regards,
Martin
[ 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 <david.tribble@noSPAM.central.beasys.com>
Date: 1998/07/09 Raw View
I haven't got the CD handy, so perhaps someone can answer my question.
Is it conforming to declare a non-member function named 'main' within
a namespace? For example:
namespace Mine
{
int main()
{
return 1;
}
}
int main()
{
return 0;
}
FWIW, MS-VC++/5.0 allows this, and treats the two functions as
different. Indeed, it won't produce an executable program unless
a 'main()' function exists that is not defined inside a namespace.
But is it okay to declare a separate nonmember function named
'main' within a (named) namespace? (I assume that only the
un-named 'main' function is special to the compiler/linker, so
that any other function named 'main' simply acts like a normal
function.)
-- David R. Tribble, dtribble@technologist.com --
-- C++, the PL/1 of the 90s.
[ 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: 1998/07/09 Raw View
David R Tribble <david.tribble@noSPAM.central.beasys.com> writes:
>I haven't got the CD handy, so perhaps someone can answer my question.
>Is it conforming to declare a non-member function named 'main' within
>a namespace? For example:
> namespace Mine
> {
> int main()
> {
> return 1;
> }
> }
> int main()
> {
> return 0;
> }
Yes, it's fine.
>FWIW, MS-VC++/5.0 allows this, and treats the two functions as
>different. Indeed, it won't produce an executable program unless
>a 'main()' function exists that is not defined inside a namespace.
The compiler is correct on all counts, according to 3.6.1 "Main
function". You can declare entities called "main" in namespaces
and classes. The main program entry point is a function "main"
in the global namespace.
--
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: loewis@informatik.hu-berlin.de (Martin v. Loewis)
Date: 1998/07/09 Raw View
David R Tribble <david.tribble@noSPAM.central.beasys.com> writes:
> I haven't got the CD handy, so perhaps someone can answer my question.
> Is it conforming to declare a non-member function named 'main' within
> a namespace?
Yes, it is. 3.6.1 [basic.start.main] says main is global. Paragraph 3
details
>> The function main shall not be used (3.2) within a program. The
>> linkage (3.5) of main is implementation=ADdefined. A program that
>> declares main to be inline or static is ill=ADformed. The name main
>> is not otherwise reserved. [Example: member functions, classes, and
>> enumerations can be called main, as can entities in other
>> namespaces. ]
This is quite explicit.
Hope this helps,
Martin
---
[ 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 ]