A. Is the following attempt valid to define the entry point 'main' of a C++ standalone program?
No. The C++ standard says "A program shall contain a global function called main
" (§3.6.1/1). In your program, the main
function is not in the global namespace; it is in an unnamed namespace.
The implicit using directive only allows names from the unnamed namespace to be looked up and used in the enclosing namespace; it does not add those names to the enclosing namespace. Specifically, "a using-directive does not add any members to the declarative region in which it appears" (§7.3.4/1).
Is this program ill-formed and why?
The program is not necessarily ill-formed. There is no rule against having a function named main
in a namespace other than the global namespace; such a function is just not the main
function. namespace { int main(); }
and int main()
are two different functions, and a well-formed program can have both of them.
Note that if your program does not have a main
function in the global namespace, then the program is ill-formed (because, as stated above, a program in a hosted environment must have a main function).
B. I have gone through dicussions on EXIT_FAILURE
and EXIT_SUCCESS
but unable to conclude if EXIT_SUCCESS
should always be 0
.
There is no requirement that EXIT_SUCCESS
expand to 0
. The C standard simply says that in <stdlib.h>
,
The macros defined are...EXIT_FAILURE
and EXIT_SUCCESS
which expand to integer constant expressions that can be used as the argument to the exit
function to return unsuccessful or successful termination status, respectively, to the host environment (C99 §7.20/3).
(Since those two macros are defined in <cstdlib>
, the C standard contains the specification for them).