views:

88

answers:

3

A. Is the following attempt valid to define the entry point 'main' of a C++ standalone program?

namespace{
 extern int main(){return 0;}
}

As far as I understand, it satisifies all the criteria about 'main' in the C++ standard (external linkage, available in global namespace because of the implicit using directive).

So is this program ill-formed and why? Any reference to the Standard?

B. I have gone through dicussions on EXIT_FAILURE and EXIT_SUCCESS but unable to conclude if EXIT_SUCCESS should always be 0. This is because as per the Standard leaving out a return statement in 'main' is equivalent to return 0. So I guess, EXIT_SUCCESS should be always 0.

$18.3 - "If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned."

A: 

A. When I try to run your code in Visual C++ 2010, I get this:

Error 1 error LNK1561: entry point must be defined

In C++, main must be in the global namespace (i.e. ::main) and cannot be a (class or instance) member function, although the name is not reserved, and may be used for other (ordinary) member functions or non-member functions.

Exit status

The value returned from the main function becomes the exit status of the process, though the C standard only ascribes specific meaning to two values: EXIT_SUCCESS (traditionally zero) and EXIT_FAILURE. The meaning of other possible return values is implementation-defined.

More about it here.

Leniel Macaferi
@Leniel Macaferi: Yes, this is the site which I was reading and which triggerred this question (My comments in Tony's post).
Chubsdad
+2  A: 

Here, namespace { } is an anonymous namespace - the idea is that nothing should escape from there into the global namespace - in practice, it should all be prefixed with something that won't clash with other translation units. You've made it extern, but not extern "C", so the special rule about main not being mangled won't protect it, and the symbol won't satisfy the linkage requirements. Anyway, to put something extern inside an anonymous namespace is just confused thinking. You consistently go out of your way to see how you can twist the Standard into weird contortions :-). Am curious - is there a reason? Are you interested in this academically, or implementing a compiler or compiler test suite or something?

See you've added discussion of EXIT_SUCCESS and EXIT_FAILURE to your question. The key point here is that you can use these from main or exit() and know they'll work on any operating system. For returning 0, it's a non-issue whether you use EXIT_SUCCESS, 0 or let main "fall through". The OS may or may not actually return 0 to the Operating System... it may be that the run-time maps that to some other value that means "success" on that OS. But, for failure - if you return an arbitrary non-0 value it may bypass that mapping, and either indicate something different from simple failure or happen to coincide with the success value. Values can also be treated differently on different operating systems: many (e.g. Linux) will strip your return value of all but the 8 least-significant bits, such that return 256 is equivalent to return 0. That's why it's not a good idea to guess at a suitable non-0 value.

Tony
While reading http://en.wikipedia.org/wiki/Main_function_(programming), the statement "The Sun Studio 11 C++ compiler allows main to be used in a non-global namespace and also as a (class or instance) member function." triggerred this thinking.
Chubsdad
I am not into anything related to Standards/compiler/compiler test suite etc though I would love to. I am just passionate about C++
Chubsdad
Well, that Wikipedia paragraph starts "main must be in the global namespace (i.e. ::main) and cannot be a (class or instance) member function, although the name is not reserved, and may be used for other (ordinary) member functions or non-member functions. The Sun Studio 11 C++ compiler...". Sounds very much like they're saying Sun allows an exception to the Standard.
Tony
And wikipedia is not my usual source for standard conformance information.
Martin York
+1  A: 

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).

James McNellis